bullet_01
#include <btBulletDynamicsCommon.h>
#include <osgViewer/Viewer>
#include <map>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib") class BulletInterface : public osg::Referenced
{
public:
static BulletInterface * Instance();
btDiscreteDynamicsWorld * GetScene() {return _scene;} void CreateWorld(const osg::Plane & plane, const osg::Vec3 & gravity);
void CreateBox(int id, const osg::Vec3 & dim, double mass);
void CreateSphere(int id, double radius, double mass);
void SetVelocity(int id, const osg::Vec3 & pos);
void SetMatrix(int id, const osg::Matrix & matrix);
osg::Matrix GetMatrix(int id); void Simulate(double step);
protected:
BulletInterface();
virtual ~BulletInterface(); typedef std::map<int , btRigidBody *> ActorMap;
ActorMap _actors;
btDiscreteDynamicsWorld * _scene;
btDefaultCollisionConfiguration * _configuration;
btCollisionDispatcher * _dispatcher;
btBroadphaseInterface * _overlappingPairCache;
btSequentialImpulseConstraintSolver * _solver;
}; BulletInterface * BulletInterface::Instance()
{
static osg::ref_ptr<BulletInterface> s_registry = new BulletInterface;
return s_registry.get();
} void BulletInterface::CreateWorld( const osg::Plane & plane, const osg::Vec3 & gravity )
{
_scene = new btDiscreteDynamicsWorld(_dispatcher, _overlappingPairCache, _solver, _configuration);
_scene->setGravity(btVector3(gravity[], gravity[], gravity[])); osg::Vec3 normal = plane.getNormal();
btCollisionShape * groundShape = new btStaticPlaneShape(btVector3(normal[], normal[], normal[]), plane[]); btTransform groundTransform;
groundTransform.setIdentity(); btDefaultMotionState * motionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rigidInfo(0.0, motionState, groundShape, btVector3(0.0, 0.0, 0.0));
btRigidBody * body = new btRigidBody(rigidInfo);
_scene->addRigidBody(body);
} void BulletInterface::CreateBox( int id, const osg::Vec3 & dim, double mass )
{
btCollisionShape * boxShape = new btBoxShape(btVector3(dim[], dim[], dim[]));
btTransform boxTransform;
boxTransform.setIdentity(); btVector3 localInertia(0.0, 0.0, 0.0);
if (mass > 0.0)
{
boxShape->calculateLocalInertia(mass, localInertia);
} btDefaultMotionState * motionState = new btDefaultMotionState(boxTransform);
btRigidBody::btRigidBodyConstructionInfo rigidInfo(mass, motionState, boxShape, localInertia);
btRigidBody * body = new btRigidBody(rigidInfo);
_scene->addRigidBody(body);
_actors[id] = body;
} void BulletInterface::CreateSphere( int id, double radius, double mass )
{
btCollisionShape * sphereShape = new btSphereShape(radius); btTransform sphereTransform;
sphereTransform.setIdentity(); btVector3 localInertia(0.0, 0.0, 0.0);
if (mass > 0.0)
{
sphereShape->calculateLocalInertia(mass, localInertia);
}
btDefaultMotionState * motionState = new btDefaultMotionState(sphereTransform);
btRigidBody::btRigidBodyConstructionInfo rigidInfo(mass, motionState, sphereShape, localInertia);
btRigidBody * body = new btRigidBody(rigidInfo);
_scene->addRigidBody(body);
_actors[id] = body;
} void BulletInterface::SetVelocity( int id, const osg::Vec3 & pos )
{
btRigidBody * actor = _actors[id];
if (actor)
{
actor->setLinearVelocity(btVector3(pos._v[], pos._v[], pos._v[]));
}
} void BulletInterface::SetMatrix( int id, const osg::Matrix & matrix )
{
btRigidBody * actor = _actors[id];
if (actor)
{
btTransform trans;
trans.setFromOpenGLMatrix(osg::Matrixf(matrix).ptr());
actor->setWorldTransform(trans);
}
} osg::Matrix BulletInterface::GetMatrix( int id )
{
btRigidBody * actor = _actors[id];
if (actor)
{
btTransform trans = actor->getWorldTransform();
osg::Matrixf matrix;
trans.getOpenGLMatrix(matrix.ptr());
return matrix;
}
return osg::Matrix();
} void BulletInterface::Simulate( double step )
{
_scene->stepSimulation(step, );
} BulletInterface::BulletInterface()
:_scene(NULL)
{
_configuration = new btDefaultCollisionConfiguration;
_dispatcher = new btCollisionDispatcher(_configuration);
_overlappingPairCache = new btDbvtBroadphase;
_solver = new btSequentialImpulseConstraintSolver;
} BulletInterface::~BulletInterface()
{
if (_scene)
{
for (int i = _scene->getNumCollisionObjects() - ; i >= ; --i)
{
btCollisionObject * obj = _scene->getCollisionObjectArray()[i];
btRigidBody * body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
delete body->getMotionState();
}
_scene->removeCollisionObject(obj);
delete obj;
}
delete _scene;
}
delete _solver;
delete _overlappingPairCache;
delete _dispatcher;
delete _configuration;
} class SampleRigidUpdater : public osgGA::GUIEventHandler
{
public:
SampleRigidUpdater(osg::Group * root)
:_root(root)
{ } void AddGround(const osg::Vec3 & gravity)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -0.5f), 100.0f, 100.0f, 1.0f))); osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
mt->addChild(geode.get());
_root->addChild(mt.get()); BulletInterface::Instance()->CreateWorld(osg::Plane(0.0f, 0.0f, 1.0f, 0.0f), gravity);
} void AddPhysicsBox(osg::Box * shape, const osg::Vec3 & pos, const osg::Vec3 & vel, double mass)
{
int id = _physicsNodes.size();
BulletInterface::Instance()->CreateBox(id, shape->getHalfLengths(), mass);
AddPhysicsData(id, shape, pos, vel, mass);
} void AddPhysicsSphere(osg::Sphere * sphere, const osg::Vec3 & pos, const osg::Vec3 & vel, double mass)
{
int id = _physicsNodes.size();
BulletInterface::Instance()->CreateSphere(id, sphere->getRadius(), mass);
AddPhysicsData(id, sphere, pos, vel, mass);
} bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::View * view = dynamic_cast<osgViewer::Viewer *>(&aa);
if (!view || !_root)
{
return false;
}
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYUP:
{
osg::Vec3 eye, center, up, dir;
view->getCamera()->getViewMatrixAsLookAt(eye, center, up);
dir = center - eye;
dir.normalize();
AddPhysicsSphere(new osg::Sphere(osg::Vec3(), 0.5f), eye, dir * 60.0f, 2.0);
}
break;
case osgGA::GUIEventAdapter::FRAME:
{
BulletInterface::Instance()->Simulate(0.02);
for (NodeMap::iterator iter = _physicsNodes.begin(); iter != _physicsNodes.end(); ++iter)
{
osg::Matrix matrix = BulletInterface::Instance()->GetMatrix(iter->first);
iter->second->setMatrix(matrix);
}
}
break;
default:
break;
}
return false;
}
protected:
void AddPhysicsData(int id, osg::Shape * shape, const osg::Vec3 & pos, const osg::Vec3 & vel, double mass)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(shape));
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
mt->addChild(geode.get());
_root->addChild(mt.get());
BulletInterface::Instance()->SetMatrix(id, osg::Matrix::translate(pos));
BulletInterface::Instance()->SetVelocity(id, vel);
_physicsNodes[id] = mt;
} protected:
typedef std::map<int, osg::observer_ptr<osg::MatrixTransform> > NodeMap;
NodeMap _physicsNodes;
osg::observer_ptr<osg::Group > _root;
};
int main()
{
int mode = ; osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osgGA::GUIEventHandler> updater;
switch ( mode )
{
case :
{
SampleRigidUpdater* rigidUpdater = new SampleRigidUpdater( root.get() );
rigidUpdater->AddGround( osg::Vec3(0.0f, 0.0f,-9.8f) );
for ( unsigned int i=; i<; ++i )
{
for ( unsigned int j=; j<; ++j )
{
rigidUpdater->AddPhysicsBox( new osg::Box(osg::Vec3(), 0.99f),
osg::Vec3((float)i, 0.0f, (float)j+0.5f), osg::Vec3(), 1.0f );
}
}
updater = rigidUpdater;
}
break;
case :
break;
default: break;
} osgViewer::Viewer viewer;
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
viewer.addEventHandler( new osgViewer::StatsHandler );
viewer.addEventHandler( new osgViewer::WindowSizeHandler );
if ( updater.valid() )
viewer.addEventHandler( updater.get() );
viewer.setSceneData( root.get() );
return viewer.run();
}
bullet_01的更多相关文章
随机推荐
- TypeScript --  面向对象特性
			
