实现一个简单的加载物理效果的ogre程序:
为方便以后的工程设置,我直接在 Tools -> Options -> Projects and Solutions -> VC++ Directories 下的 Include files/Library files作如下设置:
包含:
$(OGRENEWT_HOME)/inc
$(NEWTON_HOME)/sdk
库:
$(OGRENEWT_HOME)/lib
$(NEWTON_HOME)/sdk/x32/dll_vs9
现在开始一个新的Ogre/Newton程序:
1、对已经存在模型Ogre工程进行设置:
Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies :
OgreNewt.lib newton.lib (Release mode)
OgreNewt_d.lib newton.lib (Debug mode)
2、在工程头文件,比如 myOgreNewtApp.h:
#include <OgreNewt.h>
#include <OgreNewt_BasicFrameListener.h>
3、添加private成员变量:
OgreNewt::World* mWorld;
4、得到/销毁newton world:
//构造写入:
mWorld = new OgreNewt::World();
//析构写入
delete mWorld;
5、在createScene中:
//创建newton监听器,更新newton world中的bodies:
OgreNewt::BasicFrameListener* mOgreNewtListener;
mOgreNewtListener = new OgreNewt::BasicFrameListener( mWindow, mWorld, 120 );
mRoot->addFrameListener( mOgreNewtListener );
//*********************设置下落物体,本例对一个Box(Box02)设置************************//
//定义地面,承受物体,这里简单采用ogre中已存在的一个盒子(Box02),其节点名也为(Box02);
Ogre::Vector3 size(10.0,1.0,10.0);
Ogre::SceneNode* node = mSceneMgr->getSceneNode("Box02");
node->setScale( size );
//设置碰撞检测
OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::Box( mWorld, size, 0 ));
OgreNewt::Body* floorbody = new OgreNewt::Body( mWorld, col );
floorbody->attachNode( node );
//将地面下调5单位,让程序一启动物体下落到地面
floorbody->setPositionOrientation( Ogre::Vector3(0,-5,0), Ogre::Quaternion::IDENTITY );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
delete col;
#endif
//*********************设置下落物体,本例对一个锥体(Cone01)设置************************//
//定义物体,采用ogre中已存在的一个锥体(Cone01),其节点名也为(Cone01);
size = Ogre::Vector3( 1.3, 0.5, 0.5 );
col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::Cone( mWorld, 33,36, 0));
OgreNewt::Body* bod = new OgreNewt::Body( mWorld, col );
node = mSceneMgr->getSceneNode("Cone01");
node->setScale( size );
// 基于质量和形体尺寸的惯性计算.
Ogre::Real mass = size.x * size.y * size.z * 2.5;
Ogre::Vector3 inertia, offset;
col->calculateInertialMatrix(inertia, offset);
bod->attachNode( node );
bod->setPositionOrientation( Ogre::Vector3(-2,3,2), Ogre::Quaternion::IDENTITY );
bod->setMassMatrix( mass, mass*inertia );
bod->setCenterOfMass(offset);
//运用重力:
bod->setStandardForceCallback();
//设置下落速度(加速度)
Ogre::Vector3 dir, vec;
Ogre::Quaternion camorient = mCamera->getDerivedOrientation();
vec = Ogre::Vector3(0,0,-1);
dir = camorient * vec;
bod->setVelocity( (dir * 9.8) );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
delete col;
#endif
6、编绎,通过;
7、运行,建立运行环境:
需加入相应的dll文件。
OgreNewt.dll / OgreNewt_d.dll
newton.dll
dJointLibrary.dll / dJointLibrary_d.dll
8、效果,在程序启动后一个锥体自动下落到地面上。