I'm trying to set the position of a body with setTransform. But the object doesn't go to the place it should. It gets close to it. Every time I call setTransform with the same parameters, the object gets a little closer to the actual position... I can't figure out what I'm doing wrong... I tried also setting the object type to STATIC but that didn't change anything... What am I doing wrong?
SetTransform not working properly
JavaIs the simulation running at the same time, eg. are you calling Step to apply gravity to everything and move according to their velocities? Does it improve if you also set the velocity of the body to zero?
The simulation is running at the same time, but there is no gravity, or any external forces on the object at the moment I do that. The object also isn't moving, angular and linear velocity are both 0.
So if you use SetTransform to set the position, then you do GetPosition immediately afterwards, the position is not what you set?? That seems unlikely.
Are there any joints involved?
Is the position you set overlapping with some other fixtures that would cause the body position to be corrected when the Step runs?
Are there any joints involved?
Is the position you set overlapping with some other fixtures that would cause the body position to be corrected when the Step runs?
Yeah, thats exactly what's happening... Its crazy... There are no other bodies connected by joints to it, and I'm moving it from an empty space to another empty space, no other fixtures overlapping in either place or anywhere in between.irresistible force wrote:So if you use SetTransform to set the position, then you do GetPosition immediately afterwards, the position is not what you set??
Right now what I'm doing to fix this is putting the SetTransform in a timer loop, and stopping the loop when the current location is close enough to the desired location. That is a very ugly solution though...
Allow me to get this 100% clear... so if you do something like this:
... then the value of pos is not (1, 2) ?
That would be a huge bug that I'm sure many other people would have discovered a long time ago. There must be something else going on.
Code: Select all
body.setTransform( new Vec2(1, 2), 3 );
Vec2 pos = body.getPosition();
... print the value of pos ... ... then the value of pos is not (1, 2) ?
That would be a huge bug that I'm sure many other people would have discovered a long time ago. There must be something else going on.
Yes. ACtually, I'm doing something like a save button. So the user clicks a button, I get the current position and angle of the object and save it to a file. When I load, I do this, basicaly:
auxX,auxY and auxAngle have the position I need. As you can see, I made a loop, because the position I just set is not the position I get immediately after. The actual values in the file, that I got when I saved the position, are 11.315335,7.33776 and 8.332548 for x,y and angle.
Code: Select all
tProjeto = new Timer(10,new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
module.setTransform(new Vec2(auxX,auxY),auxAngle);
if ((moduleManager.getModule().getBody().getPosition().x-auxX<0.01) &&
(module.getBody().getPosition().y-auxY<0.01) &&
(module.getBody().getAngle()-auxAngle<0.01))
tProjeto.stop();
}
});
tProjeto.setInitialDelay(0);
tProjeto.setRepeats(true);
tProjeto.start();
auxX,auxY and auxAngle have the position I need. As you can see, I made a loop, because the position I just set is not the position I get immediately after. The actual values in the file, that I got when I saved the position, are 11.315335,7.33776 and 8.332548 for x,y and angle.
Oh god. I just realized I forgot all about something... The object is a car, and there are actually two wheels attached to it, by weld joints. I completely forgot about them, I'm sorry about that. I deleted them to see if that would help, and yes, the issue seems to be them. But I still need them in there...
I believe I have fixed the problem. I just set the body type of both wheels (which is also something I forgot to do before. Kill me now.) and now the position is been set perfectly.
Sorry for wasting your time... I should have payed more attention, I don't know how that detail slipped my mind like that....
Sorry for wasting your time... I should have payed more attention, I don't know how that detail slipped my mind like that....
Original thread: https://box2d.org/forum/viewtopic.php?t=8951