[Ogre] Crash with a simple code

Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

[Ogre] Crash with a simple code

Post by Hindi »

I would like to build a physicManager which will create the rigid bodies and run the simulation. I tried this code in my main loop and it worked great. I ceated the class and a method for box shapes but it crashes as soon as I add the rigid body to the world.

Code: Select all

void PhysicManager::setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shapeDim)
{
    shape = new btBoxShape(btVector3(shapeDim.x, shapeDim.y, shapeDim.z));
    shape->calculateLocalInertia(10, inertia);
    MyMotionState* motionState = new MyMotionState(node);
    body = new btRigidBody(10, motionState, shape, inertia);
    body->setUserPointer((void *) (node));

    mBodyList.push_back(body);
    mShapeList.push_back(shape);

    mWorld->addRigidBody(body);
}
I tried to store the bodies and shapes but it didn't help.

The world initialisation is quiet simple :

Code: Select all

    mBroadphase = new btDbvtBroadphase();
    mCollisionConfiguration = new btDefaultCollisionConfiguration();
    mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
    mSolver = new btSequentialImpulseConstraintSolver;
    mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);
Any idea ?

EDIT : The whole class for more info :

Code: Select all

#include "PhysicManager.h"
PhysicManager* PhysicManager::_singleton = NULL;

PhysicManager::PhysicManager()
{

}

PhysicManager::~PhysicManager()
{
	shutdown();
}

void PhysicManager::start(Ogre::SceneManager* sceneMgr)
{
	mSceneMgr = sceneMgr;
	
	mBroadphase = new btDbvtBroadphase();
    mCollisionConfiguration = new btDefaultCollisionConfiguration();
    mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
    mSolver = new btSequentialImpulseConstraintSolver;
	mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);

	shape = NULL;
	body = NULL;

	mWorld->setGravity(btVector3(0,-10,0));
}

void PhysicManager::shutdown()
{
	mBodyList.clear();
	mShapeList.clear();

	delete shape;
	delete body;
	
	//Pointeurs pour bullet physic
	delete mWorld;
	delete mSolver;
	delete mDispatcher;
	delete mCollisionConfiguration;
	delete mBroadphase;
}

PhysicManager* PhysicManager::getInstance()
{
if (NULL == _singleton)
    {
    _singleton =  new PhysicManager;
    }

return _singleton;
}

bool PhysicManager::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
	mWorld->stepSimulation(evt.timeSinceLastFrame, 1);
        return true;
}

void PhysicManager::setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shapeDim)
{
	shape = new btBoxShape(btVector3(shapeDim.x, shapeDim.y, shapeDim.z));
    shape->calculateLocalInertia(10, inertia);
    MyMotionState* motionState = new MyMotionState(node);
    body = new btRigidBody(10, motionState, shape, inertia);
    body->setUserPointer((void *) (node));

    mBodyList.push_back(body);
	mShapeList.push_back(shape);

    //mWorld->addRigidBody(body);
}

Code: Select all

#pragma once

#include <Ogre.h>
#include <vector>

#include <btBulletDynamicsCommon.h>
#include <Bullet-C-Api.h>
#include <btBulletCollisionCommon.h>

#include "MyMotionState.h"
#include "../Console.h"

class PhysicManager : public Ogre::FrameListener
{
	public:
		//Constructeur
		~PhysicManager();
		static PhysicManager* getInstance();

		void start(Ogre::SceneManager* sceneMgr);
		void shutdown();		

		//Vérification des entrées
		bool frameRenderingQueued(const Ogre::FrameEvent& evt);

		void setRigidBoxBody(Ogre::SceneNode *node, Ogre::Vector3 shape);
		
	private:
		PhysicManager();
		static PhysicManager* _singleton;

		Ogre::SceneManager* mSceneMgr;
		
		btDbvtBroadphase* mBroadphase;
		btDefaultCollisionConfiguration* mCollisionConfiguration;
		btCollisionDispatcher* mDispatcher;
		btSequentialImpulseConstraintSolver* mSolver;
		btDiscreteDynamicsWorld* mWorld;
		btCollisionShape* shape;
		btRigidBody* body;
		btVector3 inertia;
		
