Log In  

Cart #35201 | 2017-01-08 | Code ▽ | Embed ▽ | No License

I need help - if you try hitting the ball with the sides of the paddle, it gets stuck inside the paddle and bounces around a bunch of times. How can I fix this so that it just bounces off of the side?

Arrow keys for bottom player, ESDF for top player.

P#35202 2017-01-07 20:00 ( Edited 2017-01-08 07:03)

I have never been that good at these type of collisions so there is probably a better solution.

I was initially getting:

It kept picking up speed in this scenario so I thought the speed was the trigger.

I edited the code like so

-----lower pad collision with ball-----
        if (ball.pos.y + ball.size >=
          pad1.pos.y-ball.size+1) and ball.pos.y + ball.size <= pad1.pos.y+ball.size and
          (ball.pos.x - ball.size + ball.spd.x
          >= pad1.pos.x-4 and
          ball.pos.x + ball.spd.x <= pad1.pos.x +
          pad1.w + ball.size + 2)

notice the + ball.spd.x on both calculations against the ball.pos.x

now it does this:

Like I said there is probably a much better way of calculating this but hopefully it's a start!
good luck and happy coding

P#35218 2017-01-08 01:04 ( Edited 2017-01-08 06:04)

Highly recommend Kryzman's tutorial series on YouTube. He's still releasing more but starting with this video (https://www.youtube.com/watch?v=ejDC-aIgVIE) there's about three videos on collision, covering this exact subject.

P#35219 2017-01-08 02:03 ( Edited 2017-01-08 07:03)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 06:11:54 | 0.013s | Q:17