2

I'm making a Breakout clone and having a little trouble with the ball-to-paddle collisions. I have a rectangle represent both the ball and the paddle and when they intersect, the Y vector representing the ball's velocity is negated (as shown below). That all works fine. The problem is when the paddle is moving to the right I want it to nudge the ball a little to the right (as opposed to it just reflecting off normally) and I want the same to happen in the opposite direction is the paddle is moving to the left. I'm not sure how to go about doing this and I've looked all over. Any help would be appreciated. Thanks.

if (paddleRectangle.Intersects(ballRectangle))
{
    ballVelocity.Y *= -1;
    collision.Play(); //a collision sound
}

EDIT: Basically I want to slightly change the angle at which the ball bounces off the paddle based on which direction the paddle is moving. If the paddle is not moving, then the ball will bounce normally (by inverting the Y component of the ball's velocity)

davidsbro
  • 2,761
  • 4
  • 23
  • 33
user2773218
  • 87
  • 1
  • 6

3 Answers3

4

Add the paddle's velocity vector to the paddle's normal vector (this basically bends the normal in the direction the paddle is moving) and normalize the result. Use this as the collision normal for reflection.

Vector2 collisionNormal = Vector2.Normalize(paddleNormal + (paddleVelocity * desiredEffectAmount));
ballVelocity = Vector2.Reflect(ballVelocity, collisionNormal);
Steve H
  • 5,479
  • 4
  • 20
  • 26
1

i did some grinding in my head... and here are results. to achieve that you will need, moving direction of paddle, speed of paddle, ball speed, ball direction. and then by some math function calucalte angle and speed of bounce.

i think this image (if bouncing is phisicaly correct) will give you idea how to create this. can't help you with function that will handle this but i would go and try that way as in image.

enter image description here

Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
0

You want a little friction, but probably not real friction.

Try scaling the paddle speed down by some factor and adding it to the ball velocity.

MickLH
  • 416
  • 4
  • 13
  • I don't want to change the ball speed or the paddle speed. I want to slightly change the angle at which the ball bounces off the paddle based on which direction the paddle is moving. Sorry if I was unclear – user2773218 Oct 02 '13 at 20:06
  • 1
    So you want to change the speed of the ball. "Angle" does not exist, it is a concept derived from the ratio of the separate X and Y velocities. – MickLH Oct 02 '13 at 20:23
  • I'm living in the real world where you are storing your bits of data representing X and Y velocity. Yeah they do exist in physical reality, now are you trying to solve your problem or cause one? – MickLH Oct 02 '13 at 23:49
  • @MickLH I'm saying look at the tags − there's XNA 2 times. Most of the time velocity is represented by a `Vector2` in XNA games, and it's not a bad idea, since `Vector2` class has a ton of helpful built-in methods. Steve H posted a nice answer using one such method as an example. Your answer is more of a comment really, but I won't flag it, I'm leaving it to your discretion to decide if you think this is a worthy answer. – user1306322 Oct 03 '13 at 07:34
  • @MickLH I also just noticed you mistook me for the OP of the question. OP has a blue frame around his display name, and all other users don't. – user1306322 Oct 03 '13 at 07:42
  • 1
    Given that this both, is about as simple as you can implement it, AND provides the most satisfying gameplay in my tests, I will leave it here for someone looking for a simple solution and not trying to introduce pseudo physically correct realism into a completely fantasy 2D game at the expense of fun. -- If the OP wants to conserve energy he can still use Normalize to preserve magnitude of the velocity normal, but the fun came from being able to add energy to the ball in my test. – MickLH Oct 03 '13 at 12:02