Print

This is the source code for OgreNewt 2 tutorial.

OgreNewtApplication.h

#ifndef __OgreNewtApplication_h_
#define __OgreNewtTut_h_
 
#include "ExampleApplication.h"
#include <OgreNewt.h>
#include "OgreNewt_BasicFrameListener.h"
 
class OgreNewtApplication :
    public ExampleApplication
{
public:
    OgreNewtApplication(void);
    ~OgreNewtApplication(void);
 
    void createScene();
 
    void createFrameListener(void);
 
    void crazyCallback(OgreNewt::Body* body, float timeStep, int threadIndex );
private:
    OgreNewt::World *mWorld;
};
 
#endif

 

OgreNewtApplication.cpp

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
 
#include "OgreNewtApplication.h"
 
OgreNewtApplication::OgreNewtApplication(void)
{
    mWorld = new OgreNewt::World();
}
 
OgreNewtApplication::~OgreNewtApplication(void)
{
    delete mWorld;
}
 
void OgreNewtApplication::createScene()
{
    Ogre::Light *light = mSceneMgr->createLight("main_light");
    light->setPosition(0, 50, 0);
    light->setDiffuseColour(Ogre::ColourValue(1, 0.5, 0.5));
    mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5,0.5,0.5));
 
    mCamera->setPosition(Ogre::Vector3(100,50,100));
    mCamera->lookAt(0,0,0);
 
    Ogre::Vector3 size(100.0,5.0,100.0);
    Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    Ogre::Entity* ent = mSceneMgr->createEntity( "floorobj", "box.mesh" );
    node->attachObject( ent );
    node->setScale( size );
 
    OgreNewt::CollisionPtr col( new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, ent, true));
    OgreNewt::Body* floorbody = new OgreNewt::Body( mWorld, col );
    floorbody->attachNode(node);
    floorbody->setPositionOrientation( Ogre::Vector3(0,-5,0), Ogre::Quaternion::IDENTITY );
 
    // BOX
    size = Ogre::Vector3( 5, 5, 5 );
    node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    ent = mSceneMgr->createEntity("box_body", "box.mesh" );
    node->attachObject( ent );
    node->setScale( size );
 
    // rigid body.
    OgreNewt::CollisionPrimitives::Box *boxcol = new OgreNewt::CollisionPrimitives::Box(mWorld, size);
    col = OgreNewt::CollisionPtr(boxcol);
    OgreNewt::Body* bod = new OgreNewt::Body( mWorld, col );
    bod->attachNode( node );
 
    // initial position
    bod->setPositionOrientation( Ogre::Vector3(0,20,5), Ogre::Quaternion::IDENTITY );
 
    Ogre::Real mass = 10.0;
    Ogre::Vector3 inertia, centerOfMass;
    boxcol->calculateInertialMatrix(inertia, centerOfMass);
 
    inertia*=mass;    
 
    bod->setMassMatrix( mass, inertia );
    bod->setCenterOfMass(centerOfMass);
 
    bod->setStandardForceCallback();
 
    // CYLINDER  
    // cylinder with a radius of 5.5, height of 10.3
    size = Ogre::Vector3( 10.3, 5.5, 5.5 );
    node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    ent = mSceneMgr->createEntity("cylinder_body", "cylinder.mesh" );
    node->attachObject( ent );
    node->setScale( size );
 
    // rigid body.
    OgreNewt::CollisionPrimitives::Cylinder *cylCol = new OgreNewt::CollisionPrimitives::Cylinder(mWorld, 5.5, 10.3);
    col = OgreNewt::CollisionPtr(cylCol);
    bod = new OgreNewt::Body( mWorld, col );
    bod->attachNode( node );
 
    // initial position
    bod->setPositionOrientation( Ogre::Vector3(20,15,15), Ogre::Quaternion(Ogre::Degree(45),Ogre::Vector3(1,1,0)) );
 
    mass = 5.0;
    cylCol->calculateInertialMatrix(inertia, centerOfMass);
 
    inertia*=mass;    
 
    bod->setMassMatrix( mass, inertia );
    bod->setCenterOfMass(centerOfMass);
 
    bod->setStandardForceCallback();
 
    // CONE
    // cone with a radius of 4.8, height of 5.0
    size = Ogre::Vector3( 5.0, 4.8, 4.8 );
 
    node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    ent = mSceneMgr->createEntity("cone_body", "cone.mesh" );
    node->attachObject( ent );
    node->setScale( size );
 
    // rigid body.
    OgreNewt::CollisionPrimitives::Cone *coneCol = new OgreNewt::CollisionPrimitives::Cone(mWorld, 4.8, 5.0);
    col = OgreNewt::CollisionPtr(coneCol);
    bod = new OgreNewt::Body( mWorld, col );
    bod->attachNode( node );
 
    // initial position
    bod->setPositionOrientation( Ogre::Vector3(0,26,5), Ogre::Quaternion(Ogre::Degree(45), Ogre::Vector3(1,0,0)) );
 
    mass = 15.0;
    coneCol->calculateInertialMatrix(inertia, centerOfMass);
 
    inertia*=mass;    
 
    bod->setMassMatrix( mass, inertia );
    bod->setCenterOfMass(centerOfMass);
 
    bod->setStandardForceCallback();
 
    // CONE
    // cone with a radius of 4.8, height of 10.0
    size = Ogre::Vector3( 10.0, 4.8, 4.8 );
 
    node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    ent = mSceneMgr->createEntity("crazy_cone_body", "cone.mesh" );
    node->attachObject( ent );
    node->setScale( size );
 
    // rigid body.
    coneCol = new OgreNewt::CollisionPrimitives::Cone(mWorld, 4.8, 5.0);
    col = OgreNewt::CollisionPtr(coneCol);
    bod = new OgreNewt::Body( mWorld, col );
    bod->attachNode( node );
 
    // initial position
    bod->setPositionOrientation( Ogre::Vector3(20,26,5), Ogre::Quaternion::IDENTITY );
 
    mass = 20.0;
    coneCol->calculateInertialMatrix(inertia, centerOfMass);
 
    inertia*=mass;    
 
    bod->setMassMatrix( mass, inertia );
    bod->setCenterOfMass(centerOfMass);
 
    bod->setCustomForceAndTorqueCallback<OgreNewtApplication>(&OgreNewtApplication::crazyCallback, this);
 
 
}
 
void OgreNewtApplication::createFrameListener()
{
    mFrameListener = new ExampleFrameListener( mWindow, mCamera );
    mRoot->addFrameListener( mFrameListener );
 
    OgreNewt::BasicFrameListener* mOgreNewtListener;
    mOgreNewtListener = new OgreNewt::BasicFrameListener( mWindow, mWorld );
    mRoot->addFrameListener( mOgreNewtListener );
 
}
 
void OgreNewtApplication::crazyCallback(OgreNewt::Body *body, float timeStep, int threadIndex)
{
    Ogre::Vector3 force(0,10,0), pointToApply(4,4,4);
    body->addLocalForce(force, pointToApply);
}
 
#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
      OgreNewtApplication app;
 
      try {
         app.go();
      } catch( Ogre::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().c_str() << std::endl;
#endif
      }
 
      return 0;
   }

 


Contributors to this page: jacmoe133512 points  and Deadvirus91 points  .
Page last modified on Sunday 27 of December, 2009 20:37:42 UTC by jacmoe133512 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.