Sign in to follow this  
kiwidrifter

Collision detection with a mesh

Recommended Posts

What i have done is load up a map as a mesh in a very basic fps i am fiddling with. what im wondering is if there is anyway to detect when the player(camera) hits the walls without going through and one by one entering each wall into the code. im hoping there is because i want to be able to load in other maps and not have to enter in each wall individually. cheers

Share this post


Link to post
Share on other sites
Quote:
Original post by kiwidrifter
What i have done is load up a map as a mesh in a very basic fps i am fiddling with.

what im wondering is if there is anyway to detect when the player(camera) hits the walls without going through and one by one entering each wall into the code.

im hoping there is because i want to be able to load in other maps and not have to enter in each wall individually.

cheers
You wouldn't enter the collision data into the code any more than you'd enter the mesh data itself into the code. In other words, the collision data should be loaded from a file (or whatever) in the same way that the mesh itself is. (Or, if the environment and collision representation is simple, you could just generate the collision data from the mesh data at run time when the mesh is loaded.)

Share this post


Link to post
Share on other sites
are u able to explain a bit more.

i know i will be able to set up the code to detect collision by typing in the vertices of the walls but this would be ridicolous having to do this every time i load in a different map

i think what im trying to do is retrieve the vertices from the mesh into an array of vertices that i can then use in a function for collision


EDIT: ive managed to create an object for my 'player' which just happens to share its coordinates with my camera, ive figured it will be easier this way.

still need to figure out how to retrieve the vertices or planes from a mesh, once ive done this it should be easy enough to write some code to check for the players collision with any walls of my map and also the floor(so i can walk up stairs etc)

ive got no idea how though and i cant find any examples on google or here(id prefer and example code, easier for me to learn this way)

[Edited by - kiwidrifter on March 21, 2010 1:47:09 AM]

Share this post


Link to post
Share on other sites
I think you'll have to supply more information (e.g. how you're loading the mesh, what graphics API you're using, etc.). As is, it's not really clear (to me at least) what the problem is.

Share this post


Link to post
Share on other sites
ok im using visual basic 2008 to write the code in c++, using directx

im not using anything to handle collision at the moment.

if i were to do it, they only way i can think of is do an if statement that checks to make sure the Xpos and Zpos of the player doesnt not equal the Xpos and Zpos of any walls.

but i would have to manually enter in each walls vertices and have an if statement for each one.

ie:

//2 walls infinite wide
wall[0]->Xpos = 50;
wall[1]->Xpos = -50;

if(player->Xpos == wall[1]->Xpos + 1 && player->Xlook <= wall[1]->Xpos + 1)
player->Xmove = false;


im trying to figure out how to retrieve the Xpos and Zpos of each wall(subset) from the mesh so i dont have to manually enter them.

Share this post


Link to post
Share on other sites
Quote:
im trying to figure out how to retrieve the Xpos and Zpos of each wall(subset) from the mesh so i dont have to manually enter them.
As I mentioned earlier, I don't think we can answer this question unless we know what the problem is exactly. To restate my questions from earlier:

1. How are you loading the mesh?

2. What graphics API are you using?

Share this post


Link to post
Share on other sites
im using D3DXLoadMeshFromX() to load the mesh.

here is the whole function:


//Function to load a mesh
void LoadMesh(MESH* mesh, LPCTSTR file)
{
LPD3DXBUFFER bufMaterial;

D3DXLoadMeshFromX(file,
D3DXMESH_SYSTEMMEM,
d3ddev,
NULL,
&bufMaterial,
NULL,
&mesh->numMaterials,
&mesh->mesh);

D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();

mesh->material = new D3DMATERIAL9[mesh->numMaterials];
mesh->texture = new LPDIRECT3DTEXTURE9[mesh->numMaterials];

for(DWORD index = 0; index < mesh->numMaterials; index++)
{
mesh->material[index] = tempMaterials[index].MatD3D;
mesh->material[index].Ambient = mesh->material[index].Diffuse;

if(FAILED(D3DXCreateTextureFromFileA(d3ddev,
tempMaterials[index].pTextureFilename,
&mesh->texture[index])))
{
mesh->texture[index] = NULL;
}
}

return;
}


im using directx 9.0

Share this post


Link to post
Share on other sites
Quote:
im using D3DXLoadMeshFromX() to load the mesh.
I found this using Google (first hit for 'directx get mesh data'). Look towards the end in the section titled, 'I want to access the vertex data in my .x file, how can I?', and see if that helps.

Share this post


Link to post
Share on other sites
Quote:
that doesnt help at all, still doesnt explain how to retrieve the vertices from the mesh.
Hm, to me it looks like it's exactly what you're looking for.

Ok, try looking here; it's basically the same material, but it goes into more detail. If that's still not what you're looking for, then I'm afraid I'm out of ideas. (As I understand it, you're wanting to know how to get the vertex and index data from the mesh, and it seems to me that the links I've provided show how to do that. Maybe I'm missing something though...)

Share this post


Link to post
Share on other sites
I'll try and give a some short hints on how you could use the walls' vertex data to detect collisions, but since that topic generally involves quite a bit of math I'd suggest you read some collision detection articles on the net, e.g. here or here.

However, here's a short description of what you could do:


for each wall
calculate the wall's plane and normal from its vertices

each frame
//optional: calculate the set of walls the player potentially collides with - this is called the broad phase of collision detection
for each potentially colliding wall //this is the narrow phase
calculate the distance between the player and the wall's plane
if the distance is < player_radius the player might hit the wall (or is inside it if distance < 0)
calculate the intersection point of the ray shot from the player position to the wall's plane and the plane (i.e. the projection of the position onto the plane)
if the intersection point is inside the wall's area on the plane
the player hits the wall
else
you might need further calculations to see if the player hits an edge or corner
else
the player can't hit the wall


Hope that helps. As I said, you might need read up on the math and general collision detection.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this