• Create Account

## Pong - change angle of ball rebound depending on are of paddle hit

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1welly_59  Members

230
Like
0Likes
Like

Posted 29 March 2014 - 04:45 AM

im making a simple pong clone in unity using the 2d tools provided. I have it up and running with the ball x velocity switching when it hits a player, and the y velocity switching when it hits a wall:

void OnCollisionEnter2D(Collision2D collision) {
// Invert the velocity
if (collision.gameObject.name == "Player")
{
if (speed > 0)
{
speed++;
}
else
{
speed--;
}
speed = speed  * -1;
rigidbody2D.velocity = new Vector2 (speed, UpDown);
print ("i hit player");
}


theres a couple of extra lines in there, which increase the ball speed on every hit and also print a debug message onto the console. Any tips on modifying it so that the area of the player paddle that is hit will affect the UpDown(y axis) of the ball?

### #2DiegoSLTS  Members

2113
Like
2Likes
Like

Posted 29 March 2014 - 07:57 AM

I'm guessing that you collision.gameObject is the full object of the Player and that rigidBody2D is the ball, if not I guess you can get a pointer to those objects some how.

With the Y position of the ball and the Y position of the Player and the height of the Player you can see how far is the ball to one of the ends of the Player.

With those values you can make a linear function that returns an angle between [-a , a], and with functions like acos and asin of that angle you can get how much to increment the x and y speed. You can test for different "a" angles to see which value makes more sense (maybe 45 degrees?).

### #3ram64  Members

862
Like
0Likes
Like

Posted 01 April 2014 - 07:22 AM

I've done this in an old Pong clone some time ago. Basically you split the paddle vertically in two sides and when the ball hits you would divide the distance from the ball to the paddle center by half the height of the paddle. You will get a number between [-1, 1] where -1 would be the bottom of the paddle, 0 would be the center, and 1 would be the top.  Then you can simply multiply the Y velocity of the ball with this number and the ball will bounce off at different speeds depending on where it hits the paddle. It's basically the same thing as DiegoSLTS said but it doesn't use any angles.

EDIT:

If the ball and paddle have middle registration points it would be something like this:

MAX_Y_SPEED = 30 // the maximum speed the ball could have on the Y axis
coef = (ball.y - paddle.y) / (paddle.height * 0.5) // this will give you a number between -1 (top) and 1 (bottom)
ball.speedY  = MAX_Y_SPEED * coef

Edited by ram64, 01 April 2014 - 07:30 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.