crash when key/mouse down

sleepwalker

22-08-2009 11:40:24

Sorry if it maybe not NxOgre problem. I'm working on a project using NxOgre. when I update scene or put mouse/key pressed, the program always crashed.
I debug all day but still cannot get things done. This is my code:
NxBodyMgr.h
#pragma once

#include "NxOgre.h"
#include "NxOgreOGRE3D.h"
#include "NxPhysics.h"

#include <CEGUI/CEGUISystem.h>
#include <CEGUI/CEGUISchemeManager.h>
#include <CEGUIRenderer/OgreCEGUIRenderer.h>

#include "ExampleApplication.h"
#include "NxAppCallback.h"
#include "NxOgreFrameListener.h"

using namespace Ogre;

class NxBodyMgr : public ExampleApplication
{
public:
static NxBodyMgr* getSingletonPtr();
NxBodyMgr();
~NxBodyMgr();

public:
void createScene();
void createFrameListener();

void createNxWorld();
void createObject();
void createNxDog();
void createRoom();

void update();
void updateDogActor();

bool keyPressed(const OIS::KeyEvent &arg);
bool keyReleased(const OIS::KeyEvent &arg);
bool mouseMoved(const OIS::MouseEvent &arg);
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);


protected:
NxOgre::World* mWorld;
NxOgre::Scene* mScene;
OGRE3DRenderSystem* mRenderSystem;

CEGUI::OgreCEGUIRenderer *mGUIRenderer;
CEGUI::System *mGUISystem; // cegui system

OGRE3DBody* mBall;
OGRE3DBody* mBallTwo;
OGRE3DBody* mCube;
OGRE3DBody* mFlyDisk;
AnimationState* mAnimState;
NxOgre::Actor* mDogBox;

Bone* HeadBone;
Bone* LCalfBone;
Bone* RCalfBone;
Bone* LClavicle;
Bone* RClavicle;
Bone* LHandBone;
Bone* RHandBone;
Bone* LFootBone;
Bone* RFootBone;
Bone* ForeHeadBone;

NxOgre::Actor* HeadActor;
NxOgre::Actor* LCalfActor;
NxOgre::Actor* RCalfActor;
NxOgre::Actor* LClavicleActor;
NxOgre::Actor* RClavicleActor;
NxOgre::Actor* LHandActor;
NxOgre::Actor* RHandActor;
NxOgre::Actor* LFootActor;
NxOgre::Actor* RFootActor;
NxOgre::Actor* ForeActor;

Ogre::SceneNode* mPlayerNode;

NxOgre::PlaneGeometry* wallGeom;
NxOgre::PlaneGeometry* wallGeom2;
NxOgre::PlaneGeometry* wallGeom3;

NxOgre::RigidBodyDescription BodyDesc;

bool isReset;

NxAppCallback* mNxCallback;

RaySceneQuery *mRaySceneQuery; // The ray scene query pointer
bool mLMouseDown, mRMouseDown; // True if the mouse buttons are down
bool mCalculatingBallPath;
Vector3 mBallDir;

};


and NxBodyMgr.cpp
NxBodyMgr* NxBodyMgr::getSingletonPtr()
{
static NxBodyMgr instance;
return &instance;
}

NxBodyMgr::NxBodyMgr()
{
mWorld = NULL;
mScene = NULL;
mRenderSystem = NULL;
mGUIRenderer = NULL;
mGUISystem = NULL;
mRenderSystem = NULL;
mRaySceneQuery = NULL;

mLMouseDown = false;
mRMouseDown = false;
mCalculatingBallPath = false;
mBallDir = NULL;
isReset = false;
}

NxBodyMgr::~NxBodyMgr()
{
if(mRaySceneQuery)
{
mSceneMgr->destroyQuery(mRaySceneQuery);
mRaySceneQuery = NULL;
}

if(mNxCallback)
{
delete mNxCallback;
mNxCallback = NULL;
}

if(mRenderSystem)
{
delete mRenderSystem;
mRenderSystem = NULL;
}
}

