# Making interacting gears in 2D

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:
c=true
emit_signal("clicked", self)
if event is InputEventScreenTouch:
if !event.pressed:
c=false

func _physics_process(delta:float)->void:
global_transform.get_rotation()
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

func _integrate_forces(state):
#global_transform.get_rotation()
#global_transform.interpolate_with(transform.rotated(rotation), 10)
transform = transform.interpolate_with(transform.rotated(rotation), rotation)
#contact_monitor = true
#print(transform.rotated(rotation))

func pickup():
if held:
return
mode = RigidBody2D.MODE_STATIC
held = true

func drop(impulse=Vector2.ZERO):
if held:
mode = RigidBody2D.MODE_RIGID
held = false


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:

Physics2DServer.body_set_state(
get_rid(),
Physics2DServer.BODY_STATE_TRANSFORM,
Transform2D.IDENTITY.translated(new_position)
)


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

• 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. Apr 26 at 17:41