Archived

This topic is now archived and is closed to further replies.

Extracting Outer Edge For 2D Bitmap Collision

This topic is 4998 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

Ok.. so im working on pong and my collision sucks. Ive read a bunch of previous posts about pong collision and basic 2d bitmap collision. All the algorithms seem basically the same, use bounding boxes and check to see if a point in either box is in another, if so , then you have colliderated. In pong this exact method doesnt work. The most noticeable and ugly error is if the ball contacts the top edge of your paddle, the ball follows down the paddle and then shoots off. So it seems like what id have to do is just test for collision with the outer edges of the paddle? Heres my collision code as it is right now:
// bool Collision ( RECT &pRect1, RECT &pRect2 )
//
// Check to see if two rectangles are overlapping
bool CBall::Collision ( RECT &pRect1, RECT &pRect2 )
{
	// Test if pRect1 top-left corner falls in pRect2
	if ( pRect1.left   > pRect2.left  &&
		 pRect1.left   < pRect2.right &&
		 pRect1.top    > pRect2.top   &&
		 pRect1.bottom < pRect2.bottom )
	{
		return true;
	}

	// Test if pRect2 top-left corner falls in pRect1
	if ( pRect2.left   > pRect1.left  &&
		 pRect2.left   < pRect1.right &&
		 pRect2.top    > pRect1.top   &&
		 pRect2.bottom < pRect1.bottom )
	{
		return true;
	}
	
	// Test if pRect1 bottom-right corner falls in pRect2
	if ( pRect1.right  > pRect2.right &&
		 pRect1.right  < pRect2.left  &&
		 pRect1.top    > pRect2.top   &&
		 pRect1.bottom < pRect2.bottom )
	{
		return true;
	}

	// Test if pRect2 bottom-right corner falls in pRect1
	if ( pRect2.right  > pRect1.right &&
		 pRect2.right  < pRect1.left  &&
		 pRect2.top    > pRect1.top   &&
		 pRect2.bottom < pRect1.bottom )
	{
		return true;
	}

	return false;
}
 
And the calling condition and function
RECT player = { GetPlayerOnSide( x )->GetX(), GetPlayerOnSide( x )->GetY(), GetPlayerOnSide( x )->GetWidth(), GetPlayerOnSide( x )->GetHeight() };
	if ( Collision( ball, player ) )
	{
		mXInc = -mXInc;
		x += mXInc;
	}
 
Lemme know what you guys think!

Share this post


Link to post
Share on other sites
quote:
You could separate the screen update from the engine and have the engine run at a steady fps.


Um.. it is, and the framerate is locked to 30fps... I dont see how this relates to my collsion problem at all though.

Share this post


Link to post
Share on other sites
I think your test conditions are the problem. I think it may be possible to have overlapping boxes (haven''t thouroughly tried it out). The box-box test is really simple:

bool CBall::Collision ( RECT &r1, RECT &r2 )
{
return r1.right >= r2.left &&
r1.left <= r1.right &&
r1.bottom >= r2.top &&
r1.top <= r2.bottom;
}


Assuming: left.x < right.x and top.y < bottom.y

Skizz

Share this post


Link to post
Share on other sites