Sign in to follow this  

Yet another collision question

This topic is 4207 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 creating a simple breakout clone in C# and I need to get some better collision detection. At the moment the ball only respond to hitting the top of the paddle and then bounce off in the oppsite y direction, basicly what I want to archive is make it more sensitive to where on the paddle it hits, for example if it hit's the side of the paddle it should shift the x direction but not the y direction. http://dndesign.dk/seph/box.jpg I'm currently using the following code:
// Has the ball hit the paddle?
if (Position.X + Width >= Paddle.Left && Position.X <= Paddle.Right && Position.Y + Height <= Paddle.Bottom && Position.Y + Height >= Paddle.Top)
{
	// Then reverse the vertical direction.
	DirectionY = -1;
	// And change the Horisontal speed depending on where the ball hit the paddle.
	int PaddleHit = (Paddle.X + Paddle.Width / 2) - (Position.X + Width / 2);
				
	// PaddleHit has a value between -(Paddle.Width / 2) og Paddle.Width / 2 where 0 is the middle.
	if (PaddleHit >= 0)
	{
		// <-
		DirectionX = -1;
	}
	else
	{
		// ->
		DirectionX = 1;
	}

	MovementSpeedX = Math.Abs(PaddleHit / Sensetivity) + 1;
}

Share this post


Link to post
Share on other sites
First of all, you'll have to check the direction the ball is moving. If you don't, your ball will potentially start bouncing back and forth within a paddle. When the ball is moving to the left, just check collisions with the left player. When the ball is moving to the right, check collisions with the right player. When there is a collision, all you have to do is reverse the X direction of the ball. Looking at your code, it should look something like DirectX *= -1; .

Share this post


Link to post
Share on other sites
What I would do is this.

1) Test Sphere to left Line Segment
- If intersection, solve for smallest time it intersected
2) Test Sphere to upper Line Segment
- If intersection, solve for smallest time it intersected
3) If no inersection in step #1 - Test Sphere to right Line Segment
- If intersection, solve for smallest time it intersected.


When you have an intersection, you want to find out where it intersected first. Say it intersected with the top line and left line. Compare the intersection times and the one that is smaller, determine its delta time and move it back in the opposite direction it came from * delta time.

For example:

- Direction the ball was traveling is <5,-5>, normalize this so it now equals Direction<.747,-.747>.
- Current time is 10 seconds, intersection time is 9.5, so the delta time is 0.5 seconds.
- Move the ball back (negate Direction to <-.747, .747>) by its negative direction times delta time.
- Position = CurrentPosition + <-.747, .747> * 0.5

Now you've move the ball to its current intersection point, you just need to reflect it correctly. If it intersected with left or right, negate the x component of its velocity, otherwise negate the y component.

The only hard part is doing a line segment to sphere intersection test

Dave
- If you need anything clarified just ask!

Share this post


Link to post
Share on other sites
I think you got it wrong, I'm not doing a pong game, I'm doing breakout

http://dndesign.dk/seph/breakout.jpg

Obviously I need this for the paddle and the bricks to ensure the ball behaves as expected.

The problem with the hit detection I'm using now is that it only checks if the ball hits the area the paddle is in, not which side, so if it hits it bounces back up.

For the bricks I'll need to know if it hits the side, and since the paddle and the bricks is the same shape I might as well apply the same hit detection to the paddle.

@Ph33r, That looks like the thing I want, now to decipher that page...

Share this post


Link to post
Share on other sites

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