		std::vector<btRigidBody*> mBodyList;
		std::vector<btCollisionShape*> mShapeList;
};

Code: Select all

#pragma once

#include <btBulletDynamicsCommon.h>
#include <Bullet-C-Api.h>
#include <btBulletCollisionCommon.h>

#include <Ogre.h>

class MyMotionState : public btMotionState
{
    public:
        MyMotionState(Ogre::SceneNode* node)
        {
                mNode = node;
                mTrans.setIdentity();
        }
        virtual ~MyMotionState()
        { }
        virtual void getWorldTransform(btTransform &worldTrans) const
        {
                worldTrans = mTrans;
        }
        virtual void setWorldTransform(const btTransform &worldTrans)
        {
                mTrans = worldTrans;
                btQuaternion ori = mTrans.getRotation();
                btVector3 pos = mTrans.getOrigin();
                mNode->setPosition(Ogre::Vector3(pos.x(),pos.y(),pos.z()));
                mNode->setOrientation(Ogre::Quaternion(ori.w(),ori.x(),ori.y(),ori.z()));
        }
 
    protected:
        Ogre::SceneNode* mNode;
        btTransform mTrans;
};
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

Nothing obviously wrong that I can see.
Do you have the stack trace for the crash?
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

Visual studio point on this fonction in the xmemory file :

Code: Select all

// TEMPLATE FUNCTIONS _Cons_val AND _Dest_val
template<class _Alloc,
	class _Ty1,
	class _Ty2>
	void _Cons_val(_Alloc& _Alval, _Ty1 *_Pdest, _Ty2&& _Src)
	{	// construct using allocator
	_Alval.construct(_Pdest, _STD forward<_Ty2>(_Src));
	}
There are several "unable to evaluate" error on : __vfptr, m_shapetype, m_userPointer (all from _Pdest (btColisionShape)).
This didn't help me to understand what's wrong with the shape...
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

Yeah, I can see why that isn't incredibly clear :)

Just looking at the code for addRigidBody it doesn't seem as if there are many points where you could get an exception raised. What might be a useful start though is if you skip using your MyMotionState object (just pass NULL for now) and seeing if you get the same crash in addRigidBody.
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

I found the addRigidBoddy method but I don't understand what could make my code crashes. I tried to use the step by step debug in order to see where in the addRigidMethod the problem is. Sadly, it crashes as soon as I reach the mWorld->addRigidBody(body) line and press F11 (for detailed step). Could it be a problem with the btDiscretDynamicWorld ?

I tried without MyMotionState as you adviced but the error is still the same.

The addRigidBody method :

Code: Select all

void	btDiscreteDynamicsWorld::addRigidBody(btRigidBody* body)
{
	if (!body->isStaticOrKinematicObject() && !(body->getFlags() &BT_DISABLE_WORLD_GRAVITY))
	{
		body->setGravity(m_gravity);
	}

	if (body->getCollisionShape())
	{
		if (!body->isStaticObject())
		{
			m_nonStaticRigidBodies.push_back(body);
		} else
		{
			body->setActivationState(ISLAND_SLEEPING);
		}

		bool isDynamic = !(body->isStaticObject() || body->isKinematicObject());
		short collisionFilterGroup = isDynamic? short(btBroadphaseProxy::DefaultFilter) : short(btBroadphaseProxy::StaticFilter);
		short collisionFilterMask = isDynamic? 	short(btBroadphaseProxy::AllFilter) : 	short(btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);

		addCollisionObject(body,collisionFilterGroup,collisionFilterMask);
	}
}
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

If it won't even step into the addRigidBody function without crashing then it does suggest that somehow the mWorld object is corrupt at that point... can you look at it under locals and see if some of it's members (gravity, m_nonStaticRigidBodies, etc) look like they have 'sensible' values.
You could even simplify further and do :

mWorld->addRigidBody(NULL);

