Java Libgdx - Box2d setTransform multiple bodies with joints

1.1k Views Asked by At

I have a group of body, attached with some joints (wheel-joint, prismatic-joint, revolute-joint)

I'm trying to move that group of body to a position with the method setTransform

I decided to move only one body to the position, and others bodies should follow because of joints.

I'm getting a weird result, the bodies start to roll, move to nowhere, a real weird result, here is some image to explain :

enter image description here

How to move multiple bodies attached with joints, to a position using setTransform ?

Note : there is no obstacle thru the object, from point A to point B (moving)

1

There are 1 best solutions below

4
On BEST ANSWER

From a box2d setTransform() reference:

Set the position of the body's origin and rotation. This breaks any contacts and wakes the other bodies. Manipulating a body's transform may cause non-physical behavior.

I think that the problem is just the mechanism you are trying to use to move body and the setTransform is not the right way.

Instead of this consider using

void com.badlogic.gdx.physics.box2d.Body.setLinearVelocity(Vector2 v)

you can calculate v as a subtraction of end point and start point of the body. You should handle stopping body (by zeroing its velocity) when it will reach the target.

Please notice that setLinearVelocity does not depend on you gravity


Second solution is just to setTransform to all bodies in this "joint group". You can iterate over bodies and move them depends on their start position and the target vector

for(Body body : jointGroup)
{
    body.setTransform(body.getPosition.x - someX, ...)
    ...