# Collision detection

This topic is 4308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have 2 questions to ask you. 1. I have created a collision detection for my program and it works alright, but i'm not sure how most of you implement collision. I think my approach might be bad. I implemented ray casting collision ray->mesh polygon with D3DXIntersect. Now, while this works fine, when I searched for collision detection tutorials on the internet I only found ones about bounding boxes, all I've heard about my method is that is processor intensive. I used ray->mesh polygon because I need my character(it's supposed to be a game) to collide with house walls, and be able to enter the house. I cannot calculate the bounding sphere/box from the whole house(it's a single mesh) because then it would be just wierd, and I could not enter the house. I was thinking that perhaps it's better to calculate the bounding boxes of each wall specially so the house is made of multiple meshes, or just calculate bounding volume on each subset(if this is even possible)? Or is the method I used good? I know this method is processor intensive but I plan to be using it only for one major level mesh, while smaller objects will use bounding volumes. 2. If anyone knows how to retrieve mesh normals from an .x file I would really appreciate to say how. The whole process please :) I tried it using d3dx functions, I tried opening the .x file manually but I cant get it to work. With d3dx do I have to use vertex buffer(I already have the mesh loaded and displayed on the screen)? If so, how do i use it properly? Sorry for the long post

##### Share on other sites
I would recommend that you ask your first question in the Maths & Physics forum - you'll probably get a better answer by asking the right people [smile]

Quote:
 Original post by BearishSun2. If anyone knows how to retrieve mesh normals from an .x file I would really appreciate to say how. The whole process please :) I tried it using d3dx functions, I tried opening the .x file manually but I cant get it to work. With d3dx do I have to use vertex buffer(I already have the mesh loaded and displayed on the screen)? If so, how do i use it properly?
You haven't specified which version of the API and which language you're using.

There are a couple of ways to get the vertex normals from a mesh, the first is very easy but isn't particularly quick. The second is more involved but should be faster to work with.

Firstly you'll want to check if your mesh has any normals:

if( 0 != (mesh->GetFVF() & D3DFVF_NORMAL) ){    // We have normals, everything is great :-)}else{    // Mesh does *not* contain normals. Two possibilities:    // 1. Generate them using D3DXComputeNormals()    // 2. Skip processing}

The simple way is to use ID3DXMesh::CloneMeshFVF() with the D3DFVF_NORMAL format. This will give you a new mesh containing only the normals. You can then use ID3DXMesh::LockVertexBuffer() to read them out into a D3DXVECTOR3 array. If you need to know which normal belongs to which face (etc..) then you can inspect the index buffer, attribute table and adjacency information - all depends what you actually need.

hth
Jack

##### Share on other sites

I still cannot get it to work tho, this is what i have done:

//VERTEX
struct vertex
{
vertex(){}
vertex(float nx, float ny, float nz):
_nx(nx),
_ny(ny),
_nz(nz)
{}
float _nx, _ny, _nz;
static const DWORD FVF = D3DFVF_NORMAL;
};

//INIT
mesh->CloneMeshFVF(NULL, D3DFVF_NORMAL, device, &clonedMesh);

device->CreateVertexBuffer(clonedMesh->GetNumFaces() * sizeof(vertex),
0, vertex::FVF, D3DPOOL_MANAGED, &VB, 0);

//MAIN LOOP
vertex *vert;
VB->Lock(0, clonedMesh->GetNumFaces() * sizeof(vertex), (void**)&vert, 0);

//LOOP HERE

VB->Unlock();

When I place a breakpoint on vert[1]._ny(for example), Visual Studio reports it as { _nx = ???, _ny = ???, _nz = ??? }. For each _nx, _ny, _nz it says Expression cannot be evaluated.

So the program runs, but I can't get any data from the buffer...no error when compiling

What did I do wrong?

##### Share on other sites
Actually I managed to get the normals, but how do I extract them?

I tried vert[1]._nx and it shown me a normal from the .x file, but their order doesn't seem to be the same as in the .x file.

##### Share on other sites
Actually never mind again, I was optimizing the mesh, caused the normals to go freakish. Works now.

Thanks!

##### Share on other sites
on q2:

Bounding Boxes exist for quickly dtermining which parts of you maps could possibly collide with the player(or whatever). i.e. when you're not colliding with the bounding box of the house you do not need to check ray-triangle collision on it.

regards,
m4gnus