利用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. 大规模SOA系统中的分布事务思考

    首先是不建议采用XA两阶段提交方式去处理分布式事务,要知道要能够支持XA分布式事务,必须是要实现XA规范才可以,而Service本身是无状态的,如果这样去做了等于是把Service内部的东西暴露了出去 ...

  2. Java-动态规划-最多苹果数量的方法

    平面上有N*M个格子,每个格子中放着一定数量的苹果.你从左上角的格子开始,每一步只能向下走或是向右走,每次走到一个格子上就把格子里的苹果收集起来,这样下去,你最多能收集到多少个苹果. 思路: 解这个问 ...

  3. SharePoint 2013 App 开发—App开发概述

    基于安全性的考虑,SharePoint App 不能像其它两种方式一样,直接使用安全性更高的服务端代码的API.Javascript 扮演极为重要的角色,在SharePoint App中与ShareP ...

  4. net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件

    原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  5. wpf LookUpEdit PopupContentTemplate

    <dxg:LookUpEdit Name="searchLookUpEdit" HorizontalAlignment="Stretch" PopupHe ...

  6. LeetCode OJ——Word Break

    http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...

  7. js-随机生成16进制颜色

    <body onload="color()"></body> <script> function color(){ 方法一: document. ...

  8. React-Native解决ListView 在Android手机上无吸顶效果

    stickySectionHeadersEnabled={true} stickyHeaderIndices={[0]}

  9. pycharm上传代码到码云错误现象用户密码

    >>出现此时错误说明在pycharm>git登录用户名或密码是错误的(必须填成是自己注册的码云邮箱和密码 不允许出现中文)并且无法在当前修改用户名或密码 >>接下来打开电 ...

  10. (BruteForce)暴力破解经典题目总结

    在算法竞赛中,很多问题是来不及用数学公式推导出来的.或者说根本就找不到数学规律,这时我们就需要使用枚举来暴力破解. 不过枚举也是需要脑子的,一味的暴力只能超时.因此我这里选择了几道mooc上经典的题目 ...