void NxBodyMgr::createScene()
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5f, 0.5f, 0.5f));

// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20, 80, 50);

// Position the camera
mCamera->setPosition(0, 50, -250);
mCamera->lookAt(0, 20, 0);

// CEGUI setup
mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
mGUISystem = new CEGUI::System(mGUIRenderer);

// Mouse
CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");

mRaySceneQuery = mSceneMgr->createRayQuery(Ogre::Ray());

createNxWorld();
createObject();
createNxDog();
createRoom();

// Set Contact Callback Pair
mScene->getScene()->setActorPairFlags(*mBall->getNxActor(), *ForeActor->getNxActor(), NX_NOTIFY_ON_START_TOUCH|NX_NOTIFY_ON_TOUCH|NX_NOTIFY_ON_END_TOUCH);


}

void NxBodyMgr::createFrameListener()
{
// Create a new frame listener
mFrameListener = new NxOgreFrameListener(mWindow, mCamera, mSceneMgr, mGUIRenderer);
mRoot->addFrameListener(mFrameListener);
}

void NxBodyMgr::createNxWorld()
{
// create basic physics environment
{
// Create the world
mWorld = NxOgre::World::createWorld();

// Create scene description
NxOgre::SceneDescription sceneDesc;
sceneDesc.mGravity = NxOgre::Real3(0, -50.0f, 0);
//sceneDesc.mGravity = NxOgre::Real3(0, 0, 9.8f );
sceneDesc.mName = "DemoScene";

// Create scene
mScene = mWorld->createScene(sceneDesc);

// Set some physical scene values
mScene->getMaterial(0)->setStaticFriction(0.2);
mScene->getMaterial(0)->setDynamicFriction(0.2);
mScene->getMaterial(0)->setRestitution(0.7);

// Set contact reporter for nxogre
mNxCallback = new NxAppCallback();
mScene->getScene()->setUserContactReport(mNxCallback);

// Create render system
mRenderSystem = new OGRE3DRenderSystem(mScene);
}
}

void NxBodyMgr::createObject()
{
// create object
{
mBall = mRenderSystem->createBody(new NxOgre::Sphere(10), NxOgre::Real3(0, 100, -100), "sphere.2m.mesh", BodyDesc);
mBall->getEntity()->setMaterialName("football_white");
mBall->getSceneNode()->setScale(10, 10, 10);

mBallTwo = mRenderSystem->createBody(new NxOgre::Box(10, 10, 10), NxOgre::Real3(0, 45, 0), "cube.1m.mesh");
mBallTwo->getSceneNode()->setScale(10, 10, 10);

}
}

