当前位置: 首页 > news >正文

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; }



转载于:https://www.cnblogs.com/blfbuaa/p/6796444.html

相关文章:

  • Unity Shader-简单均值模糊
  • webpack-dev-server 设置反向代理解决跨域问题
  • CF364
  • jsp相关笔记(二)
  • CPU组成
  • 【Java并发编程】:加锁和volatile变量
  • expdp/impdp 参数说明,中英对照
  • 数据结构第11周笔记
  • for...in
  • 自学前端开发 新版css时钟效果图
  • UVA10129 Play on Words —— 欧拉回路
  • [Apio2012]dispatching 左偏树
  • 杭电1007-----C语言实现
  • 解决云服务器ECS,windows server 2012不能安装SQL Server 2012,不能安装.NET Fromework 3.5...
  • 自适应相关知识点1
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • CentOS 7 修改主机名
  • chrome扩展demo1-小时钟
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • Go 语言编译器的 //go: 详解
  • javascript 哈希表
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • PaddlePaddle-GitHub的正确打开姿势
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Transformer-XL: Unleashing the Potential of Attention Models
  • Vue2.x学习三:事件处理生命周期钩子
  • webgl (原生)基础入门指南【一】
  • 分享一份非常强势的Android面试题
  • 聊聊hikari连接池的leakDetectionThreshold
  • 适配mpvue平台的的微信小程序日历组件mpvue-calendar
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 主流的CSS水平和垂直居中技术大全
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • # 数论-逆元
  • #{} 和 ${}区别
  • #162 (Div. 2)
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (层次遍历)104. 二叉树的最大深度
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (算法)N皇后问题
  • (转)visual stdio 书签功能介绍
  • .NET Remoting学习笔记(三)信道
  • .net下简单快捷的数值高低位切换
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • .ui文件相关
  • /bin/rm: 参数列表过长"的解决办法
  • @Documented注解的作用
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录