Resetting world to initial state

KennyUK

16-04-2008 16:29:14

In my platform game, I need to reset all active bodies to an initial state when my character dies, so they can try again. I can save the position and orientation of all active bodies to the ogre scenenode when the level is loaded and when my character dies I call the following code on all active bodies:

for(a_it = active_objects.begin(); a_it != active_objects.end(); a_it++)
{
OgreNewt::Body *bod = (*a_it);
Ogre::Vector3 init_pos = bod->getOgreNode()->getInitialPosition();
Ogre::Quaternion init_orient = bod->getOgreNode()->getInitialOrientation();

bod->setVelocity(Ogre::Vector3::ZERO);
bod->setOmega(Ogre::Vector3::ZERO);
bod->setPositionOrientation(init_pos, init_orient);
}

I tested it using a stack of boxes, knocking them over in the game and then reseting the level, and it appears to work most of the time. However, sometimes after the level has been reset when I knock the boxes over they can get stuck in midair, rather than falling. Is there any more physics state that I have to reset? I have tried doing setAutoFreeze(0) for all these objects but it still seems to happen.

Any ideas?

KennyUK

16-04-2008 16:49:28

Answered my own question. I just added an unFreeze to the reset routine:

for(a_it = active_objects.begin(); a_it != active_objects.end(); a_it++)
{
OgreNewt::Body *bod = (*a_it);
Ogre::Vector3 init_pos = bod->getOgreNode()->getInitialPosition();
Ogre::Quaternion init_orient = bod->getOgreNode()->getInitialOrientation();

bod->setVelocity(Ogre::Vector3::ZERO);
bod->setOmega(Ogre::Vector3::ZERO);
bod->setPositionOrientation(init_pos, init_orient);
bod->unFreeze();
}


Seems that even if autoFreeze was off my objects were frozen.

albino

16-04-2008 18:33:50

just wondering if you could just
delete the world, and then create it again

pra

16-04-2008 22:14:33

just wondering if you could just
delete the world, and then create it again

that should be easier, but if he is able to restore all the bodies to their original states, it should be much faster

btw, SceneNodes do save their initial position and orientation? thanks, haven't known that^^

KennyUK

17-04-2008 01:31:06


btw, SceneNodes do save their initial position and orientation? thanks, haven't known that^^

You have to call scene_node->setInitialState() in order to save the initial position/orientation/... after you create your node.