Skip to main content
2 of 2
More specific title. Corrected grammar and proofread. Reformatted example code to make it easier to read.
Anko
  • 13.4k
  • 10
  • 55
  • 82

In Box2D, how can I SetTransform of a body without getting an IsLocked assertion error?

I've created a bullet body and stored a reference to it in herobody:

bDef.type = b2_dynamicBody;
bDef.position.Set(herobody->GetPosition().x, herobody->GetPosition().y);
b = world->CreateBody(&bDef);
b2FixtureDef fixturebDef;
b2CircleShape circlebShape;
circlebShape.m_radius = 0.05;
fixturebDef.shape = &circlebShape;
b->CreateFixture(&fixturebDef);

I'm trying to change the current position of the bullet. I tried to use SetTransform but I get an error Assertion failed: (IsLocked() == false) during world->step in my update method.

// This doesn't work. Causes the above assertion.
/*
b->SetTransform(
  b2Vec2(
    herobody->GetPosition().x,
    herobody->GetPosition().y
  ),
  0
);
*/

// This works, but doesn't set position directly.
b->ApplyLinearImpulse(b2Vec2(5, 0), b->GetWorldCenter(),true);

How do I do this correctly?

dex3
  • 23
  • 4