and see if that lets you step into the addRigidBodyFunction ....
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

Thank you for all the debug tip. I'm learning a lot on what tests I have to do !

I cannot step into the addRigidBody function with a NULL argument.
As you said, mWorld is the problem :

Image
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

Where abouts do you call start(Ogre::SceneManager* sceneMgr) to create the world physics objects?
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

I call it before my main loop during the scene initialisation :

Code: Select all

void AppDemarrage::createFrameListener(Ogre::Terrain *terrain)
{
	FrameListenerAder::getInstance()->start(mRoot);

	mPlayer = new Player(mWindow, mSceneMgr);
	
	PlayerInputManager::getInstance()->start(mWindow, mPlayer);
	
	PhysicManager::getInstance()->start(mSceneMgr, mRoot);                //HERE
	
	ProjectileManager::getInstance()->start(mSceneMgr);
	
	mRoot->addFrameListener(ProjectileManager::getInstance());
	mRoot->addFrameListener(mPlayer);
	mRoot->addFrameListener(PlayerInputManager::getInstance());
	mRoot->addFrameListener(PhysicManager::getInstance());
	mRoot->addFrameListener(MeshManager::getInstance());
	mRoot->addFrameListener(EnemyManager::getInstance());
}
the whole class :

Code: Select all

#include "AppDemarrage.h"

AppDemarrage::AppDemarrage() : mRoot(0)
{

}

AppDemarrage::~AppDemarrage()
{
	//Les pointeurs pour ogre
    delete mRoot;
	delete mCamera;
	delete mSceneMgr;
	delete mWindow;
	delete mLight;

}

bool AppDemarrage::start()
{
	//On créé le root
	mRoot = new Ogre::Root("plugins_d.cfg", "ogre.cfg", "Ogre.log");

	//On indique le fichier de config pour le chargement des ressources
	Ogre::ConfigFile configFile;
	configFile.load("resources_d.cfg");
	
	//On créé un itérateur et on parcourt les éléments du fichier de config pour ajouter les dossiers de ressources à la liste des dossiers à charger.
	Ogre::ConfigFile::SectionIterator seci = configFile.getSectionIterator();
	Ogre::String secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
		Ogre::ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;
			Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
		}
	}
	
	//On définit une configuration d'affichage pour pas se taper la fenêtre au lancement.
	Ogre::RenderSystem *rs = mRoot->getRenderSystemByName("Direct3D9 Rendering Subsystem"); // Pour OpenGL : "OpenGL Rendering Subsystem"
	mRoot->setRenderSystem(rs);
	rs->setConfigOption("Full Screen", "No");
	rs->setConfigOption("Video Mode", "1280 x 800 @ 32-bit colour");
	rs->setConfigOption("VSync", "Yes");

	//On créé la fenêtre
	mWindow = mRoot->initialise(true, "La trique");

	//On charge toutes les ressources d'un coup (à modifier à l'avenir)
	Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	//Créatio ndela scène
	mSceneMgr = mRoot->createSceneManager("DefaultSceneManager", "Scene Manager");

	//On éclaire un peu
	mSceneMgr->setAmbientLight(Ogre::ColourValue(1.0f, 1.0f, 1.0f));

	Ogre::Vector3 lightdir(0.55f, -0.3f, 0.75f);
	mLight = mSceneMgr->createLight("terrainLight");
	mLight->setType(Ogre::Light::LT_DIRECTIONAL);
	mLight->setDirection(lightdir);
	mLight->setDiffuseColour(Ogre::ColourValue::White);
	mLight->setSpecularColour(Ogre::ColourValue(0.4f, 0.4f, 0.4f));
	
	//On créé la console
	new Console;
	Console::getSingleton().start(mRoot);
	mRoot->addFrameListener(Console::getSingletonPtr());

	Ogre::Terrain *terrain = FieldManager::getInstance()->createField(mSceneMgr, mLight);
	
	//On créé le gestionnaire d'évènements
	
	this->createFrameListener(terrain);                                                                                  //AND HERE
	mCamera = mPlayer->getCamera();

	std::deque<Ogre::Vector3> mWalkList;
    mWalkList.push_back(Ogre::Vector3(550.0f,  0.0f,  50.0f ));
    mWalkList.push_back(Ogre::Vector3(-100.0f,  0.0f, -200.0f));
	
	MeshManager::getInstance()->start(mSceneMgr);
	EnemyManager::getInstance()->start(mSceneMgr, mWalkList);

	EnemyManager::getInstance()->createRobot(Ogre::Vector3(10, 10, 0));
	//EnemyManager::getInstance()->createRobot(Ogre::Vector3(00, 10, 0));

	ProjectileManager::getInstance()->createLaser(Ogre::Vector3(0,0,0), Ogre::Vector3(1, 0, 0));

 	mRoot->startRendering();
	/*
	while( 42 )
    {
        //Pump messages in all registered RenderWindow windows
        Ogre::WindowEventUtilities::messagePump();

		if(PlayerInputManager::getInstance()->playerShutDown())
			return false;

		if(mWindow->isClosed()) 
			return false;
 
		// Render a frame
		if(!mRoot->renderOneFrame()) 
			return false;
    }*/

    return true;
}

