mirror of
https://github.com/bulletphysics/bullet3
synced 2025-01-10 17:30:12 +00:00
2db8bfa3d6
Thanks Jay for reporting Added braking capability to btRaycastVehicle, see Bullet/Demos/VehicleDemo/VehicleDemo.cpp Added glutKeyboardUpFunc, for vehicle demo (keep accelerating/breaking, until key released/UP). Hope this is compatible with most GLUT implementations.
566 lines
15 KiB
C++
566 lines
15 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.
|
|
*/
|
|
|
|
/// September 2006: VehicleDemo is work in progress, this file is mostly just a placeholder
|
|
/// This VehicleDemo file is very early in development, please check it later
|
|
/// One todo is a basic engine model:
|
|
/// A function that maps user input (throttle) into torque/force applied on the wheels
|
|
/// with gears etc.
|
|
#include "btBulletDynamicsCommon.h"
|
|
|
|
//
|
|
// By default, Bullet Vehicle uses Y as up axis.
|
|
// You can override the up axis, for example Z-axis up. Enable this define to see how to:
|
|
//#define FORCE_ZAXIS_UP 1
|
|
//
|
|
|
|
#ifdef FORCE_ZAXIS_UP
|
|
int rightIndex = 0;
|
|
int upIndex = 2;
|
|
int forwardIndex = 1;
|
|
btVector3 wheelDirectionCS0(0,0,-1);
|
|
btVector3 wheelAxleCS(1,0,0);
|
|
#else
|
|
int rightIndex = 0;
|
|
int upIndex = 1;
|
|
int forwardIndex = 2;
|
|
btVector3 wheelDirectionCS0(0,-1,0);
|
|
btVector3 wheelAxleCS(-1,0,0);
|
|
#endif
|
|
|
|
#include "GLDebugDrawer.h"
|
|
#include <stdio.h> //printf debugging
|
|
|
|
#include "GL_ShapeDrawer.h"
|
|
|
|
#include "GlutStuff.h"
|
|
#include "VehicleDemo.h"
|
|
|
|
const int maxProxies = 32766;
|
|
const int maxOverlap = 65535;
|
|
|
|
///btRaycastVehicle is the interface for the constraint that implements the raycast vehicle
|
|
///notice that for higher-quality slow-moving vehicles, another approach might be better
|
|
///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts
|
|
float gEngineForce = 0.f;
|
|
float gBreakingForce = 0.f;
|
|
|
|
float maxEngineForce = 1000.f;//this should be engine/velocity dependent
|
|
float maxBreakingForce = 100.f;
|
|
|
|
float gVehicleSteering = 0.f;
|
|
float steeringIncrement = 0.04f;
|
|
float steeringClamp = 0.3f;
|
|
float wheelRadius = 0.5f;
|
|
float wheelWidth = 0.4f;
|
|
float wheelFriction = 1000;//1e30f;
|
|
float suspensionStiffness = 20.f;
|
|
float suspensionDamping = 2.3f;
|
|
float suspensionCompression = 4.4f;
|
|
float rollInfluence = 0.1f;//1.0f;
|
|
|
|
|
|
btScalar suspensionRestLength(0.6);
|
|
|
|
#define CUBE_HALF_EXTENTS 1
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
|
|
|
|
GLDebugDrawer debugDrawer;
|
|
|
|
int main(int argc,char** argv)
|
|
{
|
|
|
|
VehicleDemo* vehicleDemo = new VehicleDemo;
|
|
|
|
vehicleDemo->setupPhysics();
|
|
|
|
return glutmain(argc, argv,640,480,"Bullet Vehicle Demo. http://www.continuousphysics.com/Bullet/phpBB2/", vehicleDemo);
|
|
}
|
|
|
|
VehicleDemo::VehicleDemo()
|
|
:
|
|
m_carChassis(0),
|
|
m_cameraHeight(4.f),
|
|
m_minCameraDistance(3.f),
|
|
m_maxCameraDistance(10.f)
|
|
{
|
|
m_vehicle = 0;
|
|
m_cameraPosition = btVector3(30,30,30);
|
|
}
|
|
|
|
void VehicleDemo::setupPhysics()
|
|
{
|
|
|
|
extern btScalar gJitterVelocityDampingFactor;
|
|
gJitterVelocityDampingFactor = 1.f;
|
|
|
|
#ifdef FORCE_ZAXIS_UP
|
|
m_cameraUp = btVector3(0,0,1);
|
|
m_forwardAxis = 1;
|
|
#endif
|
|
|
|
btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
|
|
btCollisionDispatcher* dispatcher = new btCollisionDispatcher();
|
|
btVector3 worldMin(-1000,-1000,-1000);
|
|
btVector3 worldMax(1000,1000,1000);
|
|
btOverlappingPairCache* pairCache = new btAxisSweep3(worldMin,worldMax);
|
|
btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver();
|
|
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver);
|
|
#ifdef FORCE_ZAXIS_UP
|
|
m_dynamicsWorld->setGravity(btVector3(0,0,-10));
|
|
#endif
|
|
m_dynamicsWorld->setDebugDrawer(&debugDrawer);
|
|
|
|
|
|
#define USE_TRIMESH_GROUND 1
|
|
#ifdef USE_TRIMESH_GROUND
|
|
int i;
|
|
|
|
const float TRIANGLE_SIZE=20.f;
|
|
|
|
//create a triangle-mesh ground
|
|
int vertStride = sizeof(btVector3);
|
|
int indexStride = 3*sizeof(int);
|
|
|
|
const int NUM_VERTS_X = 50;
|
|
const int NUM_VERTS_Y = 50;
|
|
const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
|
|
|
|
const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1);
|
|
|
|
btVector3* gVertices = new btVector3[totalVerts];
|
|
int* gIndices = new int[totalTriangles*3];
|
|
|
|
|
|
|
|
for ( i=0;i<NUM_VERTS_X;i++)
|
|
{
|
|
for (int j=0;j<NUM_VERTS_Y;j++)
|
|
{
|
|
float wl = .2f;
|
|
//height set to zero, but can also use curved landscape, just uncomment out the code
|
|
float height = 0.f;//20.f*sinf(float(i)*wl)*cosf(float(j)*wl);
|
|
#ifdef FORCE_ZAXIS_UP
|
|
gVertices[i+j*NUM_VERTS_X].setValue(
|
|
(i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,
|
|
(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE,
|
|
height
|
|
);
|
|
|
|
#else
|
|
gVertices[i+j*NUM_VERTS_X].setValue(
|
|
(i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,
|
|
height,
|
|
(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
|
|
#endif
|
|
|
|
}
|
|
}
|
|
|
|
int index=0;
|
|
for ( i=0;i<NUM_VERTS_X-1;i++)
|
|
{
|
|
for (int j=0;j<NUM_VERTS_Y-1;j++)
|
|
{
|
|
gIndices[index++] = j*NUM_VERTS_X+i;
|
|
gIndices[index++] = j*NUM_VERTS_X+i+1;
|
|
gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
|
|
|
|
gIndices[index++] = j*NUM_VERTS_X+i;
|
|
gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
|
|
gIndices[index++] = (j+1)*NUM_VERTS_X+i;
|
|
}
|
|
}
|
|
|
|
btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
|
|
gIndices,
|
|
indexStride,
|
|
totalVerts,(btScalar*) &gVertices[0].x(),vertStride);
|
|
|
|
bool useQuantizedAabbCompression = true;
|
|
groundShape = new btBvhTriangleMeshShape(indexVertexArrays,useQuantizedAabbCompression);
|
|
|
|
#endif //
|
|
|
|
btTransform tr;
|
|
tr.setIdentity();
|
|
|
|
tr.setOrigin(btVector3(0,-4.5f,0));
|
|
|
|
//create ground object
|
|
localCreateRigidBody(0,tr,groundShape);
|
|
|
|
#ifdef FORCE_ZAXIS_UP
|
|
// indexRightAxis = 0;
|
|
// indexUpAxis = 2;
|
|
// indexForwardAxis = 1;
|
|
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f));
|
|
btCompoundShape* compound = new btCompoundShape();
|
|
btTransform localTrans;
|
|
localTrans.setIdentity();
|
|
//localTrans effectively shifts the center of mass with respect to the chassis
|
|
localTrans.setOrigin(btVector3(0,0,1));
|
|
#else
|
|
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));
|
|
btCompoundShape* compound = new btCompoundShape();
|
|
btTransform localTrans;
|
|
localTrans.setIdentity();
|
|
//localTrans effectively shifts the center of mass with respect to the chassis
|
|
localTrans.setOrigin(btVector3(0,1,0));
|
|
#endif
|
|
|
|
compound->addChildShape(localTrans,chassisShape);
|
|
|
|
tr.setOrigin(btVector3(0,0.f,0));
|
|
|
|
m_carChassis = localCreateRigidBody(800,tr,compound);//chassisShape);
|
|
//m_carChassis->setDamping(0.2,0.2);
|
|
|
|
|
|
clientResetScene();
|
|
|
|
/// create vehicle
|
|
{
|
|
|
|
m_vehicleRayCaster = new btDefaultVehicleRaycaster(m_dynamicsWorld);
|
|
m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster);
|
|
|
|
///never deactivate the vehicle
|
|
m_carChassis->setActivationState(DISABLE_DEACTIVATION);
|
|
|
|
m_dynamicsWorld->addVehicle(m_vehicle);
|
|
|
|
float connectionHeight = 1.2f;
|
|
|
|
|
|
bool isFrontWheel=true;
|
|
|
|
//choose coordinate system
|
|
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);
|
|
|
|
#ifdef FORCE_ZAXIS_UP
|
|
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
|
|
#else
|
|
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);
|
|
#endif
|
|
|
|
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
|
|
#ifdef FORCE_ZAXIS_UP
|
|
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
|
|
#else
|
|
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);
|
|
#endif
|
|
|
|
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
|
|
#ifdef FORCE_ZAXIS_UP
|
|
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
|
|
#else
|
|
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius);
|
|
#endif //FORCE_ZAXIS_UP
|
|
isFrontWheel = false;
|
|
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
|
|
#ifdef FORCE_ZAXIS_UP
|
|
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
|
|
#else
|
|
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius);
|
|
#endif
|
|
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
|
|
|
|
for (int i=0;i<m_vehicle->getNumWheels();i++)
|
|
{
|
|
btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
|
|
wheel.m_suspensionStiffness = suspensionStiffness;
|
|
wheel.m_wheelsDampingRelaxation = suspensionDamping;
|
|
wheel.m_wheelsDampingCompression = suspensionCompression;
|
|
wheel.m_frictionSlip = wheelFriction;
|
|
wheel.m_rollInfluence = rollInfluence;
|
|
}
|
|
}
|
|
|
|
|
|
setCameraDistance(26.f);
|
|
|
|
}
|
|
|
|
|
|
//to be implemented by the demo
|
|
void VehicleDemo::renderme()
|
|
{
|
|
|
|
updateCamera();
|
|
|
|
debugDrawer.setDebugMode(getDebugMode());
|
|
btScalar m[16];
|
|
int i;
|
|
|
|
btCylinderShapeX wheelShape(btVector3(wheelWidth,wheelRadius,wheelRadius));
|
|
btVector3 wheelColor(1,0,0);
|
|
|
|
for (i=0;i<m_vehicle->getNumWheels();i++)
|
|
{
|
|
//synchronize the wheels with the (interpolated) chassis worldtransform
|
|
m_vehicle->updateWheelTransform(i,true);
|
|
//draw wheels (cylinders)
|
|
m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
|
|
GL_ShapeDrawer::drawOpenGL(m,&wheelShape,wheelColor,getDebugMode());
|
|
}
|
|
|
|
|
|
DemoApplication::renderme();
|
|
|
|
}
|
|
|
|
void VehicleDemo::clientMoveAndDisplay()
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
{
|
|
int wheelIndex = 2;
|
|
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
|
|
m_vehicle->setBrake(gBreakingForce,wheelIndex);
|
|
wheelIndex = 3;
|
|
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
|
|
m_vehicle->setBrake(gBreakingForce,wheelIndex);
|
|
|
|
|
|
wheelIndex = 0;
|
|
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
|
|
wheelIndex = 1;
|
|
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
|
|
|
|
}
|
|
|
|
|
|
float dt = m_clock.getTimeMicroseconds() * 0.000001f;
|
|
m_clock.reset();
|
|
if (m_dynamicsWorld)
|
|
{
|
|
//during idle mode, just run 1 simulation step maximum
|
|
int maxSimSubSteps = m_idle ? 1 : 2;
|
|
if (m_idle)
|
|
dt = 1.0/420.f;
|
|
|
|
int numSimSteps = m_dynamicsWorld->stepSimulation(dt,maxSimSubSteps);
|
|
|
|
//#define VERBOSE_FEEDBACK
|
|
#ifdef VERBOSE_FEEDBACK
|
|
if (!numSimSteps)
|
|
printf("Interpolated transforms\n");
|
|
else
|
|
{
|
|
if (numSimSteps > maxSimSubSteps)
|
|
{
|
|
//detect dropping frames
|
|
printf("Dropped (%i) simulation steps out of %i\n",numSimSteps - maxSimSubSteps,numSimSteps);
|
|
} else
|
|
{
|
|
printf("Simulated (%i) steps\n",numSimSteps);
|
|
}
|
|
}
|
|
#endif //VERBOSE_FEEDBACK
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_QUICKPROF
|
|
btProfiler::beginBlock("render");
|
|
#endif //USE_QUICKPROF
|
|
|
|
|
|
renderme();
|
|
|
|
#ifdef USE_QUICKPROF
|
|
btProfiler::endBlock("render");
|
|
#endif
|
|
|
|
|
|
glFlush();
|
|
glutSwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VehicleDemo::displayCallback(void)
|
|
{
|
|
|
|
clientMoveAndDisplay();
|
|
return;
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
//m_dynamicsWorld->updateAabbs();
|
|
|
|
//draw contactpoints
|
|
//m_physicsEnvironmentPtr->CallbackTriggers();
|
|
|
|
|
|
//renderme();
|
|
|
|
|
|
glFlush();
|
|
glutSwapBuffers();
|
|
}
|
|
|
|
|
|
|
|
void VehicleDemo::clientResetScene()
|
|
{
|
|
gVehicleSteering = 0.f;
|
|
m_carChassis->setCenterOfMassTransform(btTransform::getIdentity());
|
|
m_carChassis->setLinearVelocity(btVector3(0,0,0));
|
|
m_carChassis->setAngularVelocity(btVector3(0,0,0));
|
|
m_dynamicsWorld->getBroadphase()->cleanProxyFromPairs(m_carChassis->getBroadphaseHandle());
|
|
if (m_vehicle)
|
|
{
|
|
m_vehicle->resetSuspension();
|
|
for (int i=0;i<m_vehicle->getNumWheels();i++)
|
|
{
|
|
//synchronize the wheels with the (interpolated) chassis worldtransform
|
|
m_vehicle->updateWheelTransform(i,true);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VehicleDemo::specialKeyboardUp(int key, int x, int y)
|
|
{
|
|
switch (key)
|
|
{
|
|
case GLUT_KEY_UP :
|
|
{
|
|
gEngineForce = 0.f;
|
|
break;
|
|
}
|
|
case GLUT_KEY_DOWN :
|
|
{
|
|
gBreakingForce = 0.f;
|
|
break;
|
|
}
|
|
default:
|
|
DemoApplication::specialKeyboardUp(key,x,y);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void VehicleDemo::specialKeyboard(int key, int x, int y)
|
|
{
|
|
|
|
// printf("key = %i x=%i y=%i\n",key,x,y);
|
|
|
|
switch (key)
|
|
{
|
|
case GLUT_KEY_LEFT :
|
|
{
|
|
gVehicleSteering += steeringIncrement;
|
|
if ( gVehicleSteering > steeringClamp)
|
|
gVehicleSteering = steeringClamp;
|
|
|
|
break;
|
|
}
|
|
case GLUT_KEY_RIGHT :
|
|
{
|
|
gVehicleSteering -= steeringIncrement;
|
|
if ( gVehicleSteering < -steeringClamp)
|
|
gVehicleSteering = -steeringClamp;
|
|
|
|
break;
|
|
}
|
|
case GLUT_KEY_UP :
|
|
{
|
|
gEngineForce = maxEngineForce;
|
|
break;
|
|
}
|
|
case GLUT_KEY_DOWN :
|
|
{
|
|
gBreakingForce = maxBreakingForce;
|
|
break;
|
|
}
|
|
default:
|
|
DemoApplication::specialKeyboard(key,x,y);
|
|
break;
|
|
}
|
|
|
|
// glutPostRedisplay();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void VehicleDemo::updateCamera()
|
|
{
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
|
|
btTransform chassisWorldTrans;
|
|
|
|
//look at the vehicle
|
|
m_carChassis->getMotionState()->getWorldTransform(chassisWorldTrans);
|
|
m_cameraTargetPosition = chassisWorldTrans.getOrigin();
|
|
|
|
//interpolate the camera height
|
|
#ifdef FORCE_ZAXIS_UP
|
|
m_cameraPosition[2] = (15.0*m_cameraPosition[2] + m_cameraTargetPosition[2] + m_cameraHeight)/16.0;
|
|
#else
|
|
m_cameraPosition[1] = (15.0*m_cameraPosition[1] + m_cameraTargetPosition[1] + m_cameraHeight)/16.0;
|
|
#endif
|
|
|
|
btVector3 camToObject = m_cameraTargetPosition - m_cameraPosition;
|
|
|
|
//keep distance between min and max distance
|
|
float cameraDistance = camToObject.length();
|
|
float correctionFactor = 0.f;
|
|
if (cameraDistance < m_minCameraDistance)
|
|
{
|
|
correctionFactor = 0.15*(m_minCameraDistance-cameraDistance)/cameraDistance;
|
|
}
|
|
if (cameraDistance > m_maxCameraDistance)
|
|
{
|
|
correctionFactor = 0.15*(m_maxCameraDistance-cameraDistance)/cameraDistance;
|
|
}
|
|
m_cameraPosition -= correctionFactor*camToObject;
|
|
|
|
//update OpenGL camera settings
|
|
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10000.0);
|
|
|
|
gluLookAt(m_cameraPosition[0],m_cameraPosition[1],m_cameraPosition[2],
|
|
m_cameraTargetPosition[0],m_cameraTargetPosition[1], m_cameraTargetPosition[2],
|
|
m_cameraUp.getX(),m_cameraUp.getY(),m_cameraUp.getZ());
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
}
|
|
|