|
我想实现一个简单的小方块在重力作用下,掉下来的过程,但在OSG和Physx的结合中碰到了问题,编译没有问题,链接有问题,一到回调函数中physx代码,就提示说gSence错误,不知道是什么原因- #define NOMINMAX
- #include <osg/ref_ptr>
- #include <osg/Group>
- #include <osgViewer/Viewer>
- #include <osgDB/ReadFile>
- #include <osg/MatrixTransform>
- #include <osg/NodeCallback>
- #include <osg/Camera>
- #include <osgGA/TrackballManipulator>
- #include <osg/PositionAttitudeTransform>
- #include <osg/ShapeDrawable>
- #include <iostream>
- #include <windows.h>
- #include <GL/gl.h>
- #include <GL/glut.h>
- //physx
- #include "NxPhysics.h"
- //physx
- static NxPhysicsSDK *gPhysicsSDK=NULL;
- static NxScene *gScene=NULL;
- static NxVec3 gDefaultGravity(0.0f,-9.8f,0.0f);
- static bool InitNx()
- {
- //初始化 PhysicsSDK
- gPhysicsSDK=NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION,0,NULL);
- if(!gPhysicsSDK)
- return false;
- gPhysicsSDK->setParameter(NX_SKIN_WIDTH,0.05f);
- //创建一场景
- NxSceneDesc sceneDesc;
- sceneDesc.gravity=gDefaultGravity;
- gScene=gPhysicsSDK->createScene(sceneDesc);
- NxMaterial *defaultMaterial=gScene->getMaterialFromIndex(0);
- defaultMaterial->setRestitution(0.9f);
- defaultMaterial->setStaticFriction(0.1f);
- defaultMaterial->setDynamicFriction(0.1f);
- //创建一个刚体
- int size=5;
- int radius=3;
- NxBodyDesc BodyDesc;
- BodyDesc.angularDamping=0.5f;
- BodyDesc.linearVelocity=NxVec3(2.0f,0.0f,0.0f);
- //创建一个方块
- NxBoxShapeDesc BoxDesc;
- BoxDesc.dimensions=NxVec3(float(size),float(size),float(size));
- NxActorDesc BoxActorDesc;
- BoxActorDesc.shapes.pushBack(&BoxDesc);
- BoxActorDesc.body=&BodyDesc;
- BoxActorDesc.density=0.1f;
- BoxActorDesc.globalPose.t=NxVec3(10.0,20.0,0.0);
- gScene->createActor(BoxActorDesc)->userData = (void*)size;
- }
- class Movemtbox : public osg::NodeCallback
- {
- public:
-
- virtual void operator()( osg::Node* node,osg::NodeVisitor* nv )
- {
-
- osg::MatrixTransform* mtBox =dynamic_cast<osg::MatrixTransform*>( node );
- gScene->fetchResults(NX_RIGID_BODY_FINISHED);
- gScene->simulate(1/420.0f);//一帧多少时间
- gScene->flushStream();
- //在下面这一行出错,总提示gScene读取内存位置时发生错误,百思不得其解
- int nbActors = gScene->getNbActors();//得到场景中角色的数量
- NxActor** actors = gScene->getActors();//得到场景中所有角色的队列
- NxActor* actor = *actors++;
- float glmat[16];
- actor->getGlobalPose().getColumnMajor44(glmat);//得到角色的世界位置,并转化到glmat中,是3*3矩阵
-
- osg::Matrix mat;
- mat.set(glmat[0],glmat[1],glmat[2],glmat[3],glmat[4],glmat[5],glmat[6],glmat[7],glmat[8],glmat[9],glmat[10],glmat[11],glmat[12],glmat[13],glmat[14],glmat[15]);
- mtBox->setMatrix(mat);
- traverse(node,nv);
- }
-
- protected:
-
-
- };
- osg::ref_ptr<osg::Node> createScene()
- {
- osg::Group* root = new osg::Group();
-
- osg::Box* unitCube = new osg::Box( osg::Vec3(0,0,0), 1.0f);
- osg::ShapeDrawable* unitCubeDrawable = new osg::ShapeDrawable(unitCube);
- osg::Geode* basicShapesGeode = new osg::Geode();
- basicShapesGeode->addDrawable(unitCubeDrawable);
- osg::ref_ptr<osg::MatrixTransform> mtBox =new osg::MatrixTransform;
- mtBox->setDataVariance( osg::Object::DYNAMIC );
- mtBox->setUpdateCallback( new Movemtbox );
- mtBox->addChild( basicShapesGeode );
- root->addChild(mtBox.get());
- return root;
- }
-
- int main(int argc, char** argv)
- {
-
- //physx
- InitNx();
-
- //osg
- osgViewer::Viewer viewer;
- viewer.setSceneData( createScene().get() );
- if (!viewer.getSceneData())
- return 1;
- viewer.getCamera()->setClearColor(osg::Vec4( 0.3, 0.4, 0.5, 1. ) );
-
-
- if(gPhysicsSDK && gScene)
- {
- gPhysicsSDK->releaseScene(*gScene);
- }
- gPhysicsSDK->release();
-
- return viewer.run();
- }
复制代码 |
|