When a RigidBody2D bounces off another surface, how can I make the sprite face in the direction of the bounce?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jerryjrowe

I have a player that aims and shoots bullets.
The bullets are an instantiated node of type RigidBody2D.
When I instantiate the bullets, I have no trouble rotating them to the correct appearance in any direction.

When the bullets hit anything else (players, walls, and so on), they bounce properly, as per the physics_material_override.bounce property, since I set it to 1.

But when the bounce occurs, the bullets still faces the same direction. A fireball fired with a trajectory of (1,1) will still be facing that direction even after reflecting off a wall.

I would like to rotate the whole object to the correct orientation, but do not see how. I want to just get the built-in vector2.bounce() function to work, but I don’t know how to get collision data from a RigidBody2D. (All sources I find assume you’re using a KinematicBody2D object with move_and_collide, and not a RigidBody2D object with integrate_forces, which is what I am using.)

Halp? :slight_smile:

:bust_in_silhouette: Reply From: Inces

Bullets have already bounced, so You don’t need bounce vector , but current linear_velocity vector. So no need to grab collision data, just use the collision moment to set up bullet facing to its velocity. You can use short yield to make sure physics engine reads correct vector right after collision

This is exactly what I was hoping to hear. :slight_smile:

I also foolishly tried to just change the sprite, and had a momentary problem.
Instead, I just rotated the whole bullet (which is what I wanted to do anyway), and it was perfect. My code, for those who might want it, was this:

self.rotation = linear_velocity.normalized().angle()

jerryjrowe | 2022-09-06 18:27