Files
bullet3/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp
Erwin Coumans 68f798a2da Start re-organizing demos so the physics setup can be shared easier (explicit create graphics objects, init/exit physics etc)
Add B3G_RETURN key code, only implemented in Windows so far (todo: Mac, Linux)
Fix Windows key management (use WM_CHAR event instead of WM_KEYUP
Add Return (OnKeyReturn) key support TreeNode, so we can select an item using the return key.
2014-06-24 10:14:06 -07:00

276 lines
5.9 KiB
C++

/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#define CUBE_HALF_EXTENTS 1
#define EXTRA_HEIGHT 1.f
#include "CcdPhysicsDemo.h"
#include "GlutStuff.h"
#include "GLDebugFont.h"
///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
#include "btBulletDynamicsCommon.h"
#include <stdio.h> //printf debugging
#include "GLDebugDrawer.h"
#if 0
extern btAlignedObjectArray<btVector3> debugContacts;
extern btAlignedObjectArray<btVector3> debugNormals;
#endif
static GLDebugDrawer sDebugDrawer;
CcdPhysicsDemo::CcdPhysicsDemo()
:m_ccdMode(USE_CCD)
{
setDebugMode(btIDebugDraw::DBG_DrawText+btIDebugDraw::DBG_NoHelpText);
setCameraDistance(btScalar(40.));
}
void CcdPhysicsDemo::clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//simple dynamics world doesn't handle fixed-time-stepping
//float ms = getDeltaTimeMicroseconds();
///step the simulation
if (m_dynamicsWorld)
{
m_dynamicsWorld->stepSimulation(1./60.,0);//ms / 1000000.f);
//optional but useful: debug drawing
m_dynamicsWorld->debugDrawWorld();
}
renderme();
displayText();
#if 0
for (int i=0;i<debugContacts.size();i++)
{
getDynamicsWorld()->getDebugDrawer()->drawContactPoint(debugContacts[i],debugNormals[i],0,0,btVector3(1,0,0));
}
#endif
glFlush();
swapBuffers();
}
void CcdPhysicsDemo::displayText()
{
int lineWidth=440;
int xStart = m_glutScreenWidth - lineWidth;
int yStart = 20;
if((getDebugMode() & btIDebugDraw::DBG_DrawText)!=0)
{
setOrthographicProjection();
glDisable(GL_LIGHTING);
glColor3f(0, 0, 0);
char buf[124];
glRasterPos3f(xStart, yStart, 0);
switch (m_ccdMode)
{
case USE_CCD:
{
sprintf(buf,"Predictive contacts and motion clamping");
break;
}
case USE_NO_CCD:
{
sprintf(buf,"CCD handling disabled");
break;
}
default:
{
sprintf(buf,"unknown CCD setting");
};
};
GLDebugDrawString(xStart,20,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"Press 'p' to change CCD mode");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"Press '.' or right mouse to shoot bullets");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"space to restart, h(elp), t(ext), w(ire)");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
resetPerspectiveProjection();
glEnable(GL_LIGHTING);
}
}
void CcdPhysicsDemo::displayCallback(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderme();
displayText();
//optional but useful: debug drawing to detect problems
if (m_dynamicsWorld)
{
m_dynamicsWorld->debugDrawWorld();
}
#if 0
for (int i=0;i<debugContacts.size();i++)
{
getDynamicsWorld()->getDebugDrawer()->drawContactPoint(debugContacts[i],debugNormals[i],0,0,btVector3(1,0,0));
}
#endif
glFlush();
swapBuffers();
}
void CcdPhysicsDemo::initPhysics()
{
setTexturing(true);
setShadows(true);
m_ShootBoxInitialSpeed = 4000.f;
m_defaultContactProcessingThreshold = 0.f;
GraphicsPhysicsBridge bridge;
m_physicsSetup.initPhysics(bridge);
m_dynamicsWorld = m_physicsSetup.m_dynamicsWorld;
m_dynamicsWorld->getSolverInfo().m_solverMode |=SOLVER_USE_2_FRICTION_DIRECTIONS|SOLVER_RANDMIZE_ORDER;
m_dynamicsWorld ->setDebugDrawer(&sDebugDrawer);
//m_dynamicsWorld->getSolverInfo().m_splitImpulse=false;
if (m_ccdMode==USE_CCD)
{
m_dynamicsWorld->getDispatchInfo().m_useContinuous=true;
} else
{
m_dynamicsWorld->getDispatchInfo().m_useContinuous=false;
}
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
}
void CcdPhysicsDemo::clientResetScene()
{
exitPhysics();
initPhysics();
}
void CcdPhysicsDemo::keyboardCallback(unsigned char key, int x, int y)
{
if (key=='p')
{
switch (m_ccdMode)
{
case USE_CCD:
{
m_ccdMode = USE_NO_CCD;
break;
}
case USE_NO_CCD:
default:
{
m_ccdMode = USE_CCD;
}
};
clientResetScene();
} else
{
DemoApplication::keyboardCallback(key,x,y);
}
}
void CcdPhysicsDemo::shootBox(const btVector3& destination)
{
if (m_dynamicsWorld)
{
float mass = 1.f;
btTransform startTransform;
startTransform.setIdentity();
btVector3 camPos = getCameraPosition();
startTransform.setOrigin(camPos);
setShootBoxShape ();
btRigidBody* body = this->localCreateRigidBody(mass, startTransform,m_shootBoxShape);
body->setLinearFactor(btVector3(1,1,1));
//body->setRestitution(1);
btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]);
linVel.normalize();
linVel*=m_ShootBoxInitialSpeed;
body->getWorldTransform().setOrigin(camPos);
body->getWorldTransform().setRotation(btQuaternion(0,0,0,1));
body->setLinearVelocity(linVel);
body->setAngularVelocity(btVector3(0,0,0));
body->setContactProcessingThreshold(1e30);
///when using m_ccdMode, disable regular CCD
if (m_ccdMode==USE_CCD)
{
body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
body->setCcdSweptSphereRadius(0.4f);
}
}
}
void CcdPhysicsDemo::exitPhysics()
{
m_physicsSetup.exitPhysics();
}