Advertisement Jump to content
Sign in to follow this  
welly_59

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

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

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?

Share this post


Link to post
Share on other sites
Advertisement

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?).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!