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

Godot Version

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)

NOTE: use radinas not degrees
NOTE: Avoid generalised algs like get_normal(), it is not good practise

Thank you for the suggestion and the solution did work somewhat. However, instead of tweaking and debugging my own code, I’m just going to try to use a rigid body. I didn’t realize that rigid bodies already did what I needed as I am still new to programming games. I will definitely be applying your solution though to other, more suitable situations I find myself in.