how to rotate a rigidbody to a specific angle?

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

Hi, I know I can set angular_velocity to make a rigidbody to rotate,
but what if I want the body to rotate to a specific angle, like 270 degree?

Thank you.

PS. I am using Godot 3.0

:bust_in_silhouette: Reply From: kidscancode

You have to be careful when setting a rigid body’s physical parameters. If you do it wrong, you’ll be fighting against the physics engine.

To do it correctly, you need to use the Physics2DDirectBodyState which you can access in _integrate_forces(). In this manner you can set the body’s Transform2D to any value you like.

Quick example:

func _integrate_forces(state):
	var xform = state.get_transform().rotated(some_angle)
	state.set_transform(xform)

If you’d like a full explanation, I wrote up the details with some examples here:
http://kidscancode.org/blog/2017/12/godot3_kyn_rigidbody1/

1 Like