.class关键字和类名就可以定义一个类 . 类的访问控制符--有三个,.] = ] = ] = ;.声明参数 .用接口声明方法 .理解模块--一个文件就是一个模块,就是这么个意思 ,不用想的多么高大 ...
 - C# LogHelper
			
using System; using log4net; using log4net.Config; namespace Utils { /// <summary> /// 日志帮助类(l ...
 - jvm的垃圾回收几种理解
			
1.引用计数器回收 给每个对象设置一个计数器,当该对象被引用时,计数器加1,当有其他变量不再引用该对象时,计数器减1.直到计数器数值为0,回收器视为他是‘垃圾’,可以被回收,当该对象被回收时,其他引用 ...
 - 遍历json创建树状表(首先的前提条件是要引入jquery的jquery treeTable插件)
			
"root":{ "children":[ { "name":"AA", "children":[ ...
 - java中的反射机制_____
			
一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义. 反射是java中一种强大的工具,能够使 ...
 - error C3872: “0x3000”: 此字符不允许在标识符中使用
			
主要是拷贝的程序问题,有错误的空字符. 0x3000是汉语的空格,也就是全角空格,相当于一个汉字,但你又看不见它. 你知道的,像逗号,有半角(,)和全角(,)之分的,其实空格也有. 0x3000是全角 ...
 - 向量相加CUDA练习
			
#include<string.h> #include<math.h> #include<stdlib.h> #include<stdio.h> #de ...
 - .net后台代码临时表创建
			
写法一: var dt = new DataTable(); dt.Columns.Add(new DataColumn("Id", System.Type.GetType(&qu ...
 - 集成shareSDK的微信、QQ API导致cocoaPods找不到类symbol问题的解决方法
			
因为shareSDK的微信和QQ API都只支持32位的,而cocoaPods默认要支持64位的,所以如果在工程中导入这两个API会出问题. 解决方法我就不转载啦,原文在这里: http://blog ...
 - nsstring遍历汉子
			
NSString *mytimestr=@"好人一生平安"; size_t length = [mytimestr length]; ; i < length; i++) { ...