How to freeze RigidBody2D?

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

I’ve made a kind of gate. Check a video: https://youtu.be/idbKdaFdkLM
I used RigidBody and set Custom Integrator true. Then I wrote this code:

extends RigidBody2D

func _integrate_forces(state):
    linear_velocity = Vector2(0, 0)
    pass

In the video you can see a console that outputs Y position of the object and position still changes. My method couldn’t make it stop completely. If I directly change position, object doesn’t revolve at all.

So can I freeze RigidBody on the place without making it Kinematic or Static, because it ruins the main feature of this object

:bust_in_silhouette: Reply From: Thomas Karcher

I can’t tell you why your approach doesn’t work as expected (probably rounding errors in the rotation), but I can tell you there’s a better way to do it - by using a PinJoint2D node:

https://kidscancode.org/godot_recipes/physics/joints_2d/

:bust_in_silhouette: Reply From: scrubswithnosleeves

If you are talking about the Godot icon as a “gate”, then the reason it isn’t working is because it is rotating and thus has no linear_velocity.

But the best way to do this is this:

set_mode(RigidBody2D.MODE_STATIC)

When you want to stop it.

and then do:

set_mode(RigidBody2D.MODE_RIGID)

when you want it to move again.