Why the box can't move??

billwillman

28-03-2007 11:37:57


#include "ExampleApplication.h"
#include "ExampleFrameListener.h"
#include "OgreNewt.h"

class MyFrameListener:public ExampleFrameListener
{
private:
OgreNewt::World *mworld;
public:
MyFrameListener(RenderWindow* win, Camera* cam,OgreNewt::World *w):ExampleFrameListener(win,cam)
{
mworld=w;
}

bool frameStarted(const FrameEvent& evt)
{
if(!ExampleFrameListener::frameStarted(evt))
return false;
mworld->update(60);
return true;
}

};

class MyApplication : public ExampleApplication
{
private:
OgreNewt::World *mworld;
public:
MyApplication() {
mworld=new OgreNewt::World();


}

~MyApplication() {

delete mworld;
OgreNewt::Debugger::getSingleton().deInit();
}

protected:

void createFrameListener(void)
{
mFrameListener=new MyFrameListener(mWindow,mCamera,mworld);
mFrameListener->showDebugOverlay(true);
mRoot->addFrameListener(mFrameListener);

}

// Just override the mandatory create scene method
void createScene(void)
{

//环境设置
Vector3 size=Vector3(1000,1000,1000);
mworld->setWorldSize(size,-size);

mSceneMgr->setAmbientLight(ColourValue(1,0.2,0));
Entity *sceneentity=mSceneMgr->createEntity("scneneEntity","Box01.mesh");
sceneentity->setNormaliseNormals(true);
SceneNode *scenenode=mSceneMgr->getRootSceneNode()->createChildSceneNode("sceneNode");
scenenode->attachObject(sceneentity);
OgreNewt::Collision *col=new OgreNewt::CollisionPrimitives::TreeCollision(mworld,scenenode,false);
OgreNewt::Body *body=new OgreNewt::Body(mworld,col);
delete col;
body->attachToNode(scenenode);

//加入一个BOX模型

Entity *boxentity=mSceneMgr->createEntity("boxEntity","cube.mesh");
boxentity->setNormaliseNormals(true);
SceneNode *boxnode=mSceneMgr->getRootSceneNode()->createChildSceneNode("boxNode");
boxnode->attachObject(boxentity);

AxisAlignedBox boxsize=boxentity->getBoundingBox();


OgreNewt::Collision *boxcol=new OgreNewt::CollisionPrimitives::Box(mworld,boxsize.getSize());
OgreNewt::Body* boxbody=new OgreNewt::Body(mworld,boxcol);
delete boxcol;
boxbody->attachToNode(boxnode);
boxbody->setPositionOrientation(Vector3(0,500,0),Quaternion::IDENTITY);
Ogre::Real mass=boxsize.getSize().x*boxsize.getSize().y*boxsize.getSize().z*2.5;
Vector3 intertia=OgreNewt::MomentOfInertia::CalcBoxSolid(mass,boxsize.getSize());
boxbody->setMassMatrix(mass,intertia);
boxbody->setStandardForceCallback();
boxbody->setAutoFreeze(1);

}

};



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
// Create application object
MyApplication app;

try {
app.go();
} catch( Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occured: " << e.getFullDescription();
#endif
}


return 0;
}

#ifdef __cplusplus
}
#endif

When the project runs, I can't see the Box moved(it's still in the sky)
why?

Langhole

28-03-2007 20:48:29

From what I can see...


void createFrameListener(void)
{
mFrameListener=new MyFrameListener(mWindow,mCamera,mworld);
mFrameListener->showDebugOverlay(true);
mRoot->addFrameListener(mFrameListener);

}


You need to add a 'physics' frame listener, so your bodies can actually react to forces applied etc. this is how it's done basically in the SimpleVehicle demo:


void OgreNewtonApplication::createFrameListener()
{
mFrameListener = new OgreNewtonFrameListener( mWindow, mCamera, mSceneMgr, m_World, msnCam, mCar );
mRoot->addFrameListener(mFrameListener);

mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, mSceneMgr, m_World, 60 );
mRoot->addFrameListener(mNewtonListener);
}


Of course this means you'll need a second frame listener, one that will work with OgreNewt, so read the examples :P

billwillman

29-03-2007 02:33:15

Sorry.
But I change My Code,Add your said.
but it doesn't move.
I see the Example.
I copy my "CreateScene" Code in the First Example.And it still doesn't move.
What do I forget?

My Change Code:

mFrameListener=new MyFrameListener(mWindow,mCamera);
mRoot->addFrameListener(mFrameListener);
FrameListener* newtonlistener= new OgreNewt::BasicFrameListener( mWindow, mSceneMgr, mworld, 60 );
mRoot->addFrameListener(newtonlistener);