void AppDemarrage::createFrameListener(Ogre::Terrain *terrain)
{
	FrameListenerAder::getInstance()->start(mRoot);

	mPlayer = new Player(mWindow, mSceneMgr);
	
	PlayerInputManager::getInstance()->start(mWindow, mPlayer);
	
	PhysicManager::getInstance()->start(mSceneMgr, mRoot);
	
	ProjectileManager::getInstance()->start(mSceneMgr);
	
	mRoot->addFrameListener(ProjectileManager::getInstance());
	mRoot->addFrameListener(mPlayer);
	mRoot->addFrameListener(PlayerInputManager::getInstance());
	mRoot->addFrameListener(PhysicManager::getInstance());
	mRoot->addFrameListener(MeshManager::getInstance());
	mRoot->addFrameListener(EnemyManager::getInstance());
}

xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

Ok,

You're going to need to put a breakpoint at the point you do new btDiscreteDynamicsWorld and see if thats hit, and that the object looks 'correct' after the construction. I'd also put one in void PhysicManager::shutdown() to make sure that the objects aren't being deleted unexpectedly.
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

The state of mWorld when it's created is the same as the one in the screenshot : with a lot of"unable to find the symbol" or "unable to evaluate the expression".

I still don't understand why. Isn't it the basic way to create the btDiscreteDynamicWorld ?

EDIT : Actually I did a wrong test.. The btDiscretDynamicWorld is right after it's creation.


And mWorld isn't deleted unexpectedly
Hindi
Posts: 7
Joined: Tue Jan 29, 2013 10:16 am

Re: [Ogre] Crash with a simple code

Post by Hindi »

Oh my god... I feel so bad right now.

When you asked when the start() method was used, I figured out the Player object (which need the mWorld) was created before the start() method was called.
I fixed that but it was still crashing... Because I left the addRigidBody() with a NULL argument !

I set it with the body and it works fine now...

I can't believe I missed the Player thing. What a waste of time !

Thank you a lot for your help anyway, I learned a lot about how to debug today ! :D
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: [Ogre] Crash with a simple code

Post by xexuxjy »

hehe, np. only know these things as I've done the same stuff myself ;)
billyk
Posts: 1
Joined: Fri Nov 01, 2013 6:59 pm

Re: [Ogre] Crash with a simple code

Post by billyk »

Hindi wrote:Oh my god... I feel so bad right now.

When you asked when the start() method was used, I figured out the Player object (which need the mWorld) was created before the start() method was called.
I fixed that but it was still crashing... Because I left the addRigidBody() with a NULL argument ! This is clearly not the right way of doing it.

I set it with the body and it works fine now...

I can't believe I missed the Player thing. What a waste of time !

Thank you a lot for your help anyway, I learned a lot about how to debug today ! :D
Hi Hindi,

sorry if this is a stupid question, but can you please share the code that you use to do this? Thank you.