# How do you make an object bounce based on it's angle?

v4.2.1

### Question

I am coding a little physics based pong game and I am trying to figure out how to make the paddle move based on the angle it bounces at.

So here is my code for the bouncing between my CharacterBody2D and my floor (which is a StaticBody2D)

``````func bouncing():
move_and_slide()
if get_slide_collision_count() >= 1:
var collision = get_slide_collision(0)
if collision != null:
velocity = velocity.bounce(collision.get_normal())
``````

This works fine and bounces how I want to too but I havenâ€™t been able to figure out how to make it bounce in the direction based on its angle. At first I tried:

``````func bouncing():
move_and_slide()
if get_slide_collision_count() >= 1:
var collision = get_slide_collision(0)
if collision != null:
velocity = velocity.bounce(collision.get_normal())
velocity.x += rotation_degress
``````

but that made the movement all funky and it was moving in mid air (Iâ€™m assuming this is because the â€śbouncingâ€ť function is called in the â€ś_processâ€ť function). I basically want to just simulate what would happen if it hit the ground at different angles but I havenâ€™t found anything online so far. Iâ€™m sure Iâ€™m missing something simple and I am hoping someone more experienced here can enlighten me.

1 Like

Am no math boffin. Suspect that you canâ€™t add rotation to velocity if you expect to rotate the ball. Try the various rotation methods instead.

The code isnâ€™t that readable but I can see some mistakes:

velocity.x += rotation_degress

here the overall velocity would be higher than normal

just get the x,y velocity as a Vector2 named vector and rotate it using this alg:

var x = vector.x * cos(angle) - vector.y * sin(angle)
var y = vector.x * sin(angle) + vector.y * cos(angle)
return Vector2(x,y)