• 10
• 9
• 13
• 10
• 18

# Yet another collision question

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

## 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?
{
// Then reverse the vertical direction.
DirectionY = -1;
// And change the Horisontal speed depending on where the ball hit the paddle.

// PaddleHit has a value between -(Paddle.Width / 2) og Paddle.Width / 2 where 0 is the middle.
{
// <-
DirectionX = -1;
}
else
{
// ->
DirectionX = 1;
}

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



##### 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 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 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...