#### Archived

This topic is now archived and is closed to further replies.

# Collision prob

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

## Recommended Posts

I know you guys must be sick of yet another newbie with yet another c.d. problem, but please... I am attempting to implement a basic c.d. system between 2 3d objects using bounding spheres. I thought I had it cracked when the code compiled (naive bastard that I am), but as soon as the app starts it registers that a collision has occured, and continues to do it even though the 2 objects are miles away. I think it might be the way I am using my matrices, but I am not sure. The code I am using to get the bounding spheres is this(I borrowed it from one of the posts in this forum) g_pMesh2->LockVertexBuffer(D3DLOCK_READONLY, &mesh2data); mesh2center.x = mesh2xpos; mesh2center.y = mesh2ypos; mesh2center.z = mesh2zpos; D3DXComputeBoundingSphere(mesh2data, g_pMesh2->GetNumVertices(), g_pMesh2->GetFVF(), &mesh2center, mesh2rad); g_pMesh2->UnlockVertexBuffer(); mesh2data is an unsigned char, mesh2center is the location of the object used to move it around, mesh2rad is the radius of the circle, already set at the start of the app. The code for detecting collison is as follows: relpos = mesh1center - mesh2center; distance = ((relpos.x*relpos.x) + (relpos.y*relpos.y) + (relpos.z*relpos.z)); mindistance = mesh1rad + mesh2rad; if(distance <= (mindistance * mindistance)) { //message box, then quit //MessageBox(main_window_handle, "collision occured", "collision occurred", MB_OK); //PostMessage(main_window_handle, WM_CLOSE, 0,0); } relpos is a vector, distance and mindistance are floats. I am hoping that someone out there has seen this kind of thing before, and point me in the right direction. Thanks a lot. "Sweetie! Get Mommy's bazooka!" [edited by - Mister Stewart on April 20, 2002 8:46:35 AM]

##### Share on other sites
try

  distance = fabs(distance);

since a negative distance would cause your app to report collisions even if there is none.

##### Share on other sites
quote:
Original post by Mister Stewart

The code for detecting collison is as follows:

relpos = mesh1center - mesh2center;
distance = ((relpos.x*relpos.x) + (relpos.y*relpos.y) + (relpos.z*relpos.z));
if(distance <= (mindistance * mindistance))
{
//message box, then quit
//MessageBox(main_window_handle, "collision occured", "collision occurred", MB_OK);
//PostMessage(main_window_handle, WM_CLOSE, 0,0);

}

Greetings,

Your math checks out just fine. The way that you are calculating it, distance will definately be a positive number; a sum of squares is always positive. So, my guess would be that your problem is in the calculation of meshNcenter and meshNrad; N\in{1,2}.

Unfortunately, I don't know directX at all so I can't verify whether the other part of the code is correct. I would suggest printing out the vectors meshNcenter, and the values of meshNrad to verify that they agree with what you know the values to be. Or, at least setting a breakpoint at the "if" to inspect said values. My hunch is that something's not getting set correctly.

Alternately, you could easily code an inefficient function which calculates the center and radius for you. The center is the midpoint of the bounding box of the mesh; ie: calculate max & min values for x, y, and z of all the vertices in the mesh then take the average of each min/max pair as the center. Then, the radius of the bounding sphere is the max{||v-center||:v a vertex of the mesh} (that is, calculate the distance of each vertex from the center, and take the maximum; taking the max over the squares of the distance won't work if you have any distances less than 1, as the square of a value less than 1 is less than the value being squared).

I hope I've given you some leads to chase. Good luck!

-Eraslin

[edited by - Eraslin on April 20, 2002 10:33:38 PM]

##### Share on other sites
Thanks all - I got it working. I found a bit of code that was pretty much the same as the one I was using, with slight differences in the radius and distance calculations.

Now I gotta spend some time reading over it and understanding it, then I''m gonna try my hand at understanding mesh animation.

Thanks again for the replies.

"Sweetie! Get Mommy''s bazooka!

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011522
• ### Who's Online (See full list)

There are no registered users currently online

×