查看: 1512|回复: 7

请问osgBullet平台如何搭建呢?

[复制链接]

该用户从未签到

发表于 2013-7-14 22:42:59 | 显示全部楼层 |阅读模式
照着demo例子的配置试了好久了,还是没有成功搭建自己的osgBullet平台,总是出现error LNK2019: 无法解析的外部符号,求搭建成功的大神们指点指点啊!不尽感激

该用户从未签到

发表于 2013-7-16 09:50:29 | 显示全部楼层
您没有给出足够的信息,我无法判断什么

该用户从未签到

 楼主| 发表于 2013-8-8 23:32:41 | 显示全部楼层
array 发表于 2013-7-16 09:50
您没有给出足够的信息,我无法判断什么

array你好!目前我的osgBullet环境已经搭建好了,但是把如下代码添加进osgBullet后就会出现图示错误,根据以往经验我怀疑是读取文件时路径错误,可是我又不知道如何把对应路径的文件正确添加进去(在没有使用osgBullet时我使用osg都是直接使用data文件夹中的模型文件的,但是现在我不知道这样做是不是还有效了),请您指点指点啊


未标题-1.jpg
  1. #include <osgDB/ReadFile>
  2. #include <osgViewer/Viewer>
  3. #include <osg/MatrixTransform>
  4. #include <osg/ShapeDrawable>
  5. #include <osg/Geode>

  6. #include <osgbDynamics/MotionState.h>
  7. #include <osgbCollision/CollisionShapes.h>
  8. #include <osgbDynamics/RigidBody.h>
  9. #include <osgbCollision/Utils.h>

  10. #include <btBulletDynamicsCommon.h>

  11. #include <string>
  12. #include <osg/io_utils>



  13. osg::MatrixTransform*
  14. makeDie( btDynamicsWorld* bw )
  15. {
  16.     osg::MatrixTransform* root = new osg::MatrixTransform;
  17.         //const std::string fileName( "dice.osg" );
  18.         const std::string fileName( "cow.osg" );
  19.     osg::Node* node = osgDB::readNodeFile( fileName );
  20.         if( node == NULL )
  21.         {
  22.                 osg::notify( osg::FATAL ) << "Can't find "" << fileName << "". Make sure OSG_FILE_PATH includes the osgBullet data directory." << std::endl;
  23.                 exit( 0 );
  24.         }
  25.     root->addChild( node );

  26.     btCollisionShape* cs = osgbCollision::btBoxCollisionShapeFromOSG( node );
  27.    
  28.     osg::ref_ptr< osgbDynamics::CreationRecord > cr = new osgbDynamics::CreationRecord;
  29.     cr->_sceneGraph = root;
  30.     cr->_shapeType = BOX_SHAPE_PROXYTYPE;
  31.     cr->_mass = 1.f;
  32.     cr->_restitution = 1.f;
  33.     btRigidBody* body = osgbDynamics::createRigidBody( cr.get(), cs );
  34.     bw->addRigidBody( body );

  35.     return( root );
  36. }


  37. btDynamicsWorld*
  38. initPhysics()
  39. {
  40.     btDefaultCollisionConfiguration * collisionConfiguration = new btDefaultCollisionConfiguration();
  41.     btCollisionDispatcher * dispatcher = new btCollisionDispatcher( collisionConfiguration );
  42.     btConstraintSolver * solver = new btSequentialImpulseConstraintSolver;

  43.     btVector3 worldAabbMin( -10000, -10000, -10000 );
  44.     btVector3 worldAabbMax( 10000, 10000, 10000 );
  45.     btBroadphaseInterface * inter = new btAxisSweep3( worldAabbMin, worldAabbMax, 1000 );

  46.     btDynamicsWorld * dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, inter, solver, collisionConfiguration );

  47.     dynamicsWorld->setGravity( btVector3( 0, 0, 9.8 ) );

  48.     return( dynamicsWorld );
  49. }


  50. /* \cond */
  51. class ShakeManipulator : public osgGA::GUIEventHandler
  52. {
  53. public:
  54.     ShakeManipulator( osgbDynamics::MotionState* motion )
  55.       : _motion( motion )
  56.     {}

  57.     bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
  58.     {
  59.         switch( ea.getEventType() )
  60.         {
  61.             case osgGA::GUIEventAdapter::KEYUP:
  62.             {
  63.                 if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Space)
  64.                 {
  65.                     btTransform trans; trans.setIdentity();
  66.                     _motion->setWorldTransform( trans );

  67.                     return true;
  68.                 }

  69.                 return false;
  70.             }

  71.             case osgGA::GUIEventAdapter::PUSH:
  72.             {
  73.                 _lastX = ea.getXnormalized();
  74.                 _lastY = ea.getYnormalized();

  75.                 btTransform world;
  76.                 _motion->getWorldTransform( world );
  77.                 btVector3 o = world.getOrigin();
  78.                 o[ 2 ] = 0.25;
  79.                 world.setOrigin( o );
  80.                 _motion->setWorldTransform( world );

  81.                 return true;
  82.             }
  83.             case osgGA::GUIEventAdapter::DRAG:
  84.             {
  85.                 btVector3 move;
  86.                 move[ 0 ] = _lastX - ea.getXnormalized();
  87.                 move[ 1 ] = ea.getYnormalized() - _lastY;
  88.                 move[ 2 ] = 0.;
  89.                 move *= 10.;
  90.                 btTransform moveTrans; moveTrans.setIdentity();
  91.                 moveTrans.setOrigin( move );
  92.                 btTransform world;
  93.                 _motion->getWorldTransform( world );
  94.                 btTransform netTrans = moveTrans * world;
  95.                 btVector3 o = netTrans.getOrigin();
  96.                 o[ 2 ] = 0.;
  97.                 netTrans.setOrigin( o );

  98.                 _motion->setWorldTransform( netTrans );

  99.                 _lastX = ea.getXnormalized();
  100.                 _lastY = ea.getYnormalized();

  101.                 return true;
  102.             }
  103.             default:
  104.             break;
  105.         }
  106.         return false;
  107.     }

  108. protected:
  109.     osgbDynamics::MotionState* _motion;
  110.     float _lastX, _lastY;
  111. };
  112. /* \endcond */




  113. osg::Geode* osgBox( const osg::Vec3& center, const osg::Vec3& halfLengths )
  114. {
  115.     osg::Vec3 l( halfLengths * 2. );
  116.     osg::Box* box = new osg::Box( center, l.x(), l.y(), l.z() );
  117.     osg::ShapeDrawable* shape = new osg::ShapeDrawable( box );
  118.     shape->setColor( osg::Vec4( 1., 1., 1., 1. ) );
  119.     osg::Geode* geode = new osg::Geode();
  120.     geode->addDrawable( shape );
  121.     return( geode );
  122. }



  123. int
  124. main( int argc,
  125.       char ** argv )
  126. {
  127.     btDynamicsWorld* bulletWorld = initPhysics();
  128.     osg::Group* root = new osg::Group;

  129.     root->addChild( makeDie( bulletWorld ) );
  130.     root->addChild( makeDie( bulletWorld ) );


  131.     /* BEGIN: Create environment boxes */
  132.     float xDim( 10. );
  133.     float yDim( 10. );
  134.     float zDim( 6. );
  135.     float thick( .1 );

  136.     osg::MatrixTransform* shakeBox = new osg::MatrixTransform;
  137.     btCompoundShape* cs = new btCompoundShape;
  138.     { // floor -Z (far back of the shake cube)
  139.         osg::Vec3 halfLengths( xDim*.5, yDim*.5, thick*.5 );
  140.         osg::Vec3 center( 0., 0., zDim*.5 );
  141.         shakeBox->addChild( osgBox( center, halfLengths ) );
  142.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  143.         btTransform trans; trans.setIdentity();
  144.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  145.         cs->addChildShape( trans, box );
  146.     }
  147.     { // top +Z (invisible, to allow user to see through; no OSG analogue
  148.         osg::Vec3 halfLengths( xDim*.5, yDim*.5, thick*.5 );
  149.         osg::Vec3 center( 0., 0., -zDim*.5 );
  150.         //shakeBox->addChild( osgBox( center, halfLengths ) );
  151.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  152.         btTransform trans; trans.setIdentity();
  153.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  154.         cs->addChildShape( trans, box );
  155.     }
  156.     { // left -X
  157.         osg::Vec3 halfLengths( thick*.5, yDim*.5, zDim*.5 );
  158.         osg::Vec3 center( -xDim*.5, 0., 0. );
  159.         shakeBox->addChild( osgBox( center, halfLengths ) );
  160.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  161.         btTransform trans; trans.setIdentity();
  162.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  163.         cs->addChildShape( trans, box );
  164.     }
  165.     { // right +X
  166.         osg::Vec3 halfLengths( thick*.5, yDim*.5, zDim*.5 );
  167.         osg::Vec3 center( xDim*.5, 0., 0. );
  168.         shakeBox->addChild( osgBox( center, halfLengths ) );
  169.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  170.         btTransform trans; trans.setIdentity();
  171.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  172.         cs->addChildShape( trans, box );
  173.     }
  174.     { // bottom of window -Y
  175.         osg::Vec3 halfLengths( xDim*.5, thick*.5, zDim*.5 );
  176.         osg::Vec3 center( 0., -yDim*.5, 0. );
  177.         shakeBox->addChild( osgBox( center, halfLengths ) );
  178.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  179.         btTransform trans; trans.setIdentity();
  180.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  181.         cs->addChildShape( trans, box );
  182.     }
  183.     { // bottom of window -Y
  184.         osg::Vec3 halfLengths( xDim*.5, thick*.5, zDim*.5 );
  185.         osg::Vec3 center( 0., yDim*.5, 0. );
  186.         shakeBox->addChild( osgBox( center, halfLengths ) );
  187.         btBoxShape* box = new btBoxShape( osgbCollision::asBtVector3( halfLengths ) );
  188.         btTransform trans; trans.setIdentity();
  189.         trans.setOrigin( osgbCollision::asBtVector3( center ) );
  190.         cs->addChildShape( trans, box );
  191.     }
  192.     /* END: Create environment boxes */

  193.     osgbDynamics::MotionState * shakeMotion = new osgbDynamics::MotionState();
  194.     shakeMotion->setTransform( shakeBox );
  195.     btScalar mass( 0.0 );
  196.     btVector3 inertia( 0, 0, 0 );
  197.     btRigidBody::btRigidBodyConstructionInfo rb( mass, shakeMotion, cs, inertia );
  198.     btRigidBody* shakeBody = new btRigidBody( rb );
  199.     shakeBody->setCollisionFlags( shakeBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
  200.     shakeBody->setActivationState( DISABLE_DEACTIVATION );
  201.     bulletWorld->addRigidBody( shakeBody );

  202.     root->addChild( shakeBox );

  203.     osgViewer::Viewer viewer;
  204.     viewer.setUpViewInWindow( 150, 150, 400, 400 );
  205.     viewer.setSceneData( root );
  206.     viewer.getCamera()->setViewMatrixAsLookAt(
  207.         osg::Vec3( 0, 0, -20 ), osg::Vec3( 0, 0, 0 ), osg::Vec3( 0, 1, 0 ) );
  208.     viewer.getCamera()->setProjectionMatrixAsPerspective( 40., 1., 1., 50. );
  209.     viewer.addEventHandler( new ShakeManipulator( shakeMotion ) );

  210.     viewer.realize();
  211.     double prevSimTime = 0.;
  212.     while( !viewer.done() )
  213.     {
  214.         const double currSimTime = viewer.getFrameStamp()->getSimulationTime();
  215.         double elapsed( currSimTime - prevSimTime );
  216.         if( viewer.getFrameStamp()->getFrameNumber() < 3 )
  217.             elapsed = 1./60.;
  218.         //osg::notify( osg::ALWAYS ) << elapsed / 3. << ", " << 1./180. << std::endl;
  219.         bulletWorld->stepSimulation( elapsed, 4, elapsed/4. );
  220.         prevSimTime = currSimTime;
  221.         viewer.frame();
  222.     }

  223.     return( 0 );
  224. }
复制代码

该用户从未签到

发表于 2013-8-9 08:16:38 | 显示全部楼层
自己一步一步调试吧  这么长的代码 我们猜不到啊

该用户从未签到

 楼主| 发表于 2013-8-9 12:18:04 | 显示全部楼层
liuzhiyu123 发表于 2013-8-9 08:16
自己一步一步调试吧  这么长的代码 我们猜不到啊

这段代码是我原文引用osgBullet例子里面的,在demo里面运行可以,可是复制到我自己搭建的osgBullet环境里面运行就会出错,但是调试时候还不报错

该用户从未签到

发表于 2013-8-9 16:33:15 | 显示全部楼层
不是 release 和 debug 版本的库 混用了吧

该用户从未签到

 楼主| 发表于 2013-8-10 22:30:59 | 显示全部楼层
liuzhiyu123 发表于 2013-8-9 16:33
不是 release 和 debug 版本的库 混用了吧

谢谢提醒!

该用户从未签到

 楼主| 发表于 2013-8-11 13:09:22 | 显示全部楼层
liuzhiyu123 发表于 2013-8-9 16:33
不是 release 和 debug 版本的库 混用了吧

这个问题解决了,可还是不行,出现这样的警告:
osgBullet.exe 中的 0x778e15de 处有未经处理的异常: 0xC0000005: 读取位置 0x00000004 时发生访问冲突
请问这个是不是模型读取路径问题呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表