Help implementing collision detection

ShinHoshi

Newcomer
Sorry if this is not the place for this.

I am making a game for one of my University subjects and I am having problems with the collision detection. Right now I use a simple but fast sphere-based collision detection system. The problem of it is that it detects collisions a lot of time before "the real collision" happens. I am actually looking for a box-based system.

That's the one I am currently using. I found it on gamedev. Very simple to understand and implement. The problem is that is too unefficient.

bool bSphereTest(Object3D* obj1, Object3D* obj2 )
{
point relPos = obj1->GetPos() - obj2->GetPos(); //Calculem el vector entre els 2
float dist = relPos.x * relPos.x + relPos.y * relPos.y + relPos.z * relPos.z; //Distància que els separa
float minDist = obj1->GetRadi() + obj2->GetRadi(); //La suma de les 2 distàncies
return dist <= minDist * minDist; //Si això es compleix vol dir que s'estan tocant ! retornem 1
}

Can you give me the scopes of a better algorythm ???
 
They are tanks in a 3D field. Only the x and z coordinates vary since the y is invariable. But I plan on put other objects such as houses, trees...and I would like to use the same collision detection system.
 
does the turret stick out beyond the body of the tank? If it does, do you have to keep it that way?

if the turret does not extend beyond the body of the tank then a simple bounding box should do the trick.

later,
epic
 
I've tried to use a Bounding Box but it doesn't seem to work. First when loading the 3D model from the .ASE file, I save the greatest x, y, z coordinate in order to make the box.

Then, every time I want to check a colision I create 4 points for the object to test (if I had movement in the Y-AXIS they would be 8) and for each point I test if it colides with the box of the other object.


//Hem de mirar en quins 3 eixos es toquen
bool bBoxTest(Object3D* obj1, Object3D* obj2 )
{
float overlapx, overlapy,overlapz;
obj1->calcular_BB();

for(int i=0;i<4;i++)
{
if(obj2->GetPos().x+obj2->caixa_contenidora.xmax<=obj1->test_points.x)
return false;
if(obj2->GetPos().x-obj2->caixa_contenidora.xmax>=obj1->test_points.x)
return false;
if(obj2->GetPos().z+obj2->caixa_contenidora.zmax<=obj1->test_points.z)
return false;
if(obj2->GetPos().z-obj2->caixa_contenidora.zmax>=obj1->test_points.z)
return false;
}

return true;
}


However, it doesn't seem to work.
 
I've found the problem. Probably nobody cares but I solved it:

quins 3 eixos es toquen
bool bBoxTest(Object3D* obj1, Object3D* obj2 )
{
// float overlapx, overlapy,overlapz;
bool sit=false;
obj1->calcular_BB();


for(int i=0;i<4;i++)
{
sit=true;

if(obj2->GetPos().x+obj2->caixa_contenidora.xmax<=obj1->test_points.x)
sit= false;
if(obj2->GetPos().x-obj2->caixa_contenidora.xmax>=obj1->test_points.x)
sit= false;
if(obj2->GetPos().z+obj2->caixa_contenidora.zmax<=obj1->test_points.z)
sit= false;
if(obj2->GetPos().z-obj2->caixa_contenidora.zmax>=obj1->test_points.z)
sit= false;

if(sit)
return true;
}

return false;
}
 
Back
Top