How to remove a rigid body dynamically?

mengzhu

20-02-2010 04:54:32

I didn't find functions on this in Ogrenewt,and after using Newton api to do this,the program crashed :lol: ;So please tell me the correct way,thanks!

PJani

20-02-2010 21:38:08

OgreNewt::Body * bdy = new OgreNewt::Body(....)
...
...
delete bdy; //you just delete body :)

mengzhu

21-02-2010 02:08:01

I found ~Body() does this just after I asked, :oops: ,it includes newton functions; Thank you;
And even at first,the crash is caused by an ogre issue but not newton,it's my miss. :oops:

PJani

21-02-2010 12:21:39

answered from you private message.

I think destroying bodies in contactsProcess and calling object->~body() its bad idea(you are making memory leaks this way. Its the reason why it doesnt crash if you call object->~body() in contactsProcess, its leaving some stuff undeleted).

Destroying bodies in contactProcess its not easy job.
I will explain how to do it.

Make std::queue(in our case named waitqueue --> std::queue<OgreNewt::Body*> waitqueue) in your MaterialPair which handles conveyor belt.(how to make and use std::queue its well explained on http://www.cplusplus.com/reference/stl/queue/)

When you want to delete body in contactProcess.


first you world->lockCriticalSection(because of multithreading) or else you will get migrene.
then push that body in queue.
world->unlockCriticalSection() or else we can only kill our program.
example

if(want to remove body)
{
m_world->lockCriticalSection()

waitqueue.push_back(pointer_to_body);

m_world->unlockCriticalSection()
}



after this is over in some FrameListener of ogre.
do

while(!waitqueue.empty())
{
Ogre::SceneNode* m_scnNode = (Ogre::SceneNode*)(waitqueue.back()->getNode());
Ogre::Entity* m_oent = (Ogre::Entity*)(m_scnNode->getAttachedObject(0));
//deattach entity!
m_oent->detachFromParent();
//destroy Entity;
m_ScnMan->destroyEntity(m_oent);
//Destroy enything that was left meybe attached to that node.
m_scnNode->removeAndDestroyAllChildren();
//Destroy SceneNode
m_ScnMan->destroySceneNode(m_scnNode);

//Delete body
delete waitqueue.back();

//remove our body from queue...
waitqueue.pop_back();
}


and your object should be completely deleted with scenenode.

mengzhu

22-02-2010 00:30:11

Thanks for your enthusiastic help(you even told me how to use queue~).

p.s.,I use Irr before, it's convenient but lacks some "important" things for noobs(e.g. a physx wrapper's character controller still uncompleted for a year,ok, somebody would say,the bullet wrapper is ok now,but Irr's shadowvolume node still has "artifact" bug when character stays in shadows,and Irr hasn't a pssm sample),somebody would say again"why you a noob need so much? " ,,I just can say "because I'm a noob",some noobs like I back to ogre now.It's unpredictable if they'll back to Irr several months, :mrgreen:

Fortunately,I find there 's a typing error in Ogre,"detatchFromParent()" should be "detach..." :lol:

PJani

22-02-2010 05:54:54

Thanks for your enthusiastic help(you even told me how to use queue~).

p.s.,I use Irr before, it's convenient but lacks some "important" things for noobs(e.g. a physx wrapper's character controller still uncompleted for a year,ok, somebody would say,the bullet wrapper is ok now,but Irr's shadowvolume node still has "artifact" bug when character stays in shadows,and Irr hasn't a pssm sample),somebody would say again"why you a noob need so much? " ,,I just can say "because I'm a noob",some noobs like I back to ogre now.It's unpredictable if they'll back to Irr several months, :mrgreen:

Fortunately,I find there 's a typing error in Ogre,"detatchFromParent()" should be "detach..." :lol:


Year and half a go i started to use Irr to, but after 1 week of usage i switched to Ogre and i could not be more happy. :D. That typo bug was fixed in ogre 1.7.