Skip to main content
2 of 2
Added section on how to SetTransform in response to an event, rather than on every step.
Anko
  • 13.4k
  • 10
  • 55
  • 82

Call SetTransform before or after your World::step call. Make sure it's not inside a Box2D callback, as those happen inside the step, during which the World is locked.


Further details:

The isLocked() == false assert implies you're attempting to directly change something in the Box2D World during a simulation step. This could break the simulation, so Box2D guards against it. SetTransform directly changes the body's position, so it is disallowed when locked.

ApplyLinearImpulse works even when locked, because all it does is register a new impulse that should act on the body, which does not directly change anything. The impulse is only applied on the next world step.


If you want the SetTransform to in response to some event (not every step), you'll need to introduce some additional state. For example, a bool shouldResetBallPosition that the appropriate callback sets to true. You can then then check for that inside your update method, with something like—

if (shouldResetBallPosition) {
    b->setTransform( ... );
    ...
    shouldResetBallPosition = false;
}
Anko
  • 13.4k
  • 10
  • 55
  • 82