• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Collision detection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1regonaros  Members

Posted 21 December 2012 - 11:08 PM

I'm currently in the process of creating a first person shooter-style game, I recently moved to C++ and I'm new to DirectX development, well, I've learnt the basics, drawing primitives, texture mapping, key detection and things like transformation of matrices, I've found a handful of useful online resources which have helped me a lot.

So I've learn quite a bit, but now I'm stuck at that stage where you're not sure what you should do next, 'what is next'.. where can I get started, should I try rendering two triangles and do some collision checks or should I move on to import .x meshes into my game, as for the former, this brings up the question "where can I learn about collision detection in regards to implementing it in with DirectX?".

Let's first explain exactly what Collision detection is, this is how Wikipedia describes it:

Collision detection typically refers to the computational problem of detecting the intersection of two or more objects. While the topic is most often associated with its use in video games and other physical simulations, it also has applications in robotics. In addition to determining whether two objects have collided, collision detection systems may also calculate time of impact (TOI), and report a contact manifold (the set of intersecting points). Collision response deals with simulating what happens when a collision is detected (see physics engine, rag-doll physics). Solving collision detection problems requires extensive use of concepts from linear algebra and computational geometry.

How can I implement a collision detection system in the easiest way possible inside a game engine? If you could, please refer to tutorials, videos, websites and other documentation.

I read somewhere about how rectangles are used in this by encapsulating the object and performing x-y-z axis checks to see whether or not they are colliding or have collided, but the real question is how can I implement these checks using standard game models and other world objects including terrain.

I have came across D3DXIntersect, not knowing much about it, I would appreciate if you could provide me with some examples of it being used in a DirectX game engine.

Also, how would I go about performing these collision checks whilst implementing rag-doll physics, can this be handled solely within DirectX or is other software required?

For example: A player walks into a wooden box, a collision check is performed and the box moves according to rag-doll physics, which of course requires some usage of advanced mathematics here.

How would I go about implementing this in the most easiest way possible?

### #2Medo3337  Members

Posted 21 December 2012 - 11:58 PM

I would recommend you to consider using a physics library, there are alot of physics libraries out there that support collision detection as well as body dynamics.

Since you will be working on First-Person Shooter game, you don't need collision only but you also need to implement physics to the game.

You can consider using one of the following physics engines:
- Havok
- Bullet
- PhysX

Here you can find a list of physics engines (Most of them support collision detection):
http://www.gamedev.net/topic/475753-list-of-physics-engines-and-reference-material-updated-7-march-2011/

### #30r0d  Members

Posted 22 December 2012 - 12:00 AM

There is no "easiest way possible" to implement ragdoll physics.  You should either use a physics engine or not have it in your game at all.  The only other alternative is to implement it yourself.  But, if you have to ask about how to implement it, then you wont be able to do it.

Also I'd suggest you keep DirectX strictly to your rendering code.  Yes I know it provides some math functionality, but it's a rendering library, use it for rendering.

### #4CreatureNZ  Members

Posted 22 December 2012 - 02:16 AM

If you have your rendering working, start by creating and drawing to objects in the world/scene. ID3DXmeshs are nice and quick to create. If you can do that, try moving them around the world independently of each other. Once you have that working, create some very basic collision tests using spheres.

if the distance between the two spheres is less than the sum of the radii squared, the objects are colliding.

http://www.gamasutra.com/view/feature/3015/pool_hall_lessons_fast_accurate_.php

Once you have this working, you can use it to further improve your collision test accuracy.

D3DXIntersect is used to test a ray (3D vector) against a ID3DXmesh, i recently used it in a project that required object selection (similar to RTS games unit selection).

This can be used for collision tests if you want, but you should use the sphere tests first.

To use D3DXIntersect, you need two vectors; one for the starting position of the ray, and another for the direction of the ray.

You also need an ID3DXmesh to do the test against. Make sure you know if the object is in world space or model space. If its in model space, you will need to multiply your vectors by the inverse of the world matrix to get them into the same space.

BOOL bHit = FALSE;
FLOAT fDistance = 0.0f;
D3DXVECTOR3 tvec3Origin;
D3DXVECTOR3 tvec3Direction;

//Intersects the ray with the mesh, sets bool with the result
D3DXIntersect(m_pMesh, &tvec3Origin, &tvec3Direction, &bHit, NULL, NULL, NULL, &fDistance, NULL, NULL);

if(bHit = TRUE)
{
//the ray has collided with the mesh
{
}
}

While this would work, I'm sure there is a much much better way of testing for collisions. The problem with using D3DXIntersect in this manner is that the ray is infinite, if an object is heading towards another, it will show up as a hit, hence the need for the second check. They way I have shown it above kind of wastes the accuracy of D3DXIntersect. A basic spherical collision test could have been performed at a fraction of the cost.

Never tried to implement rag doll physics, so not much help in that regard.

Edited by CreatureNZ, 23 December 2012 - 06:05 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.