Collision Detecting

Does anybody know how to do basic collision detecting? Just detection for planes and spheres. No complex objects.

goto glvelocity.gamedev.net, under code archive, I think I remember classes for planes & spheres and other stuff.

I think you can find some usefull information
on http://www.spinningkids.org/umine/index2.html

Spheres are easy. If the distance between spheres are less than their combined radius they collide. I don’t remember planes though.

Say you have two spheres, A and B, with components (x, y, z, R), where x, y and z gives its location and R its radius. Then they collide if |(Ax - Bx, Ay - By, Az - Bz)| < AR + BR.

If you have infinite planes, it’s easy. If they are not parallel, they collide.

Bounded planes (polygons) are tougher. Maybe you could do it by seeing if any of the edges from either polygon pass through the other polygon. That simplifies it to multiple line/plane intersections. Then you just need to see if any of the intersection points lie inside either polygon.

j

Thanks. The stuff you told me is for spheres to spheres right? Is it the same for spheres to planes?

For Sphere to Plane it is simple(i think so):
just dotproduct of the normal (of the plane) with the center of the sphere so you will have the distance of the sphere to the origin. the difference between this distance and the distance Origin<->Plane must be smaller than the radius then a collision occured.
To get the distance of the plane to the origin, you simply dotproduct a point of the plane with the normal…
cn - vn <=> (c-v)*n < r
with c = center of sphere
v = vertex of plane
n = normal of plane
r = radius of sphere

Well i think the simplest way to do Collision detection is the "Hardboxes system.

This means all objects are givin a box through which you cant go.
now you use arrays and loops to check all colisions in the game.

int x; //player x
int y; // player y
int playerwidth; //playerwidth
int playerheight; // playerheight

int maxobjects = 2; //number of objects
int objectx[maxobjects];
int objecty[maxobjects];
int objectwidth[maxobjects];
int objectheight[maxobjects];

for(int i = 1; i < maxobjects+1; i++)
{
if(x+playerwidth > objectx[i] && x < objectx[i]+objectwidth[i])
{
if(y+playerheight > objecty[i] && y < objecty[i]+objectheight[i])
{
//weve collided!!!
}
}
}

this is for 2d but it can be altered for 3d

Sorry Starnut,
you are right, for understanding it is easier to check a simple ‘box clip’,
but the box system has some serious drawbacks:

-for 3D you have to check 8 corners, not 4 for the Sphere/Plane check a vector subtract and a vector multiplication will give you the distance

-if you are rotating objects, you have to construct your box every frame new(else you should use non-axis-aligned bounding boxes, another story)

I think the sphere/plane collision (which has drawbacks, too, can use for first step collision detection, then you have to use more advanced methods (eg BSP/Octree like in hidden Surface Removal)

To create an unaligned bounding box, you only need 8 vector-matrix multiplications. Doesn’t cost too much.

PeterParker. That works if the frame rate is hight and the objects are large. But what happens if the object has enough velocity to be in one frame in front of the plane and the next frame completely behind that plane. For any object that moves faster than twice it’s size a frame, you should perform a line-plane intersection test. On the other hand, the frame rate is not guaranteed to be constant, so you should do that anyway.