void NxBodyMgr::createNxDog()
{
// create dog physics model
{
// load dog
mPlayerNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
Entity* mPlayerEnt = mSceneMgr->createEntity("player", "dog_hsq.mesh");
mPlayerNode->attachObject(mPlayerEnt);
mPlayerNode->setPosition(Vector3(50, 0, 0));
mPlayerNode->setScale(50, 50, 50);
mPlayerEnt->setMaterialName("dog_hsq");

mAnimState = mPlayerEnt->getAnimationState("c_Stand_Run");
mAnimState->setEnabled(true);
mAnimState->setLoop(true);

//// Set one box for dog
//mDogBox = mScene->createActor(new NxOgre::Sphere(20));


// bind actor to skeleton bones
Ogre::SkeletonPtr skel = SkeletonManager::getSingleton().load("dog_hsq.skeleton",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

HeadBone = skel->getBone("Bip02 Head");
HeadActor = mScene->createActor(new NxOgre::Sphere(20));

ForeHeadBone = skel->getBone("Bip02 Ponytail2");
ForeActor = mScene->createActor(new NxOgre::Sphere(25));

LCalfBone = skel->getBone("Bip02 L Calf");
LCalfActor= mScene->createActor(new NxOgre::Capsule(5,10));

RCalfBone = skel->getBone("Bip02 R Calf");
RCalfActor= mScene->createActor(new NxOgre::Capsule(5,10));

LClavicle = skel->getBone("Bip02 L Clavicle");
LClavicleActor = mScene->createActor(new NxOgre::Capsule(5,10));

RClavicle = skel->getBone("Bip02 R Clavicle");
RClavicleActor = mScene->createActor(new NxOgre::Capsule(5,10));

LHandBone = skel->getBone("Bip02 L Hand");
LHandActor= mScene->createActor(new NxOgre::Capsule(5,15));

RHandBone = skel->getBone("Bip02 R Hand");
RHandActor= mScene->createActor(new NxOgre::Capsule(5,15));

LFootBone = skel->getBone("Bip02 L Foot");
LFootActor= mScene->createActor(new NxOgre::Capsule(5,10));

RFootBone = skel->getBone("Bip02 R Foot");
RFootActor= mScene->createActor(new NxOgre::Capsule(5,10));

}
}

void NxBodyMgr::createRoom()
{
{
// Create floor plane (BloodyMess)
mScene->createSceneGeometry(new NxOgre::PlaneGeometry(0, NxOgre::Real3(0, 1, 0)), NxOgre::Matrix44_Identity);

// Create floor plane (Ogre)
plane = new MovablePlane("Plane");
plane->d = 0;
plane->normal = Vector3::UNIT_Y;
Ogre::MeshManager::getSingleton().createPlane("PlaneMesh",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*plane, 500, 500, 32, 32, true, 1, 100, 100, Vector3::UNIT_Z);
planeEnt = mSceneMgr->createEntity("PlaneEntity", "PlaneMesh");
planeEnt->setMaterialName("Ogre/Skin");

mPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mPlaneNode->attachObject(planeEnt);
}

{
// Create Another Wall1
wallGeom = new NxOgre::PlaneGeometry(-250, NxOgre::Real3(0, 0, -1));
mScene->createSceneGeometry(wallGeom, NxOgre::Matrix44_Identity);

wall = new MovablePlane("wall");
wall->d = -250;
wall->normal = Vector3::UNIT_Z;
Ogre::MeshManager::getSingleton().createPlane("wallMesh",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*wall, 500, 250, 32, 32, true, 1, 100, 100, Vector3::UNIT_Y);
wallEnt = mSceneMgr->createEntity("wallEntity", "wallMesh");
wallEnt->setMaterialName("Ogre/Skin");

mWallNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mWallNode->attachObject(wallEnt);
mWallNode->translate( Vector3(0,125,0));
}

{
// Create Another Wall2
wallGeom2 = new NxOgre::PlaneGeometry(-250, NxOgre::Real3(-1, 0, 0));
mScene->createSceneGeometry(wallGeom2, NxOgre::Matrix44_Identity);

MovablePlane* wall2 = new MovablePlane("wall2");
wall2->d = -250;
wall2->normal = Vector3::UNIT_X;
Ogre::MeshManager::getSingleton().createPlane("wallMesh2",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*wall2, 500, 250, 32, 32, true, 1, 100, 100, Vector3::UNIT_Y);
Entity *wallEnt2 = mSceneMgr->createEntity("wallEntity2", "wallMesh2");
wallEnt2->setMaterialName("Ogre/Skin");

Ogre::SceneNode* mWallNode2 = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mWallNode2->attachObject(wallEnt2);
mWallNode2->translate( Vector3(0,125,0));
}

{
// Create Another Wall3
wallGeom3 = new NxOgre::PlaneGeometry(-250, NxOgre::Real3(1, 0, 0));
mScene->createSceneGeometry(wallGeom3, NxOgre::Matrix44_Identity);

MovablePlane* wall3 = new MovablePlane("wall3");
wall3->d = -250;
wall3->normal = -Vector3::UNIT_X;
Ogre::MeshManager::getSingleton().createPlane("wallMesh3",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*wall3, 500, 250, 32, 32, true, 1, 100, 100, Vector3::UNIT_Y);
Entity *wallEnt3 = mSceneMgr->createEntity("wallEntity3", "wallMesh3");
wallEnt3->setMaterialName("Ogre/Skin");

Ogre::SceneNode* mWallNode3 = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mWallNode3->attachObject(wallEnt3);
mWallNode3->translate( Vector3(0,125,0));
}

{
// Create Screen Wall
mScene->createSceneGeometry(new NxOgre::PlaneGeometry(-250, NxOgre::Real3(0, 0, 1)), NxOgre::Matrix44_Identity);
// Create Ceiling
mScene->createSceneGeometry(new NxOgre::PlaneGeometry(-250, NxOgre::Real3(0, -1, 0)), NxOgre::Matrix44_Identity);
}
}

void NxBodyMgr::updateDogActor()
{
//Ogre::Vector3 dogpos = mPlayerNode->getPosition();
//mDogBox->setGlobalPosition(NxOgre::Real3(dogpos));

Ogre::Vector3 bonePos0 = mPlayerNode->_getFullTransform()*HeadBone->_getDerivedPosition();
HeadActor->setGlobalPosition(NxOgre::Real3(bonePos0));

Ogre::Vector3 bonePos1 = mPlayerNode->_getFullTransform()*LCalfBone->_getDerivedPosition();
LCalfActor->setGlobalPosition(NxOgre::Real3(bonePos1));

Ogre::Vector3 bonePos2 = mPlayerNode->_getFullTransform()*RCalfBone->_getDerivedPosition();
RCalfActor->setGlobalPosition(NxOgre::Real3(bonePos2));

Ogre::Vector3 bonePos3 = mPlayerNode->_getFullTransform()*LClavicle->_getDerivedPosition();
LClavicleActor->setGlobalPosition(NxOgre::Real3(bonePos3));

Ogre::Vector3 bonePos4 = mPlayerNode->_getFullTransform()*RClavicle->_getDerivedPosition();
RClavicleActor->setGlobalPosition(NxOgre::Real3(bonePos4));

Ogre::Vector3 bonePos5 = mPlayerNode->_getFullTransform()*LHandBone->_getDerivedPosition();
LHandActor->setGlobalPosition(NxOgre::Real3(bonePos5));

Ogre::Vector3 bonePos6 = mPlayerNode->_getFullTransform()*RHandBone->_getDerivedPosition();
RHandActor->setGlobalPosition(NxOgre::Real3(bonePos6));

Ogre::Vector3 bonePos7 = mPlayerNode->_getFullTransform()*LFootBone->_getDerivedPosition();
LFootActor->setGlobalPosition(NxOgre::Real3(bonePos7));

Ogre::Vector3 bonePos8 = mPlayerNode->_getFullTransform()*RFootBone->_getDerivedPosition();
RFootActor->setGlobalPosition(NxOgre::Real3(bonePos8));

Ogre::Vector3 bonePos9 = mPlayerNode->_getFullTransform()*ForeHeadBone->_getDerivedPosition();
ForeActor->setGlobalPosition(NxOgre::Real3(bonePos9));

}

void NxBodyMgr::update()
{
updateDogActor();

////Collision Response
//if(mNxCallback->isContact())
//{
// mFlyDisk->addForce(NxOgre::Real3(0, 1000, 0));
// //isContact = false;
// mNxCallback->setContactSignal(false);
//}
}

bool NxBodyMgr::keyPressed(const OIS::KeyEvent &arg)
{
//just a test to push the ball
switch(arg.key)
{
case OIS::KC_I:
mBall->addForce(NxOgre::Real3(0, 0, 20), NxOgre::Enums::ForceMode_Force);
break;
case OIS::KC_K:
mBall->addForce(NxOgre::Real3(0, 0, -20), NxOgre::Enums::ForceMode_Force);
break;
case OIS::KC_J:
mBall->addForce(NxOgre::Real3(20, 0, 0), NxOgre::Enums::ForceMode_Force);
break;
case OIS::KC_L:
mBall->addForce(NxOgre::Real3(-20, 0, 0), NxOgre::Enums::ForceMode_Force);
break;
case OIS::KC_U:
mBall->addForce(NxOgre::Real3(0, 100, 0), NxOgre::Enums::ForceMode_Force);
break;
case OIS::KC_G:
mBall->setGlobalPosition(NxOgre::Real3(0, 50, -230));
mBall->getNxActor()->raiseBodyFlag(NX_BF_KINEMATIC);
isReset = true;
break;
default:
break;
}

return true;
}

bool NxBodyMgr::keyReleased(const OIS::KeyEvent &arg)
{
return true;

}

bool NxBodyMgr::mouseMoved(const OIS::MouseEvent &arg)
{
if (mLMouseDown)
{
if (mCalculatingBallPath)
{
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ogre::Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.d_x/float(arg.state.width),mousePos.d_y/float(arg.state.height));

mRaySceneQuery->setRay(mouseRay);
mRaySceneQuery->setSortByDistance(true);

RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr;

for ( itr = result.begin(); itr != result.end(); itr++ )
{
if (itr->movable && itr->movable->getName() == "PlaneEntity" || itr->movable->getName() == "wallEntity")
{
// calculate ball's move dir (mouse cursor point out)
mBallDir = (mouseRay.getPoint(itr->distance) - mBall->getSceneNode()->getPosition()).normalisedCopy();

}

}
}

}
else if (mRMouseDown)
{
//mCamera->yaw(Degree(-arg.state.X.rel * mRotateSpeed));
//mCamera->pitch(Degree(-arg.state.Y.rel * mRotateSpeed));
}

return true;
}

