I want to create mutually rotating gears for a puzzle game.

I looked at a lot of material, but didn't understand how to make a RigidBody2D stay in one place correctly and interact with each other.

Here is the code I use:

extends RigidBody2D
var angle = 1
signal clicked
var held = false
var c 
var vec 
var speed:float = 100
var angele:float = 100
func _input_event(viewport, event, shape_idx):
    if event is InputEventScreenTouch:
        if event.pressed:
            emit_signal("clicked", self)
    if event is InputEventScreenTouch:
        if !event.pressed:

func _physics_process(delta:float)->void:
    global_transform.interpolate_with(transform.rotated(rotation), 10)
    if c == true and held:
        global_transform.origin = get_global_mouse_position()
        vec = Vector2(get_position())
    if c == false and vec != null:
        position = vec
    #rotation = deg2rad(angle)

func _integrate_forces(state):
    #global_transform.interpolate_with(transform.rotated(rotation), 10)
    transform = transform.interpolate_with(transform.rotated(rotation), rotation)
    #contact_monitor = true
func pickup():
    if held:
    mode = RigidBody2D.MODE_STATIC
    held = true
func drop(impulse=Vector2.ZERO):
    if held:
        mode = RigidBody2D.MODE_RIGID
        held = false

1 Answer 1


how to make a RigidBody2D stay in one place

From a design approach, you would make an StaticBody2D (or some other body) and a PinJoint2D with your RigidBody2D.

From a programmer approach, you would write state.transform in _integrate_forces (which is also the most common way to do a teleport). For reference, the state parameter you get in _integrate_forces is of type Physics2DDirectBodyState.

And if you rather use a lower level API, you can write the transform of the RigidBody2D at any time with Physics2DServer.body_set_state, which will be understood as a teleport (Godot will not solve the collision as if the RigidBody2D moved from the prior transform to the new one). For example:


And neither of the approaches described above would require changing the mode of the RigidBody2D.

  • \$\begingroup\$ Thank you very much, I didn't know about the current tool like PinJoint2D then it changes everything and the understanding becomes clearer. I will try following your advice. \$\endgroup\$ Apr 26 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .