1

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)

LeSam
  • 1,235
  • 4
  • 18
  • 38

1 Answers1

1

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, ...)
    ...
m.antkowicz
  • 13,268
  • 18
  • 37
  • I made bodies and joint with a software called Rube, I import them and trying to set there respawn position in my game, that's why I think setTransform is the aproprieted method for my case – LeSam Sep 02 '15 at 04:05
  • then maybe try to apply **setTransform** to all bodies in this "joint-group"? Something like for(Body body : bodiesInJointGroup){ body.setTransform( body.getPosition.x - someX, ... ) } – m.antkowicz Sep 02 '15 at 04:07
  • I think that might be the answer, I will try that as soon as possible and tell you if it works. Write it as an answer in case that works – LeSam Sep 02 '15 at 04:20
  • it works you can write this as answer I will valid it – LeSam Sep 03 '15 at 17:31