bool NxBodyMgr::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if(id == OIS::MB_Left)
{
mLMouseDown = true;

CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ogre::Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.d_x/float(arg.state.width), mousePos.d_y/float(arg.state.height) );

mRaySceneQuery->setRay(mouseRay);
mRaySceneQuery->setSortByDistance(true);

// Execute query
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr = result.begin();

if ( itr != result.end() && (itr->movable && itr->movable->getParentSceneNode()->getName() == mBall->getSceneNode()->getName()) ) // I clicked the ball
{
mCalculatingBallPath= true;
mBall->getSceneNode()->showBoundingBox(true);

}
else
{
mBall->getSceneNode()->showBoundingBox(false);
mCalculatingBallPath= false;
}
}
else if (id == OIS::MB_Right)
{
CEGUI::MouseCursor::getSingleton().hide();
mRMouseDown = true;
}

return true;
}

bool NxBodyMgr::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if (id == OIS::MB_Left)
{

if(mCalculatingBallPath && isReset)
{
mBall->getNxActor()->clearBodyFlag(NX_BF_KINEMATIC);
mBall->addForce(20000 * (NxOgre::Real3)mBallDir);
//mFlyDisk->addForce((NxOgre::Real3)Vector3(0, 5000, -5000));

mCalculatingBallPath = false;
isReset = false;
}

mLMouseDown = false;

}
else if (id == OIS::MB_Right)
{
CEGUI::MouseCursor::getSingleton().show();
mRMouseDown = false;
}

return true;
}


Really appreciate if someone could help me out. I noticed mball in keypressed function may have a null pointer, but I dont know how to fix this.
Thanks. :D

spacegaier

22-08-2009 13:01:34

Telling from your second thread here, I'd say it is no NxOgre problem ;) . Next time, please first try to find the crash causing line and then you know where to post about it.

Therefore, I lock the thread here. If there is a reason to reopen, PM to me.