Problem with deleting and creating Bodies

Christian

21-11-2008 18:35:49

Hello,

i want to change the physic values of bodies like mass and later materialpair´s values with lua while the program is running. To do that, I wanted to delete the bodies and create new one´s because I want to reuse the Bodycreation Method instead of writing something new.

To trigger the loading of the new physic values I use the keyboard (OIS buffered). The strange thing now is, that the Program sometimes crashes, sometimes instantly, sometimes after changing the physic values very often.

The error is then also not always at the same position in the code. Because of that I can´t realy track, from where the error is coming from and I hope you can tell me after a look at how I do the creation and deletion of the bodies.


This is where the initialisation of the new values is started:

bool CamKeyListener::keyReleased(const OIS::KeyEvent &e)
{
switch (e.key)
{
...

case OIS::KC_P:
{
luaSystem::ManagerLua::getSingleton()->loadPhysicConfig();
}
break;



The Method loadPhysicConfig() gathers the values and then calls initPlayerPhysic(...) to delete the old player bodies and create new ones

void ManagerPhysic::initPlayerPhysic(float playerMass)
{
Player * p;
OgreNewt::Body * playerBody;
Ogre::Vector3 currentPosition;

//mass is used in the gravityandmovement Callback via getPlayerMass()
this->playerMass = playerMass; //playerMass is a global member




//I read all the players out of a vector.
//For simplicity I have a working Player-Object from here on
p = getAPlayer();

//get Playerbody
playerBody = p->getPhysicBody();

//get current position
currentPosition = p->getPosition();

//remove Things that could make it crash
playerBody->setUserData(NULL);
playerBody->removeForceAndTorqueCallback();
playerBody->removeTransformCallback();

//Delete the Body
delete playerBody;

//create a new One
playerBody = createPlayerPhysics(p->getGraphicObject(), currentPosition);

//set Userdata
playerBody->setUserData(p);
}



And this is how I create Bodies for my Players


OgreNewt::Body * ManagerPhysic::createPlayerPhysics(Ogre::Entity * ent, Ogre::Vector3 position)
{

//Get bounding box for collision shape
Ogre::AxisAlignedBox boundingBox = ent->getWorldBoundingBox(true);
Ogre::Vector3 boundingBoxSize = boundingBox.getSize();

//create Collision Shape
OgreNewt::Collision * collisionShape
= new OgreNewt::CollisionPrimitives::Box(newtonWorld, boundingBoxSize);

//create the body
OgreNewt::Body * playerBody = new OgreNewt::Body(newtonWorld, collisionShape, PLAYER);
delete collisionShape;

//connect Body with Entity
playerBody->attachToNode(ent->getParentSceneNode());

//Set position
playerBody->setPositionOrientation(position, Ogre::Quaternion::IDENTITY);

//Material ID
playerBody->setMaterialGroupID(materialPlayer);

//compute inertia
Ogre::Real mass;
Ogre::Vector3 inertia;

mass = playerMass; //playerMass is a global member
inertia = OgreNewt::MomentOfInertia::CalcBoxSolid(mass, boundingBoxSize);

//set Matrix
playerBody->setMassMatrix(mass, inertia);

//deactivate Freeze
playerBody->setAutoFreeze(0);

//activate Gravity and movement
playerBody->setCustomForceAndTorqueCallback(playerMovementCallback);

return playerBody;
}



Any help or advice is appreciated. I realy like to know where this behaviour is coming from and what I am doing wrong. If some more is needed just tell me...

Christian

22-11-2008 18:23:07

ok, I have solved it.
I´m new to c++ and I mixed something up about pointers and made a mistake concerning basic c++ that had nothing to do with newton. I just forgot to tell the player about the new Body.