Bullet Physics OpenGL 刚体应用程序模板 Rigid Simulation in Bullet
利用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的更多相关文章
- 转:Bullet物理引擎不完全指南(Bullet Physics Engine not complete Guide)
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 前言 Bullet据称为游戏世界占有率为第三的物理引擎,也是前几大引擎目前唯一能够 ...
- 学习基于OpenGL的CAD程序的开发计划(一)
本人目前从事的工作面对的客户中很多来自高端制造业,他们对CAD/CAE/CAM软件的应用比较多.公司现有的软件产品主要是用于渲染展示及交互,但面对诸如CAD方面的应用(比如基于约束的装配.制造工艺的流 ...
- 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序
三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...
- 【原创】前端开发人员如何制作微信小程序模板
(我的博客网站中的原文:http://www.xiaoxianworld.com/archives/305,欢迎遇到的小伙伴常来瞅瞅,给点评论和建议,有错误和不足,也请指出.) 最近接触了一下微信小程 ...
- 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法
主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...
- opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上
1.问题:opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上 2.环境:vc6.0 win7,64位,opengl. 3.解决:将glut32.dl ...
- 微信小程序模板消息群发解决思路
基于微信的通知渠道,微信为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.(微信6.5.2及以上版本支持模板功能.低于该版本将无法收到模板消息.) 模板推送位置:服务通 ...
- 【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 ...
- 【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 ...
随机推荐
- svg优雅降级技术
这是一个名叫Alexey Ten首先提出来的,类似下面的代码: <svg width="96" height="96"> <image xli ...
- DataSet的Merge方法合并两张表
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] UniqueConstraint uc = new UniqueConstraint("pk" ...
- 【Visual Studio】error: /ZI”和“/Gy-”命令行选项不兼容(转)
原文转自 http://bbs.100home.net/view/4206.html [问题说明]vc6项目用vs2015打开时遇到的错误 [解决方法]项目属性->c/c++->常规-&g ...
- Lua开发环境搭建(Mac OS X)
1. 安装Rudix Rudix: http://rudix.org curl -O https://raw.githubusercontent.com/rudix-mac/rpm/2015.4/ru ...
- Linux 之 文件内容查看
文件内容查看 参考教程:[千峰教育] 文件内容查看: cat: 作用:一次性顺序显示文件的所有内容 格式:cat file tac: 作用:一次性倒序显示文件的所有内容 格式:tac file hea ...
- Java后端WebSocket的Tomcat实现 html5 WebSocket 实时聊天
WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据.Tomcat7.0.47上才能运行. 需要添加Tomcat里lib目 ...
- Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...
- 如果当前地图文档中有独立的Table,通过Engine如何获取该Table?
将IMap转为ITableCollection,通过ITableCollection.get_Table(int index);来获取该Table
- Unity -- 入门教程一
首先声明一下,我用的Unity版本是4.6.6,编译环境是VS2010,其余的我会慢慢介绍,安装的过程这里我就不做讲解了,度娘那会做的比我详细.安装包可以在最下面的联系方式找我要,现在开始进入主题. ...
- 基于GPU加速的三维空间分析【转】
基于GPU加速的三维空间分析 标签:supermap地理信息系统gisit 文:李凯 随着三维GIS 的快速发展和应用普及,三维空间分析技术以其应用中的实用性成为当前GIS技术研究的热点领域.面对日益 ...