|
楼主 |
发表于 2013-8-8 23:32:41
|
显示全部楼层
array 发表于 2013-7-16 09:50
您没有给出足够的信息,我无法判断什么
array你好!目前我的osgBullet环境已经搭建好了,但是把如下代码添加进osgBullet后就会出现图示错误,根据以往经验我怀疑是读取文件时路径错误,可是我又不知道如何把对应路径的文件正确添加进去(在没有使用osgBullet时我使用osg都是直接使用data文件夹中的模型文件的,但是现在我不知道这样做是不是还有效了),请您指点指点啊
- #include <osgDB/ReadFile>
- #include <osgViewer/Viewer>
- #include <osg/MatrixTransform>
- #include <osg/ShapeDrawable>
- #include <osg/Geode>
- #include <osgbDynamics/MotionState.h>
- #include <osgbCollision/CollisionShapes.h>
- #include <osgbDynamics/RigidBody.h>
- #include <osgbCollision/Utils.h>
- #include <btBulletDynamicsCommon.h>
- #include <string>
- #include <osg/io_utils>
- osg::MatrixTransform*
- makeDie( btDynamicsWorld* bw )
- {
- osg::MatrixTransform* root = new osg::MatrixTransform;
- //const std::string fileName( "dice.osg" );
- const std::string fileName( "cow.osg" );
- osg::Node* node = osgDB::readNodeFile( fileName );
- if( node == NULL )
- {
- osg::notify( osg::FATAL ) << "Can't find "" << fileName << "". Make sure OSG_FILE_PATH includes the osgBullet data directory." << std::endl;
- exit( 0 );
- }
- root->addChild( node );
- btCollisionShape* cs = osgbCollision::btBoxCollisionShapeFromOSG( node );
-
- osg::ref_ptr< osgbDynamics::CreationRecord > cr = new osgbDynamics::CreationRecord;
- cr->_sceneGraph = root;
- cr->_shapeType = BOX_SHAPE_PROXYTYPE;
- cr->_mass = 1.f;
- cr->_restitution = 1.f;
- btRigidBody* body = osgbDynamics::createRigidBody( cr.get(), cs );
- bw->addRigidBody( body );
- return( root );
- }
- btDynamicsWorld*
- initPhysics()
- {
- btDefaultCollisionConfiguration * collisionConfiguration = new btDefaultCollisionConfiguration();
- btCollisionDispatcher * dispatcher = new btCollisionDispatcher( collisionConfiguration );
- btConstraintSolver * solver = new btSequentialImpulseConstraintSolver;
- btVector3 worldAabbMin( -10000, -10000, -10000 );
- btVector3 worldAabbMax( 10000, 10000, 10000 );
- btBroadphaseInterface * inter = new btAxisSweep3( worldAabbMin, worldAabbMax, 1000 );
- btDynamicsWorld * dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, inter, solver, collisionConfiguration );
- dynamicsWorld->setGravity( btVector3( 0, 0, 9.8 ) );
- return( dynamicsWorld );
- }
- /* \cond */
- class ShakeManipulator : public osgGA::GUIEventHandler
- {
- public:
- ShakeManipulator( osgbDynamics::MotionState* motion )
- : _motion( motion )
- {}
- bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
- {
- switch( ea.getEventType() )
- {
- case osgGA::GUIEventAdapter::KEYUP:
- {
- if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Space)
- {
- btTransform trans; trans.setIdentity();
- _motion->setWorldTransform( trans );
- return true;
- }
- return false;
- }
- case osgGA::GUIEventAdapter::PUSH:
- {
- _lastX = ea.getXnormalized();
- _lastY = ea.getYnormalized();
- btTransform world;
- _motion->getWorldTransform( world );
- btVector3 o = world.getOrigin();
- o[ 2 ] = 0.25;
- world.setOrigin( o );
- _motion->setWorldTransform( world );
- return true;
- }
- case osgGA::GUIEventAdapter::DRAG:
- {
- btVector3 move;
- move[ 0 ] = _lastX - ea.getXnormalized();
- move[ 1 ] = ea.getYnormalized() - _lastY;
- move[ 2 ] = 0.;
- move *= 10.;
- btTransform moveTrans; moveTrans.setIdentity();
- moveTrans.setOrigin( move );
- btTransform world;
- _motion->getWorldTransform( world );
- btTransform netTrans = moveTrans * world;
- btVector3 o = netTrans.getOrigin();
- o[ 2 ] = 0.;
- netTrans.setOrigin( o );
- _motion->setWorldTransform( netTrans );
- _lastX = ea.getXnormalized();
- _lastY = ea.getYnormalized();
- return true;
- }
- default:
- break;
- }
- return false;
- }
- protected:
- osgbDynamics::MotionState* _motion;
- float _lastX, _lastY;
- };
- /* \endcond */
- osg::Geode* osgBox( const osg::Vec3& center, const osg::Vec3& halfLengths )
- {
- osg::Vec3 l( halfLengths * 2. );
- osg::Box* box = new osg::Box( center, l.x(), l.y(), l.z() );
- osg::ShapeDrawable* shape = new osg::ShapeDrawable( box );
- shape->setColor( osg::Vec4( 1., 1., 1., 1. ) );
- osg::Geode* geode = new osg::Geode();
- geode->addDrawable( shape );
- return( geode );
- }
- int
- main( int argc,
- char ** argv )
- {
- btDynamicsWorld* bulletWorld = initPhysics();
- osg::Group* root = new osg::Group;
- root->addChild( makeDie( bulletWorld ) );
- root->addChild( makeDie( bulletWorld ) );
- /* BEGIN: Create environment boxes */
- float xDim( 10. );
- float yDim( 10. );
- float zDim( 6. );
- float thick( .1 );
- osg::MatrixTransform* shakeBox = new osg::MatrixTransform;
- btCompoundShape* cs = new btCompoundShape;
- { // floor -Z (far back of the shake cube)
- osg::Vec3 halfLengths( xDim*.5, yDim*.5, thick*.5 );
- osg::Vec3 center( 0., 0., zDim*.5 );
- shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- { // top +Z (invisible, to allow user to see through; no OSG analogue
- osg::Vec3 halfLengths( xDim*.5, yDim*.5, thick*.5 );
- osg::Vec3 center( 0., 0., -zDim*.5 );
- //shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- { // left -X
- osg::Vec3 halfLengths( thick*.5, yDim*.5, zDim*.5 );
- osg::Vec3 center( -xDim*.5, 0., 0. );
- shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- { // right +X
- osg::Vec3 halfLengths( thick*.5, yDim*.5, zDim*.5 );
- osg::Vec3 center( xDim*.5, 0., 0. );
- shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- { // bottom of window -Y
- osg::Vec3 halfLengths( xDim*.5, thick*.5, zDim*.5 );
- osg::Vec3 center( 0., -yDim*.5, 0. );
- shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- { // bottom of window -Y
- osg::Vec3 halfLengths( xDim*.5, thick*.5, zDim*.5 );
- osg::Vec3 center( 0., yDim*.5, 0. );
- shakeBox->addChild( osgBox( center, halfLengths ) );
- btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
- btTransform trans; trans.setIdentity();
- trans.setOrigin( osgbCollision::asBtVector3( center ) );
- cs->addChildShape( trans, box );
- }
- /* END: Create environment boxes */
- osgbDynamics::MotionState * shakeMotion = new osgbDynamics::MotionState();
- shakeMotion->setTransform( shakeBox );
- btScalar mass( 0.0 );
- btVector3 inertia( 0, 0, 0 );
- btRigidBody::btRigidBodyConstructionInfo rb( mass, shakeMotion, cs, inertia );
- btRigidBody* shakeBody = new btRigidBody( rb );
- shakeBody->setCollisionFlags( shakeBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
- shakeBody->setActivationState( DISABLE_DEACTIVATION );
- bulletWorld->addRigidBody( shakeBody );
- root->addChild( shakeBox );
- osgViewer::Viewer viewer;
- viewer.setUpViewInWindow( 150, 150, 400, 400 );
- viewer.setSceneData( root );
- viewer.getCamera()->setViewMatrixAsLookAt(
- osg::Vec3( 0, 0, -20 ), osg::Vec3( 0, 0, 0 ), osg::Vec3( 0, 1, 0 ) );
- viewer.getCamera()->setProjectionMatrixAsPerspective( 40., 1., 1., 50. );
- viewer.addEventHandler( new ShakeManipulator( shakeMotion ) );
- viewer.realize();
- double prevSimTime = 0.;
- while( !viewer.done() )
- {
- const double currSimTime = viewer.getFrameStamp()->getSimulationTime();
- double elapsed( currSimTime - prevSimTime );
- if( viewer.getFrameStamp()->getFrameNumber() < 3 )
- elapsed = 1./60.;
- //osg::notify( osg::ALWAYS ) << elapsed / 3. << ", " << 1./180. << std::endl;
- bulletWorld->stepSimulation( elapsed, 4, elapsed/4. );
- prevSimTime = currSimTime;
- viewer.frame();
- }
- return( 0 );
- }
复制代码 |
|