Collision not working at higher speeds
Hey everyone, I've heard before there is a way to prevent objects from going right through each other when using AABB collision, I'm just not sure what term to look for as far as the right terms or techniques would be called. I want to do it myself.
Could anyone point me in the right direction of an article or site to explain the proper fix for this kind of thing?
Hah I suppose it might help if you saw my code!
Code:
void Physics::Update(std::vector<Renderable_Object*> & Game_Objects)
{
for (unsigned int i = 0; i < Game_Objects.size(); i++)
{
if (!Game_Objects[i]->isPhysical) continue;
for (unsigned int j = i+1; j < Game_Objects.size(); j++)
{
if (!Game_Objects[j]->isPhysical) continue;
if( Game_Objects[i]->AABB &&
Game_Objects[j]->AABB &&
collision2d( Game_Objects[i]->getMin(), Game_Objects[i]->getMax(),
Game_Objects[j]->getMin(), Game_Objects[j]->getMax()))
{
collisionResponse(Game_Objects[i], Game_Objects[j]);
}
else if(Game_Objects[i]->Plane &&
Game_Objects[j]->AABB &&
planeIntersection2d(Game_Objects[i], Game_Objects[j]->getLocation()))
{
collisionResponse(Game_Objects[i], Game_Objects[j]);
}
else if(Game_Objects[j]->Plane &&
Game_Objects[i]->AABB &&
planeIntersection2d(Game_Objects[j], Game_Objects[i]->getLocation()))
{
collisionResponse(Game_Objects[j], Game_Objects[i]);
}
}
}
}
void Physics::collisionResponse(Renderable_Object * collidedObjectA , Renderable_Object * collidedObjectB)
{
if (collidedObjectA->defaultCollisionResponse == 1 && collidedObjectB->defaultCollisionResponse == 1)
{
if (collidedObjectA->Plane)
{
collidedObjectB->getLocation().x = collidedObjectA->getLocation().x;
}
if (collidedObjectB->Plane)
{
collidedObjectA->getLocation().x = collidedObjectB->getLocation().x;
}
}
if (collidedObjectA->defaultCollisionResponse == 0)
{
if (collidedObjectA->getVelocity().x != 0)
{
reverse_x_velocity(collidedObjectA->getVelocity());
}
if (collidedObjectA->getVelocity().y != 0)
{
reverse_y_velocity(collidedObjectA->getVelocity());
}
}
else if (collidedObjectB->defaultCollisionResponse == 0)
{
if (collidedObjectB->getVelocity().x != 0)
{
reverse_x_velocity(collidedObjectB->getVelocity());
}
if (collidedObjectB->getVelocity().y != 0)
{
reverse_y_velocity(collidedObjectB->getVelocity());
}
}
}
bool Physics::collision2d(Vector2 & a_min, Vector2 & a_max, Vector2 & b_min, Vector2 & b_max)
{
return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);
}
bool Physics::planeIntersection2d(Renderable_Object * boundingPlane, Vector2 & objectPosition)
{
Ray line_between_objects(objectPosition,boundingPlane->getLocation());
double DotProduct=line_between_objects.direction.dot(boundingPlane->getLocation().normal());
double l2;
//determine if ray parallel to plane
if ((DotProduct<0)&&(DotProduct>-0))
return false;
l2=(boundingPlane->getLocation().normal().dot(boundingPlane->getLocation()-objectPosition))/DotProduct;
if (l2<-0)
return false;
return true;
}
void Physics::reverse_y_velocity(Vector2 & velocity)
{
velocity.y *= -1;
}
void Physics::reverse_x_velocity(Vector2 & velocity)
{
velocity.x *= -1;
}