Sphere Collision Detection with TriIntersect?

Started by
0 comments, last by SoaringTortoise 19 years, 10 months ago
Hi. I've just put in a quick form of collision detection to my game - basically - i check if the velocity vector of my ship intersects a triangle of the world mesh - if it does - i calculate the normal and make it bounce away from the tringle at the same angle it hit it - easy - but the problem is i want sphere collision detection because otherwise parts of my ship go outside the world (not good :)). Before i get into the messy business of intersections with planes and trinagles and stuff tring to find a sphere collision - i was wondering did any of you see a handier way i could do it with the triintersect function - maybe a more stragically picked ray point - or can it only be done the long hard way. Here's my code for the slightly screwed up collision detection. void Ship_World_CollisionDetection() { for (int s = 0; s < NumberOfShips; s++) { my_vertex *vertices; WORD *indices; //////////////////////////////////////////////////////// scenerymodel[1].Mesh->LockVertexBuffer(0,(VOID**)&vertices); scenerymodel[1].Mesh->LockIndexBuffer(0,(VOID**)&indices); for (int v = 0; v < scenerymodel[1].Mesh->GetNumFaces() * 3; v = v + 3) { V p1, p2, p3; p1.x = vertices[indices[v]].x; p1.y = vertices[indices[v]].y; p1.z = vertices[indices[v]].z; p2.x = vertices[indices[v + 1]].x; p2.y = vertices[indices[v + 1]].y; p2.z = vertices[indices[v + 1]].z; p3.x = vertices[indices[v + 2]].x; p3.y = vertices[indices[v + 2]].y; p3.z = vertices[indices[v + 2]].z; V p1_p2, p1_p3, trinormal; p1_p2 = V(p2) - V(p1); p1_p3 = V(p3) - V(p1); D3DXVec3Cross(&trinormal, &p1_p3, &p1_p2); D3DXVec3Normalize(&trinormal, &trinormal); float disttointersection; D3DXVec3Normalize(&ship.velvecn, &ship.velvec); if (D3DXIntersectTri(&p1, &p2, &p3, &ship.Position, &ship.velvecn, NULL, NULL, &disttointersection)) if (disttointersection &lt; ship.rad) { M velvecreflectmatrix; D3DXMatrixRotationAxis(&velvecreflectmatrix, &trinormal, D3DX_PI); D3DXVec3TransformCoord(&ship.velvec, &ship.velvec, &velvecreflectmatrix); ship.velvec.x = -ship.velvec.x; ship.velvec.y = -ship.velvec.y; ship.velvec.z = -ship.velvec.z; } } scenerymodel[1].Mesh-&gt;UnlockVertexBuffer(); scenerymodel[1].Mesh-&gt;UnlockIndexBuffer(); //////////////////////////////////////////////////////// } } </font> By the way V is defined as D3DXVECTOR3 and M is defined as D3DMATRIX, velvec is the velocity vector - velvecn is the vectors normal. Any ideas to turn this into a sphere collision test? Any help would be…. helpful :) <!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - Cuchulainn on July 6, 2004 7:08:53 PM]<!–EDIT–></span><!–/EDIT–>
You'll Never Beat The Irish!
Advertisement
Something that's working for me goes a bit like this (I had a problem getting sphere-sphere interesection)

1) Use a bounding-sphere first pass detection (very quick)
2) As well as using the Ship's velocity vector, I create a ray from the center of the ship to the center of the detected object
3) With the ray, i do a simple intersect test with the detected-object's model.

This works accurately enough for me and it's pretty fast. It's not very good with widely irregular shapes (i.e. it works best with objects that approximate spheres, cubes and cylinders, but NOT trees), but I've not found many things that can't use one of those primatives as a refined collision-test.
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...

This topic is closed to new replies.

Advertisement