Kim2
27-09-2008 12:14:08
I can't work out why when I try to delete my enemy objects it crashes my program.
It seems like the program is still trying to call the transform callback even though I delete the body and the node it is attached to. Please somebody help me!
Here is how I make the object and then try to delete it immediately afterwards (as people on the ogre forums said to do) as a test:
Any attempt to delete the body crashes the program and similar for trying to delete the node (I guess because this automatically means the body is deleted?). As I mentioned, the last call looking at the debug info is to the transform callback.
This probably isn't necessary but just in case, the code for the Enemy:
Thanks a lot for any help or advice!
It seems like the program is still trying to call the transform callback even though I delete the body and the node it is attached to. Please somebody help me!
Here is how I make the object and then try to delete it immediately afterwards (as people on the ogre forums said to do) as a test:
enemy = new Enemy(mSceneMgr, m_World, Vector3((event[1] * 2-125),20.0,0.0),Quaternion::IDENTITY, counter, 100);
Enemies.push_back(enemy);
counter++;
for (int i; i < enemy->Enemynode->numAttachedObjects();++i){
mSceneMgr->destroyMovableObject( enemy->Enemynode->getAttachedObject(i) );
}
delete enemy->bod;
enemy->Enemynode->removeAndDestroyAllChildren(); //Remove All Child Nodes
mSceneMgr->destroySceneNode(enemy->Enemynode->getName()); //Remove This Node
Any attempt to delete the body crashes the program and similar for trying to delete the node (I guess because this automatically means the body is deleted?). As I mentioned, the last call looking at the debug info is to the transform callback.
This probably isn't necessary but just in case, the code for the Enemy:
#include "Enemy.h"
#include "include/MaterialsList.h"
void Enemy::ApplyForceAndTorqueEvent (OgreNewt::Body* body)
{
float mass;
float Ixx;
float Iyy;
float Izz;
Vector3 force;
Vector3 torque;
force[0] = 0.0;
force[1] = 0.0;
force[2] = 2000.0;
torque[0] = 0.0;
torque[1] = 0.0;
torque[2] = 0.0;
body->addForce(force);
}
// Default constructor
Enemy::Enemy(Ogre::SceneManager* mgr, OgreNewt::World* world, Ogre::Vector3 Position, Ogre::Quaternion Orientation, int counter, float scale)
{
//statePointer = &state;
mSceneManager = mgr;
MaterialsList* test = new MaterialsList(world);
pos = Position;
orient = Orientation;
Ogre::Real mass = 100;
// calculate the inertia based on box formula and mass
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mass, Ogre::Vector3(3.5,2.5,1.2));
EnemyEnt = mgr->createEntity( "Enemy"+Ogre::StringConverter::toString(counter), "enemyS1.mesh" );
Enemynode = mgr->getRootSceneNode()->createChildSceneNode("Enemy"+Ogre::StringConverter::toString(counter));
Enemynode->attachObject( EnemyEnt );
Enemynode->setPosition(Vector3(Position.x, Position.y, Position.z));
Enemynode->setScale(scale/70, scale/70, scale/70);
OgreNewt::ConvexCollision* col = new OgreNewt::CollisionPrimitives::ConvexHull(world,Enemynode);
bod = new OgreNewt::Body( world, col );
delete col;
bod->attachToNode( Enemynode );
bod->setMassMatrix( mass, inertia );
bod->setCenterOfMass(Ogre::Vector3(0,0,0));
bod->setCustomForceAndTorqueCallback<Enemy>( &Enemy::ApplyForceAndTorqueEvent, this );
bod->setAutoFreeze(1);
bod->setPositionOrientation(Vector3(Position.x, Position.y, Position.z),Orientation);
}
Vector3 Enemy::GetPos()
{
return Vector3(pos.x, pos.y-3, pos.z);
}
Quaternion Enemy::GetOrient()
{
return orient;
}
Thanks a lot for any help or advice!