利用Bullet物理引擎实现刚体的自由落体模拟的模板

Bullet下载地址

Main.cpp

#include <GLUT/glut.h>
#include <cstdlib> /* for exit */
#include <vector>
#include "btBulletDynamicsCommon.h"
#include "BulletCollision/Gimpact/btGImpactShape.h"
#include "BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h" using namespace std; float zoom = 2000.f;
float rotx = 20;
float roty = 0;
float tx = 0;
float ty = 0;
int lastx=0;
int lasty=0;
unsigned char Buttons[3] = {0};
float lightPosition[] = { -200, 300, 300, 1.0f};
float ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
float diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
float specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; btDiscreteDynamicsWorld* mp_btDynamicsWorld = NULL;
btRigidBody *cube = NULL;
btRigidBody *ground = NULL; void InitWorld()
{
btDefaultCollisionConfiguration *config = new btDefaultCollisionConfiguration();
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(config); btDbvtBroadphase *broadphase = new btDbvtBroadphase(); // Dynamic AABB tree method
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
mp_btDynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, config);
mp_btDynamicsWorld->setGravity(btVector3(0, -9800, 0)); //millimeter 9.8 * 1000
} void InitObject()
{
//init cube
btCollisionShape *collisionShape = new btBoxShape(btVector3(200,200,200));
//initial position
btVector3 pos = btVector3(0, 600, 0);
btQuaternion qrot(0, 0, 0, 1); btDefaultMotionState* motion_state = new btDefaultMotionState(btTransform(qrot, pos)); btScalar mass = btScalar(10);
btVector3 inertia = btVector3(0, 0, 0);//guan xing
collisionShape->calculateLocalInertia(mass, inertia); cube = new btRigidBody(mass, motion_state, collisionShape, inertia); btScalar restitution = btScalar(0);
cube->setRestitution(restitution);
//default 0.5
btScalar friction = btScalar(0.8);
cube->setFriction(friction); mp_btDynamicsWorld->addRigidBody(cube); //init ground
btCollisionShape *groundShape = new btBoxShape(btVector3(1000,0.5,1000)); //half size btVector3 groundpos = btVector3(0,0,0);
btQuaternion groundrot(0, 0, 0, 1);
btDefaultMotionState* groundMotion = new btDefaultMotionState(btTransform(groundrot, groundpos));
ground = new btRigidBody(0.0, groundMotion, groundShape);//mass = 0 means it is a static object
btScalar rest = btScalar(1);
ground->setRestitution(rest);
mp_btDynamicsWorld->addRigidBody(ground);
} void DeleteBullet()
{
//cube
delete cube->getMotionState();
mp_btDynamicsWorld->removeRigidBody(cube);
delete cube;
cube = NULL; //ground
delete ground->getMotionState();
mp_btDynamicsWorld->removeRigidBody(ground);
delete ground;
ground = NULL; //world
delete mp_btDynamicsWorld->getBroadphase();
delete mp_btDynamicsWorld;
mp_btDynamicsWorld = NULL;
} void DrawGrid(int _halfLen, int _gridNum)
{
glColor3f(1.0f,1.0f,1.0f); // draw grid
glLineWidth(2);
glBegin(GL_LINES);
for(int i = -_halfLen;i <= _halfLen; i += (_halfLen/_gridNum)) {
glVertex3f(i,0,-_halfLen);
glVertex3f(i,0,_halfLen); glVertex3f(_halfLen,0,i);
glVertex3f(-_halfLen,0,i);
}
glEnd(); }
void DrawCoordinate(float _flengthX, float _flengthY, float _flengthZ)
{
glLineWidth(5);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(_flengthX,0,0);
glEnd(); glBegin(GL_LINES);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,_flengthY,0);
glEnd(); glBegin(GL_LINES);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,_flengthZ);
glEnd();
} void DrawBulletObject()
{
//cube
btTransform trans = cube->getWorldTransform();
btScalar m[16];
trans.getOpenGLMatrix(m);
glColor3f(0, 0, 1);
glPushMatrix();
glMultMatrixf((GLfloat*)m);
glutSolidCube(400);
glPopMatrix(); //ground
glColor3f(0, 1, 0);
glPushMatrix();
glScalef(1, 0.0005, 1);
glutSolidCube(2000); //size
glPopMatrix(); } void Simulate()
{
double dt = 1.f/60.0f;
if(mp_btDynamicsWorld)
mp_btDynamicsWorld->stepSimulation(dt,1);
}
//-------------------------------------------------------------------------------
///
void Display()
{
Simulate(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0,-zoom);
glTranslatef(tx,ty,0);
glRotatef(rotx,1,0,0);
glRotatef(roty,0,1,0); glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT1, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition); glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL); DrawBulletObject();
glDisable( GL_LIGHTING );
glDisable(GL_COLOR_MATERIAL); //DrawGrid(1000, 10);
//DrawCoordinate(1000,1000,1000); glutPostRedisplay();
glutSwapBuffers();
} void Init()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnable(GL_NORMALIZE); InitWorld();
InitObject();
} void Reshape(int w, int h)
{
// prevent divide by 0 error when minimised
if(w==0)
h = 1; glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(float)w/h,0.1,5000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} //-------------------------------------------------------------------------------
//
void Motion(int x,int y)
{
int diffx=x-lastx;
int diffy=y-lasty;
lastx=x;
lasty=y; if( Buttons[2] )
{
zoom -= (float) 1* diffx*2;
}
else
if( Buttons[0] )
{
rotx += (float) 1 * diffy;
roty += (float) 1 * diffx;
}
else
if( Buttons[1] )
{
tx += (float) 1 * diffx;
ty -= (float) 1 * diffy;
}
glutPostRedisplay();
} //-------------------------------------------------------------------------------
//
void Mouse(int b,int s,int x,int y)
{
lastx=x;
lasty=y;
switch(b)
{
case GLUT_LEFT_BUTTON:
Buttons[0] = ((GLUT_DOWN==s)?1:0);
break;
case GLUT_MIDDLE_BUTTON:
Buttons[1] = ((GLUT_DOWN==s)? 1:0);
break;
case GLUT_RIGHT_BUTTON:
Buttons[2] = ((GLUT_DOWN==s)?1:0);
break;
default:
break;
}
glutPostRedisplay();
} void Keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 'q':
case 'Q':
case 27: // ESC key
exit(0);
break;
case 'r':
DeleteBullet();
InitWorld();
InitObject();
break;
default:
break;
}
} int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("Bullet Framework");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutKeyboardFunc(Keyboard);
Init(); glutMainLoop(); return 0;
}

