kintaro
03-04-2006 19:18:54
Hi, I am trying to run oFusion with OgreNewt, in the way that I tried to add OgreNewt in a oFusion sample, oSceneLoader demo. The main code is below. In the case I putted a scene that load correctly, and the green ninja from ogre. Till here, everything is ok. When I add a box primitive to the ninja node, the compiler does not gives any error, but when I run the program, I see that in the ninja disapears in the first second that the programs begins to run. The program continue to run, does not crash, but the ninja disapears.
Thank for help to solve this program.
Here is the code:
Thank for help to solve this program.
Here is the code:
#include "ExampleApplication.h"
// oScene loader library header file
#include "OgreOSMScene.h"
#include <OgreNewt.h>
// Callback handler to post-process created objects
class oSceneCallback : public OSMSceneCallbacks {
public:
// We override the OnCreate method for cameras (See IXMLSceneCallbacks class)
void OnCameraCreate(Ogre::Camera* pCamera, TiXmlElement* pCameraDesc) {
// If a camera of name "FirstCamera" is loaded, it will be set as the default current
if(pCamera->getName() == "FirstCamera")
Ogre::Root::getSingleton().getAutoCreatedWindow()->getViewport(0)->setCamera(pCamera);
}
};
// Event handler to animate
class oSceneLibDemoFrameListener : public ExampleFrameListener
{
protected:
SceneManager* mSceneMgr;
public:
oSceneLibDemoFrameListener(SceneManager* sceneMgr, RenderWindow* win, Camera* cam)
: mSceneMgr(sceneMgr), ExampleFrameListener(win, cam)
{
}
bool frameStarted(const FrameEvent& evt)
{
static Real timeDelay = 0;
static Real currentTime = 0;
timeDelay -= evt.timeSinceLastFrame;
// We can cycle cameras with the 'c' key
if (mInputDevice->isKeyDown(KC_C) && timeDelay <= 0) {
Camera* firstCam;
Camera* currentCam = mWindow->getViewport(0)->getCamera();
Viewport* vp = mWindow->getViewport(0);
SceneManager::CameraIterator it = mSceneMgr->getCameraIterator();
if(it.hasMoreElements())
firstCam = it.peekNextValue();
while(it.hasMoreElements()) {
Ogre::Camera* cam = it.getNext();
if(currentCam == cam) {
Ogre::Camera* camera = it.hasMoreElements() ? it.getNext() : firstCam;
vp->setCamera(camera);
}
}
timeDelay = 0.5f;
}
// We update all loaded animations each frame
SceneManager::AnimationIterator animationIt = mSceneMgr->getAnimationIterator();
while(animationIt.hasMoreElements()) {
Animation* animation = animationIt.getNext();
const Animation::TrackList& trackList = animation->_getTrackList();
Animation::TrackList::const_iterator it = trackList.begin();
Animation::TrackList::const_iterator iend = trackList.end();
for(; it != iend; ++it) {
const Ogre::AnimationTrack* track = it->second;
track->getAssociatedNode()->resetToInitialState();
}
currentTime += evt.timeSinceLastFrame;
animation->apply(currentTime);
}
// Call default
return ExampleFrameListener::frameStarted(evt);
}
};
class oSceneLibApplication : public ExampleApplication {
public:
oSceneLibApplication() {}
protected:
// Just override the mandatory create scene method
void createScene(void)
{
// Here is the code that will load the scene file
// A fixed filename "scene.osm" is implemented for this demo
// Create the scene loader
OSMScene oScene;
// Create an oE_Loader Callback object to post-process created objects
oSceneCallback oe_Callback;
// Initialises with the scene to be loaded and the callback if requiered
oScene.initialise("teste.osm", &oe_Callback);
// create and setup the scene in the root node
oScene.createScene();
mSceneMgr = oScene.getSceneManager();
mCamera = oScene.getCameraList().at(0);
/*// Create a default camera in case no cameras were saved in the scene
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Vector3(0,0,500));
// Look back along -Z
mCamera->lookAt(Vector3(0,0,-300));*/
mCamera->setNearClipDistance(5);
// Create one viewport, entire window
Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
m_World = new OgreNewt::World();
//create ninja entity
Entity* mEntity;
mEntity = mSceneMgr->createEntity( "Ninja", "ninja.mesh" );
SceneNode* node;
node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
node->attachObject(mEntity);
node->setPosition(mCamera->getPosition().x - 700, mCamera->getPosition().y + 100 , mCamera->getPosition().z);
node->scale(1,1,1);
OgreNewt::Collision* ent_col = new OgreNewt::CollisionPrimitives::Box( m_World, Ogre::Vector3( 20 , 100 , 20 ) );
OgreNewt::Body* ent_body = new OgreNewt::Body( m_World, ent_col );
Ogre::Vector3 ent_inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( 10, Ogre::Vector3( 2 , 10 , 2) );
ent_body->setMassMatrix( 10 , ent_inertia );
ent_body->attachToNode( node );
mWindow->setDebugText(ent_body->getOgreNode()->getAttachedObject(0)->getName());
Ogre::Vector3 Up_Direction_1( 0 , 1 , 0 );
OgreNewt::BasicJoints::UpVector* Up_Vector_1;
Up_Vector_1 = new OgreNewt::BasicJoints::UpVector( m_World, ent_body, Up_Direction_1 );
Ogre::Vector3 Up_Direction_2 ( 1 , 0 , 0 );
OgreNewt::BasicJoints::UpVector* Up_Vector_2;
Up_Vector_2 = new OgreNewt::BasicJoints::UpVector( m_World, ent_body, Up_Direction_2 );
//ent_body->setStandardForceCallback();
delete ent_col;
//delete m_World;
}
// The scene manager will be created by the Scene Loader lib
void chooseSceneManager(void) {}
void createCamera(void) {}
void createViewports(void) {}
// Create new frame listener
void createFrameListener(void)
{
mFrameListener= new oSceneLibDemoFrameListener(mSceneMgr, mWindow, mCamera);
mRoot->addFrameListener(mFrameListener);
Ogre::FrameListener* mNewtonListener;
mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, mCamera, mSceneMgr, m_World, 60 );
mRoot->addFrameListener(mNewtonListener);
}
private:
OgreNewt::World* m_World;
};