Connect rotation of 2 RigidBodies2D

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

So, the task seems pretty easy: create 2 RigidBodies2D and connect them, so, they’ll move like tank caterpillars, that is, they should always have the same rotation and have the same distance between each other.

I tried 2 different joints: PinJoint2D and GroveJoint2D with length = 1. But all of them worked until first collision with something else: after collision the started to rotate independently.

The code is pretty simple:

extends Node2D...

func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		left.add_central_force(Vector2.UP.rotated(left.global_rotation) * force)
	if Input.is_action_pressed("ui_left"):
		right.add_central_force(Vector2.UP.rotated(right.global_rotation) * force)

(don’t pay attention to missing underscores, idk why, but the website decided that I want to make text italic instead of putting the underscores)

extends RigidBody2D...

func _physics_process(delta):
	var rotated_velocity = self.linear_velocity.rotated(-self.global_rotation)
	var x_velocity = Vector2(rotated_velocity.x, 0).rotated(self.global_rotation).normalized()
	var y_velocity = Vector2(0, rotated_velocity.y).rotated(self.global_rotation).normalized()
	self.add_central_force(-self.weight * y_friction_coefficient * y_velocity)
	self.add_central_force(-self.weight * x_friction_coefficient * x_velocity)

What am I doing wrong?

:bust_in_silhouette: Reply From: deaton64

Are you creating something that moves like tank caterpillars because that’s what they are?
If so, maybe try KinematicBody where you control the movement.
What it sounds like is happening, is that the object hits another object and reacts in the way that it should. Physics is kicking in and moving the objects. The way a ball would or a spaceship in sapce.

And… If I want to make, let’s say, another tank somewhere and make pushable, then… Will I need to simulate all of the physics in that case?

llem00n | 2022-11-12 14:45

I think so, but the whole object could be made up of multiple sprites. So maybe that’s the way to do it, if it’s a tank and it needs to be rigidbody…

deaton64 | 2022-11-12 15:44