Bullet Physics OpenGL 刚体应用程序模板 Rigid Simulation in Bullet的更多相关文章

  1. 转:Bullet物理引擎不完全指南(Bullet Physics Engine not complete Guide)

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 前言 Bullet据称为游戏世界占有率为第三的物理引擎,也是前几大引擎目前唯一能够 ...

  2. 学习基于OpenGL的CAD程序的开发计划(一)

    本人目前从事的工作面对的客户中很多来自高端制造业,他们对CAD/CAE/CAM软件的应用比较多.公司现有的软件产品主要是用于渲染展示及交互,但面对诸如CAD方面的应用(比如基于约束的装配.制造工艺的流 ...

  3. 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序

    三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...

  4. 【原创】前端开发人员如何制作微信小程序模板

    (我的博客网站中的原文:http://www.xiaoxianworld.com/archives/305,欢迎遇到的小伙伴常来瞅瞅,给点评论和建议,有错误和不足,也请指出.) 最近接触了一下微信小程 ...

  5. 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法

    主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...

  6. opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上

    1.问题:opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上 2.环境:vc6.0  win7,64位,opengl. 3.解决:将glut32.dl ...

  7. 微信小程序模板消息群发解决思路

    基于微信的通知渠道,微信为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.(微信6.5.2及以上版本支持模板功能.低于该版本将无法收到模板消息.) 模板推送位置:服务通 ...

  8. 【RTOS】基于V7开发板的最新版uCOS-III V3.07.03程序模板,含MDK和IAR,支持uC/Probe,与之前版本变化较大

    模板下载: 链接:https://pan.baidu.com/s/1_4z_Lg51jMT87RrRM6Qs3g   提取码:2gns 对MDK的AC6也做了支持:https://www.cnblog ...

  9. 【RTOS】基于V7开发板的最新版uCOS-II V2.92.16程序模板,含MDK和IAR,支持uC/Probe

    模板下载: 链接:https://pan.baidu.com/s/10a9Hi0MD14obR_B1LAQEFA     提取码:z76n 1.MDK使用MDK5.26及其以上版本. 2.IAR使用I ...

随机推荐

  1. Codeforces989E. A Trance of Nightfall

    $n \leq 200$个平面上的点,$q \leq 200$次询问:重复操作$m \leq 10000$次,到达点$x$的概率最大是多少.操作:一开始选点$P$,不一定要是给定点,可以是平面上任一点 ...

  2. 在 POSIX 线程编程中避免内存泄漏

    检测和避免 POSIX 线程内存泄漏的技巧 POSIX 线程(pthread)编程定义了一套标准的 C 编程语言类型.函数和常量 — 且 pthreads 提供了一种强大的线程管理工具.要充分使用 p ...

  3. Servlet 2.4 规范之第五篇:请求

    request对象封装了来自客户端的所有请求信息.在HTTP协议中,客户端发给服务端的所有信息都是通过request对象的请求头和请求体来传送的.           SRV.4.1    HTTP协 ...

  4. js-jquery 中$.ajax -浅显接触

    工作了将近2年,终于开始自己写ajax了!!!真紧张的! 当年培训时就没有学ajax,就让我们自己看看,我是那种主动学习的人吗?不是!!!所以搞不懂ajax!!!!! 在工作中,数据的绑定我们之前都是 ...

  5. Codeforces 932 A.Palindromic Supersequence (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    占坑,明天写,想把D补出来一起写.2/20/2018 11:17:00 PM ----------------------------------------------------------我是分 ...

  6. [转载][FPGA]有限状态机FSM学习笔记(二)

    1. Mealy和Moore状态机的互换 对于给定的时序逻辑功能,可以用Mealy机实现,也可以用Moore机实现.根据Moore机比Mealy机输出落后一个周期的特性,可以实现两种状态机之间的转换. ...

  7. android-samples-mvp

    Model–view–presenter (MVP)介绍 mvp在wiki上的介绍为 Model  定义用户界面所需要被显示的数据模型,一个模型包含着相关的业务逻辑 View  View不应该处理业务 ...

  8. [NSURL URLWithString:] 返回nil

    具体问题原因是url中输入的有中文,那么这个就看作非法的字符无法识别.这种的必须使用post方式来发送消息.具体为: tmp = mainurl;            [parameters app ...

  9. 【转】Ubuntu下出现Mysql error(2002)的解决方法

    过了一阵子后,为了写分布式作业,重新使用Mysql时,发现虽然启动成功了,但是连接的时候去出现如下错误ERROR 2002 (HY000): Can't connect to local MySQL ...

  10. Python3.2官方文档翻译--作用域和命名空间实例

    6.2.1 作用域和命名空间实例 以下的实例主要用来示范怎样引用不同的作用域和命名空间,keywordglobal和nonlocalru怎样影响变量绑定. 实例执行结果是: After local a ...