Rigid body starting position reset to origin sometimes

Post Reply
chisser98
Posts: 5
Joined: Wed Apr 19, 2017 2:08 am

Rigid body starting position reset to origin sometimes

Post by chisser98 »

Hi,

I've got a basic physics simulation running, and it seemed to be working well, other than I noticed that most of my rigid bodies were starting off at position (0, 0, 0) instead of the position I set, which was (5, 0, 0). I verified after I added the rigid body ( via a call to `motionState->getWorldTransform().getOrigin();`) That the initial position is in fact correct.

Sometimes, however, the rigid bodies would actually start at the correct position I set. I eventually started to see that when I ran the physics engine like this:

Code: Select all

...
float32 dt = 1.0f / 1000.0f;
void Scene::tick(const float32 elapsedTime)
{
        dynamicsWorld_->stepSimulation(dt, 10);
}
...
All of my entities started to show up in the correct position.

However, if I tick the physics simulation in a way that tries to reflect the actual elapsed time, Like so:

Code: Select all

float32 dt = 1.0f / 1000.0f;
float32 accumulator = 0.0;
void Scene::tick(const float32 elapsedTime)
{
	accumulator += elapsedTime;
	
	while ( accumulator >= dt )
    {
        dynamicsWorld_->stepSimulation(dt, 10);
        accumulator -= dt;
    }
}
Most of the entities would spawn At the origin (i.e. not at the correct position).

In both scenarios, the dynamics world is getting the exact same Delta Time value, it's just that in one case sometimes it doesn't get called if enough time has elapsed.

Does this make sense to anyone? I'm really not sure what I'm doing wrong.
chisser98
Posts: 5
Joined: Wed Apr 19, 2017 2:08 am

Re: Rigid body starting position reset to origin sometimes

Post by chisser98 »

Well, it turns out I was creating my rigid bodies at (0, 0, 0), and then trying to reposition them by changing the world transform. If I set the initial position of the rigid body when I create it to be (5, 0, 0), everything works as expected.

I did some googling around how to move rigid bodies, and it seems there's not a definitive solution on how this is done? I've read some places that say you can't really do it and you need to remove and re-add the rigid body, whereas some say you can set the world transforms (As I've done).

Is there a definitive answer on how I can move a rigid body ( without applying force to it)?
Post Reply