Sign in to follow this  

What's the best solution for this collision response algorithm?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on my first properly coded 2D game project and have run into a wall regarding basic AABB collision response. The algorithm I'm using now partially works, but doesn't account for velocity yet and I'm not quite sure how to implement it. Another problem with the current method is that objects going into another ones corner will "slip" onto the wrong side of the object due to the speed, like so: The way I have/want to this set up now is
if(objects[x].getMTD(objects[y])!=0)
{
   //code to partially correct object x according to the velocity of both objects
   objects[y].getMTD(objects[x]) //finish correcting the second object
   //process collision etc
}
The way it is now if a fast object and a slow object are moving toward each other the faster object will push the slow object in the direction its moving instead of both of them staying in place. Is the method I'm using plus additional velocity code going to work for this kind of operation or should I consider a different collision algorithm altogether? Here is the code for my getMTD method:
/*PROBLEMS/NON-INLUDED FEATURES: should account for velocity.
if both objects are moving it needs to account for that so they correct
to the right places. if a collision is found by this function it corrects
the object then uses the rhs object to check for a collision with this one.*/

/*Returns a vector of the minimum translation distance. 
NOTE: members right and bottom of CL_Rect are actually being
used as height and width, not as the MAX co-ordinate.*/
Vector2 CollisionBox::getMTD(
	CL_Rect rhsarea, /*area of the AABB to check collision with*/
	Vector2 thisvel, /*velocity of this object*/
	Vector2 rhsvel) /*velocity of the rhs object*/
{
	Vector2 temp(0,0); //the vector to return
	int diff=0; 
	int mtd=0;
	int axis=0; /*0=x axis 1= y axis*/
	int side=0; /*-1=left/down 1=right/up*/

	/*left (this -> rhs)*/
	diff = area.left + area.right - rhsarea.left; /*area is the CL_Rect of this object*/
	if(diff<0)
		return Vector2(0,0);

	mtd = diff;
	axis=0;
	side=-1;

	/*right (rhs <- this)*/
	diff = rhsarea.left + rhsarea.right - area.left;
	if(diff<0)
		return Vector2(0,0);

	if(diff<mtd)
	{
		mtd=diff;
		axis=0;
		side=1;
	}
	
	/*down (this ^ rhs)*/
	diff = area.top + area.bottom - rhsarea.top;
	if(diff<0)
		return Vector2(0,0);

	if(diff<mtd)
	{
		mtd=diff;
		axis=1;
		side=-1;
	}

	/*up (this V rhs)*/
	diff = rhsarea.top + rhsarea.bottom - area.top;
	if(diff<0)
		return Vector2(0,0);

	if(diff<mtd)
	{
		mtd=diff;
		axis=1;
		side=1;
	}

	if(mtd!=0)
		cout << "MTD: " << mtd << endl;

	if(axis==1)
	{
		temp.y=(side*mtd);
	}
	else
	{
		temp.x=(side*mtd);
	}

	return temp;
}

This has been a major roadblock in my game making process for months and I would really appreciate some help on this.

Share this post


Link to post
Share on other sites
There's actually a velocity independent way to handle moving AABB.

Treat one as stationary. velTemp = vel1 - vel2; then do line segment to line segment intersection tests using vertex to vertex + veltemp on object 1 and vertex to vertex - velTemp for object 2. This actually works for all convex and concave shapes and has many optimizations. Dot product checks being the most obvious. This becomes useful if your rectangles are traveling faster than their colliding objects width and doesn't require tedius interpolation techniques.
useful intersection algorithms

Share this post


Link to post
Share on other sites
Use interpolation. If the object is moving further per frame than its smallest cross sectional diameter, break it into seperate steps. Do this for the bounding volume. When an intersection occurs, test the geometry of the object to double check the collision before posting an "OMG dude you crashed" event.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this