# Sphere Collision Detection with TriIntersect?

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

## Recommended Posts

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[s].velvecn, &ship[s].velvec); if (D3DXIntersectTri(&p1, &p2, &p3, &ship[s].Position, &ship[s].velvecn, NULL, NULL, &disttointersection)) if (disttointersection < ship[s].rad) { M velvecreflectmatrix; D3DXMatrixRotationAxis(&velvecreflectmatrix, &trinormal, D3DX_PI); D3DXVec3TransformCoord(&ship[s].velvec, &ship[s].velvec, &velvecreflectmatrix); ship[s].velvec.x = -ship[s].velvec.x; ship[s].velvec.y = -ship[s].velvec.y; ship[s].velvec.z = -ship[s].velvec.z; } } scenerymodel[1].Mesh->UnlockVertexBuffer(); scenerymodel[1].Mesh->UnlockIndexBuffer(); //////////////////////////////////////////////////////// } } 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 :) [Edited by - Cuchulainn on July 6, 2004 7:08:53 PM]

##### Share on other sites
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.

• 17
• 10
• 19
• 14
• 19