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 ...
随机推荐
- 方格取数(hdu 1565)
Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数 ...
- jrebel 激活
jrebel idea插件激活,亲测可用: 在jrebel server处,写上: http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b ...
- git超详细教程【转】
转自:http://blog.csdn.net/liuwengai/article/details/52072344 GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流 ...
- Java 基础【04】数组内存分配
可能Java 数组大家都很熟悉,最近我遇到了一个关于Java 数组内存分配的问题. 突然发现许多书上“基本数据类型存储在栈内存当中,对象则保存在堆内存”这句话完全是错误的.下面是个简单的例子代码: p ...
- 51nod 1088 最长回文子串 【中心拓展法/输出长度和路径】
1088 最长回文子串 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字 ...
- 洛谷—— P1407 工资
https://www.luogu.org/problemnew/show/P1407 题目描述 有一家世界级大企业,他们经过调查,发现了一个奇特的现象,竟然在自己的公司里,有超过一半的雇员,他们的工 ...
- Careercup | Chapter 3
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- 网络流24T 太空飞行计划问题
题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的 ...
- IntelliJ IDEA删除所有断点
参考: http://blog.csdn.net/yanziit/article/details/73459795