2015-04-16 16:55:32 +00:00
/*
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
///@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"
# include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
# include "BulletDynamics/MLCPSolvers/btDantzigSolver.h"
# include "BulletDynamics/MLCPSolvers/btSolveProjectedGaussSeidel.h"
# include "BulletDynamics/MLCPSolvers/btMLCPSolver.h"
class btVehicleTuning ;
struct btVehicleRaycaster ;
class btCollisionShape ;
# include "BulletDynamics/Vehicle/btRaycastVehicle.h"
# include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
# include "BulletDynamics/ConstraintSolver/btSliderConstraint.h"
2015-04-29 20:33:26 +00:00
# include "../CommonInterfaces/CommonExampleInterface.h"
2015-04-16 16:55:32 +00:00
# include "LinearMath/btAlignedObjectArray.h"
# include "btBulletCollisionCommon.h"
2015-04-20 22:28:52 +00:00
# include "../CommonInterfaces/CommonGUIHelperInterface.h"
2015-04-16 16:55:32 +00:00
# include "../CommonInterfaces/CommonRenderInterface.h"
# include "../CommonInterfaces/CommonWindowInterface.h"
# include "../CommonInterfaces/CommonGraphicsAppInterface.h"
///VehicleDemo shows how to setup and use the built-in raycast vehicle
2018-09-23 21:17:31 +00:00
class ForkLiftDemo : public CommonExampleInterface
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
public :
2017-01-16 06:26:11 +00:00
GUIHelperInterface * m_guiHelper ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
/* extra stuff*/
2015-04-16 16:55:32 +00:00
btVector3 m_cameraPosition ;
class btDiscreteDynamicsWorld * m_dynamicsWorld ;
btDiscreteDynamicsWorld * getDynamicsWorld ( )
{
return m_dynamicsWorld ;
}
btRigidBody * m_carChassis ;
btRigidBody * localCreateRigidBody ( btScalar mass , const btTransform & worldTransform , btCollisionShape * colSape ) ;
int m_wheelInstances [ 4 ] ;
2018-09-23 21:17:31 +00:00
//----------------------------
2015-04-16 16:55:32 +00:00
btRigidBody * m_liftBody ;
2018-09-23 21:17:31 +00:00
btVector3 m_liftStartPos ;
2015-04-16 16:55:32 +00:00
btHingeConstraint * m_liftHinge ;
btRigidBody * m_forkBody ;
2018-09-23 21:17:31 +00:00
btVector3 m_forkStartPos ;
2015-04-16 16:55:32 +00:00
btSliderConstraint * m_forkSlider ;
btRigidBody * m_loadBody ;
2018-09-23 21:17:31 +00:00
btVector3 m_loadStartPos ;
2015-04-16 16:55:32 +00:00
void lockLiftHinge ( void ) ;
void lockForkSlider ( void ) ;
bool m_useDefaultCamera ;
2018-09-23 21:17:31 +00:00
//----------------------------
2015-04-16 16:55:32 +00:00
btAlignedObjectArray < btCollisionShape * > m_collisionShapes ;
2018-09-23 21:17:31 +00:00
class btBroadphaseInterface * m_overlappingPairCache ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
class btCollisionDispatcher * m_dispatcher ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
class btConstraintSolver * m_constraintSolver ;
2015-04-16 16:55:32 +00:00
class btDefaultCollisionConfiguration * m_collisionConfiguration ;
2018-09-23 21:17:31 +00:00
class btTriangleIndexVertexArray * m_indexVertexArrays ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
btVector3 * m_vertices ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
btRaycastVehicle : : btVehicleTuning m_tuning ;
btVehicleRaycaster * m_vehicleRayCaster ;
btRaycastVehicle * m_vehicle ;
btCollisionShape * m_wheelShape ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
float m_cameraHeight ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
float m_minCameraDistance ;
float m_maxCameraDistance ;
2015-04-16 16:55:32 +00:00
ForkLiftDemo ( struct GUIHelperInterface * helper ) ;
virtual ~ ForkLiftDemo ( ) ;
virtual void stepSimulation ( float deltaTime ) ;
2018-09-23 21:17:31 +00:00
virtual void resetForklift ( ) ;
2015-04-16 16:55:32 +00:00
virtual void clientResetScene ( ) ;
virtual void displayCallback ( ) ;
2018-09-23 21:17:31 +00:00
2015-04-16 16:55:32 +00:00
virtual void specialKeyboard ( int key , int x , int y ) ;
virtual void specialKeyboardUp ( int key , int x , int y ) ;
2018-09-23 21:17:31 +00:00
virtual bool mouseMoveCallback ( float x , float y )
2015-04-16 16:55:32 +00:00
{
return false ;
}
2018-09-23 21:17:31 +00:00
virtual bool mouseButtonCallback ( int button , int state , float x , float y )
2015-04-16 16:55:32 +00:00
{
return false ;
}
2018-09-23 21:17:31 +00:00
virtual bool keyboardCallback ( int key , int state ) ;
2015-04-16 16:55:32 +00:00
virtual void renderScene ( ) ;
virtual void physicsDebugDraw ( int debugFlags ) ;
void initPhysics ( ) ;
void exitPhysics ( ) ;
2015-05-01 18:42:14 +00:00
virtual void resetCamera ( )
{
float dist = 8 ;
2017-06-01 22:30:37 +00:00
float pitch = - 32 ;
float yaw = - 45 ;
2018-09-23 21:17:31 +00:00
float targetPos [ 3 ] = { - 0.33 , - 0.72 , 4.5 } ;
m_guiHelper - > resetCamera ( dist , yaw , pitch , targetPos [ 0 ] , targetPos [ 1 ] , targetPos [ 2 ] ) ;
2015-05-01 18:42:14 +00:00
}
2015-04-16 16:55:32 +00:00
/*static DemoApplication* Create()
{
ForkLiftDemo * demo = new ForkLiftDemo ( ) ;
demo - > myinit ( ) ;
demo - > initPhysics ( ) ;
return demo ;
}
*/
} ;
2015-04-29 18:05:00 +00:00
btScalar maxMotorImpulse = 4000.f ;
2015-04-16 16:55:32 +00:00
//the sequential impulse solver has difficulties dealing with large mass ratios (differences), between loadMass and the fork parts
2018-09-23 21:17:31 +00:00
btScalar loadMass = 350.f ; //
2015-04-16 16:55:32 +00:00
//btScalar loadMass = 10.f;//this should work fine for the SI solver
# ifndef M_PI
2018-09-23 21:17:31 +00:00
# define M_PI 3.14159265358979323846
2015-04-16 16:55:32 +00:00
# endif
# ifndef M_PI_2
2018-09-23 21:17:31 +00:00
# define M_PI_2 1.57079632679489661923
2015-04-16 16:55:32 +00:00
# endif
# ifndef M_PI_4
2018-09-23 21:17:31 +00:00
# define M_PI_4 0.785398163397448309616
2015-04-16 16:55:32 +00:00
# endif
2018-09-23 21:17:31 +00:00
int rightIndex = 0 ;
int upIndex = 1 ;
int forwardIndex = 2 ;
btVector3 wheelDirectionCS0 ( 0 , - 1 , 0 ) ;
btVector3 wheelAxleCS ( - 1 , 0 , 0 ) ;
2015-04-16 16:55:32 +00:00
bool useMCLPSolver = true ;
2018-09-23 21:17:31 +00:00
# include <stdio.h> //printf debugging
2015-04-16 16:55:32 +00:00
# include "ForkLiftDemo.h"
///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
2018-09-23 21:17:31 +00:00
float gEngineForce = 0.f ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
float defaultBreakingForce = 10.f ;
float gBreakingForce = 100.f ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
float maxEngineForce = 1000.f ; //this should be engine/velocity dependent
float maxBreakingForce = 100.f ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
float gVehicleSteering = 0.f ;
float steeringIncrement = 0.04f ;
float steeringClamp = 0.3f ;
float wheelRadius = 0.5f ;
float wheelWidth = 0.4f ;
float wheelFriction = 1000 ; //BT_LARGE_FLOAT;
float suspensionStiffness = 20.f ;
float suspensionDamping = 2.3f ;
float suspensionCompression = 4.4f ;
float rollInfluence = 0.1f ; //1.0f;
2015-04-16 16:55:32 +00:00
btScalar suspensionRestLength ( 0.6 ) ;
# define CUBE_HALF_EXTENTS 1
////////////////////////////////////
ForkLiftDemo : : ForkLiftDemo ( struct GUIHelperInterface * helper )
2018-09-23 21:17:31 +00:00
: m_guiHelper ( helper ) ,
m_carChassis ( 0 ) ,
m_liftBody ( 0 ) ,
m_forkBody ( 0 ) ,
m_loadBody ( 0 ) ,
m_indexVertexArrays ( 0 ) ,
m_vertices ( 0 ) ,
m_cameraHeight ( 4.f ) ,
m_minCameraDistance ( 3.f ) ,
m_maxCameraDistance ( 10.f )
2015-04-16 16:55:32 +00:00
{
helper - > setUpAxis ( 1 ) ;
m_vehicle = 0 ;
m_wheelShape = 0 ;
2018-09-23 21:17:31 +00:00
m_cameraPosition = btVector3 ( 30 , 30 , 30 ) ;
2015-04-16 16:55:32 +00:00
m_useDefaultCamera = false ;
2018-09-23 21:17:31 +00:00
// setTexturing(true);
// setShadows(true);
2015-04-16 16:55:32 +00:00
}
void ForkLiftDemo : : exitPhysics ( )
{
2018-09-23 21:17:31 +00:00
//cleanup in the reverse order of creation/initialization
2015-04-16 16:55:32 +00:00
//remove the rigidbodies from the dynamics world and delete them
int i ;
2018-09-23 21:17:31 +00:00
for ( i = m_dynamicsWorld - > getNumCollisionObjects ( ) - 1 ; i > = 0 ; i - - )
2015-04-16 16:55:32 +00:00
{
btCollisionObject * obj = m_dynamicsWorld - > getCollisionObjectArray ( ) [ i ] ;
btRigidBody * body = btRigidBody : : upcast ( obj ) ;
if ( body & & body - > getMotionState ( ) )
{
while ( body - > getNumConstraintRefs ( ) )
{
btTypedConstraint * constraint = body - > getConstraintRef ( 0 ) ;
m_dynamicsWorld - > removeConstraint ( constraint ) ;
delete constraint ;
}
delete body - > getMotionState ( ) ;
m_dynamicsWorld - > removeRigidBody ( body ) ;
2018-09-23 21:17:31 +00:00
}
else
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
m_dynamicsWorld - > removeCollisionObject ( obj ) ;
2015-04-16 16:55:32 +00:00
}
delete obj ;
}
//delete collision shapes
2018-09-23 21:17:31 +00:00
for ( int j = 0 ; j < m_collisionShapes . size ( ) ; j + + )
2015-04-16 16:55:32 +00:00
{
btCollisionShape * shape = m_collisionShapes [ j ] ;
delete shape ;
}
m_collisionShapes . clear ( ) ;
delete m_indexVertexArrays ;
delete m_vertices ;
//delete dynamics world
delete m_dynamicsWorld ;
2018-09-23 21:17:31 +00:00
m_dynamicsWorld = 0 ;
2015-04-16 16:55:32 +00:00
delete m_vehicleRayCaster ;
m_vehicleRayCaster = 0 ;
delete m_vehicle ;
2018-09-23 21:17:31 +00:00
m_vehicle = 0 ;
2015-04-16 16:55:32 +00:00
delete m_wheelShape ;
2018-09-23 21:17:31 +00:00
m_wheelShape = 0 ;
2015-04-16 16:55:32 +00:00
//delete solver
delete m_constraintSolver ;
2018-09-23 21:17:31 +00:00
m_constraintSolver = 0 ;
2015-04-16 16:55:32 +00:00
//delete broadphase
delete m_overlappingPairCache ;
2018-09-23 21:17:31 +00:00
m_overlappingPairCache = 0 ;
2015-04-16 16:55:32 +00:00
//delete dispatcher
delete m_dispatcher ;
2018-09-23 21:17:31 +00:00
m_dispatcher = 0 ;
2015-04-16 16:55:32 +00:00
delete m_collisionConfiguration ;
2018-09-23 21:17:31 +00:00
m_collisionConfiguration = 0 ;
2015-04-16 16:55:32 +00:00
}
ForkLiftDemo : : ~ ForkLiftDemo ( )
{
//exitPhysics();
}
void ForkLiftDemo : : initPhysics ( )
{
2018-09-23 21:17:31 +00:00
int upAxis = 1 ;
2015-04-16 16:55:32 +00:00
2015-04-29 21:02:50 +00:00
m_guiHelper - > setUpAxis ( upAxis ) ;
2018-09-23 21:17:31 +00:00
btVector3 groundExtents ( 50 , 50 , 50 ) ;
groundExtents [ upAxis ] = 3 ;
2015-04-29 21:02:50 +00:00
btCollisionShape * groundShape = new btBoxShape ( groundExtents ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( groundShape ) ;
m_collisionConfiguration = new btDefaultCollisionConfiguration ( ) ;
m_dispatcher = new btCollisionDispatcher ( m_collisionConfiguration ) ;
2018-09-23 21:17:31 +00:00
btVector3 worldMin ( - 1000 , - 1000 , - 1000 ) ;
btVector3 worldMax ( 1000 , 1000 , 1000 ) ;
m_overlappingPairCache = new btAxisSweep3 ( worldMin , worldMax ) ;
2015-04-16 16:55:32 +00:00
if ( useMCLPSolver )
{
btDantzigSolver * mlcp = new btDantzigSolver ( ) ;
//btSolveProjectedGaussSeidel* mlcp = new btSolveProjectedGaussSeidel;
btMLCPSolver * sol = new btMLCPSolver ( mlcp ) ;
m_constraintSolver = sol ;
2018-09-23 21:17:31 +00:00
}
else
2015-04-16 16:55:32 +00:00
{
m_constraintSolver = new btSequentialImpulseConstraintSolver ( ) ;
}
2018-09-23 21:17:31 +00:00
m_dynamicsWorld = new btDiscreteDynamicsWorld ( m_dispatcher , m_overlappingPairCache , m_constraintSolver , m_collisionConfiguration ) ;
2015-04-16 16:55:32 +00:00
if ( useMCLPSolver )
{
2018-09-23 21:17:31 +00:00
m_dynamicsWorld - > getSolverInfo ( ) . m_minimumSolverBatchSize = 1 ; //for direct solver it is better to have a small A matrix
}
else
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
m_dynamicsWorld - > getSolverInfo ( ) . m_minimumSolverBatchSize = 128 ; //for direct solver, it is better to solve multiple objects together, small batches have high overhead
2015-04-16 16:55:32 +00:00
}
2016-03-03 02:01:33 +00:00
m_dynamicsWorld - > getSolverInfo ( ) . m_globalCfm = 0.00001 ;
2015-04-16 16:55:32 +00:00
m_guiHelper - > createPhysicsDebugDrawer ( m_dynamicsWorld ) ;
//m_dynamicsWorld->setGravity(btVector3(0,0,0));
2018-09-23 21:17:31 +00:00
btTransform tr ;
tr . setIdentity ( ) ;
tr . setOrigin ( btVector3 ( 0 , - 3 , 0 ) ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
//either use heightfield or triangle mesh
2015-04-16 16:55:32 +00:00
//create ground object
2018-09-23 21:17:31 +00:00
localCreateRigidBody ( 0 , tr , groundShape ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
btCollisionShape * chassisShape = new btBoxShape ( btVector3 ( 1.f , 0.5f , 2.f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( chassisShape ) ;
btCompoundShape * compound = new btCompoundShape ( ) ;
m_collisionShapes . push_back ( compound ) ;
btTransform localTrans ;
localTrans . setIdentity ( ) ;
//localTrans effectively shifts the center of mass with respect to the chassis
2018-09-23 21:17:31 +00:00
localTrans . setOrigin ( btVector3 ( 0 , 1 , 0 ) ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
compound - > addChildShape ( localTrans , chassisShape ) ;
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
btCollisionShape * suppShape = new btBoxShape ( btVector3 ( 0.5f , 0.1f , 0.5f ) ) ;
2015-04-16 16:55:32 +00:00
btTransform suppLocalTrans ;
suppLocalTrans . setIdentity ( ) ;
//localTrans effectively shifts the center of mass with respect to the chassis
2018-09-23 21:17:31 +00:00
suppLocalTrans . setOrigin ( btVector3 ( 0 , 1.0 , 2.5 ) ) ;
2015-04-16 16:55:32 +00:00
compound - > addChildShape ( suppLocalTrans , suppShape ) ;
}
2018-09-23 21:17:31 +00:00
tr . setOrigin ( btVector3 ( 0 , 0.f , 0 ) ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
m_carChassis = localCreateRigidBody ( 800 , tr , compound ) ; //chassisShape);
2015-04-16 16:55:32 +00:00
//m_carChassis->setDamping(0.2,0.2);
2018-09-23 21:17:31 +00:00
m_wheelShape = new btCylinderShapeX ( btVector3 ( wheelWidth , wheelRadius , wheelRadius ) ) ;
2015-04-16 16:55:32 +00:00
m_guiHelper - > createCollisionShapeGraphicsObject ( m_wheelShape ) ;
int wheelGraphicsIndex = m_wheelShape - > getUserIndex ( ) ;
2018-09-23 21:17:31 +00:00
const float position [ 4 ] = { 0 , 10 , 10 , 0 } ;
const float quaternion [ 4 ] = { 0 , 0 , 0 , 1 } ;
const float color [ 4 ] = { 0 , 1 , 0 , 1 } ;
const float scaling [ 4 ] = { 1 , 1 , 1 , 1 } ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
for ( int i = 0 ; i < 4 ; i + + )
2015-04-16 16:55:32 +00:00
{
m_wheelInstances [ i ] = m_guiHelper - > registerGraphicsInstance ( wheelGraphicsIndex , position , quaternion , color , scaling ) ;
}
{
2018-09-23 21:17:31 +00:00
btCollisionShape * liftShape = new btBoxShape ( btVector3 ( 0.5f , 2.0f , 0.05f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( liftShape ) ;
btTransform liftTrans ;
m_liftStartPos = btVector3 ( 0.0f , 2.5f , 3.05f ) ;
liftTrans . setIdentity ( ) ;
liftTrans . setOrigin ( m_liftStartPos ) ;
2018-09-23 21:17:31 +00:00
m_liftBody = localCreateRigidBody ( 10 , liftTrans , liftShape ) ;
2015-04-16 16:55:32 +00:00
btTransform localA , localB ;
localA . setIdentity ( ) ;
localB . setIdentity ( ) ;
localA . getBasis ( ) . setEulerZYX ( 0 , M_PI_2 , 0 ) ;
localA . setOrigin ( btVector3 ( 0.0 , 1.0 , 3.05 ) ) ;
localB . getBasis ( ) . setEulerZYX ( 0 , M_PI_2 , 0 ) ;
localB . setOrigin ( btVector3 ( 0.0 , - 1.5 , - 0.05 ) ) ;
2018-09-23 21:17:31 +00:00
m_liftHinge = new btHingeConstraint ( * m_carChassis , * m_liftBody , localA , localB ) ;
// m_liftHinge->setLimit(-LIFT_EPS, LIFT_EPS);
2015-04-16 16:55:32 +00:00
m_liftHinge - > setLimit ( 0.0f , 0.0f ) ;
m_dynamicsWorld - > addConstraint ( m_liftHinge , true ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * forkShapeA = new btBoxShape ( btVector3 ( 1.0f , 0.1f , 0.1f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( forkShapeA ) ;
btCompoundShape * forkCompound = new btCompoundShape ( ) ;
m_collisionShapes . push_back ( forkCompound ) ;
btTransform forkLocalTrans ;
forkLocalTrans . setIdentity ( ) ;
forkCompound - > addChildShape ( forkLocalTrans , forkShapeA ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * forkShapeB = new btBoxShape ( btVector3 ( 0.1f , 0.02f , 0.6f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( forkShapeB ) ;
forkLocalTrans . setIdentity ( ) ;
forkLocalTrans . setOrigin ( btVector3 ( - 0.9f , - 0.08f , 0.7f ) ) ;
forkCompound - > addChildShape ( forkLocalTrans , forkShapeB ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * forkShapeC = new btBoxShape ( btVector3 ( 0.1f , 0.02f , 0.6f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( forkShapeC ) ;
forkLocalTrans . setIdentity ( ) ;
forkLocalTrans . setOrigin ( btVector3 ( 0.9f , - 0.08f , 0.7f ) ) ;
forkCompound - > addChildShape ( forkLocalTrans , forkShapeC ) ;
btTransform forkTrans ;
m_forkStartPos = btVector3 ( 0.0f , 0.6f , 3.2f ) ;
forkTrans . setIdentity ( ) ;
forkTrans . setOrigin ( m_forkStartPos ) ;
m_forkBody = localCreateRigidBody ( 5 , forkTrans , forkCompound ) ;
localA . setIdentity ( ) ;
localB . setIdentity ( ) ;
localA . getBasis ( ) . setEulerZYX ( 0 , 0 , M_PI_2 ) ;
localA . setOrigin ( btVector3 ( 0.0f , - 1.9f , 0.05f ) ) ;
localB . getBasis ( ) . setEulerZYX ( 0 , 0 , M_PI_2 ) ;
localB . setOrigin ( btVector3 ( 0.0 , 0.0 , - 0.1 ) ) ;
m_forkSlider = new btSliderConstraint ( * m_liftBody , * m_forkBody , localA , localB , true ) ;
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 0.1f ) ;
2018-09-23 21:17:31 +00:00
// m_forkSlider->setLowerAngLimit(-LIFT_EPS);
// m_forkSlider->setUpperAngLimit(LIFT_EPS);
2015-04-16 16:55:32 +00:00
m_forkSlider - > setLowerAngLimit ( 0.0f ) ;
m_forkSlider - > setUpperAngLimit ( 0.0f ) ;
m_dynamicsWorld - > addConstraint ( m_forkSlider , true ) ;
btCompoundShape * loadCompound = new btCompoundShape ( ) ;
m_collisionShapes . push_back ( loadCompound ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * loadShapeA = new btBoxShape ( btVector3 ( 2.0f , 0.5f , 0.5f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( loadShapeA ) ;
btTransform loadTrans ;
loadTrans . setIdentity ( ) ;
loadCompound - > addChildShape ( loadTrans , loadShapeA ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * loadShapeB = new btBoxShape ( btVector3 ( 0.1f , 1.0f , 1.0f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( loadShapeB ) ;
loadTrans . setIdentity ( ) ;
loadTrans . setOrigin ( btVector3 ( 2.1f , 0.0f , 0.0f ) ) ;
loadCompound - > addChildShape ( loadTrans , loadShapeB ) ;
2018-09-23 21:17:31 +00:00
btCollisionShape * loadShapeC = new btBoxShape ( btVector3 ( 0.1f , 1.0f , 1.0f ) ) ;
2015-04-16 16:55:32 +00:00
m_collisionShapes . push_back ( loadShapeC ) ;
loadTrans . setIdentity ( ) ;
loadTrans . setOrigin ( btVector3 ( - 2.1f , 0.0f , 0.0f ) ) ;
loadCompound - > addChildShape ( loadTrans , loadShapeC ) ;
loadTrans . setIdentity ( ) ;
m_loadStartPos = btVector3 ( 0.0f , 3.5f , 7.0f ) ;
loadTrans . setOrigin ( m_loadStartPos ) ;
2018-09-23 21:17:31 +00:00
m_loadBody = localCreateRigidBody ( loadMass , loadTrans , loadCompound ) ;
2015-04-16 16:55:32 +00:00
}
/// create vehicle
{
m_vehicleRayCaster = new btDefaultVehicleRaycaster ( m_dynamicsWorld ) ;
2018-09-23 21:17:31 +00:00
m_vehicle = new btRaycastVehicle ( m_tuning , m_carChassis , m_vehicleRayCaster ) ;
2015-04-16 16:55:32 +00:00
///never deactivate the vehicle
m_carChassis - > setActivationState ( DISABLE_DEACTIVATION ) ;
m_dynamicsWorld - > addVehicle ( m_vehicle ) ;
float connectionHeight = 1.2f ;
2018-09-23 21:17:31 +00:00
bool isFrontWheel = true ;
2015-04-16 16:55:32 +00:00
//choose coordinate system
2018-09-23 21:17:31 +00:00
m_vehicle - > setCoordinateSystem ( rightIndex , upIndex , forwardIndex ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
btVector3 connectionPointCS0 ( CUBE_HALF_EXTENTS - ( 0.3 * wheelWidth ) , connectionHeight , 2 * CUBE_HALF_EXTENTS - wheelRadius ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
m_vehicle - > addWheel ( connectionPointCS0 , wheelDirectionCS0 , wheelAxleCS , suspensionRestLength , wheelRadius , m_tuning , isFrontWheel ) ;
connectionPointCS0 = btVector3 ( - CUBE_HALF_EXTENTS + ( 0.3 * wheelWidth ) , connectionHeight , 2 * CUBE_HALF_EXTENTS - wheelRadius ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
m_vehicle - > addWheel ( connectionPointCS0 , wheelDirectionCS0 , wheelAxleCS , suspensionRestLength , wheelRadius , m_tuning , isFrontWheel ) ;
connectionPointCS0 = btVector3 ( - CUBE_HALF_EXTENTS + ( 0.3 * wheelWidth ) , connectionHeight , - 2 * CUBE_HALF_EXTENTS + wheelRadius ) ;
2015-04-16 16:55:32 +00:00
isFrontWheel = false ;
2018-09-23 21:17:31 +00:00
m_vehicle - > addWheel ( connectionPointCS0 , wheelDirectionCS0 , wheelAxleCS , suspensionRestLength , wheelRadius , m_tuning , isFrontWheel ) ;
connectionPointCS0 = btVector3 ( CUBE_HALF_EXTENTS - ( 0.3 * wheelWidth ) , connectionHeight , - 2 * CUBE_HALF_EXTENTS + wheelRadius ) ;
m_vehicle - > addWheel ( connectionPointCS0 , wheelDirectionCS0 , wheelAxleCS , suspensionRestLength , wheelRadius , m_tuning , isFrontWheel ) ;
for ( int i = 0 ; i < m_vehicle - > getNumWheels ( ) ; i + + )
2015-04-16 16:55:32 +00:00
{
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 ;
}
}
resetForklift ( ) ;
2018-09-23 21:17:31 +00:00
// setCameraDistance(26.f);
2015-04-16 16:55:32 +00:00
m_guiHelper - > autogenerateGraphicsObjects ( m_dynamicsWorld ) ;
}
void ForkLiftDemo : : physicsDebugDraw ( int debugFlags )
{
if ( m_dynamicsWorld & & m_dynamicsWorld - > getDebugDrawer ( ) )
{
m_dynamicsWorld - > getDebugDrawer ( ) - > setDebugMode ( debugFlags ) ;
m_dynamicsWorld - > debugDrawWorld ( ) ;
}
}
//to be implemented by the demo
void ForkLiftDemo : : renderScene ( )
{
m_guiHelper - > syncPhysicsToGraphics ( m_dynamicsWorld ) ;
2018-09-23 21:17:31 +00:00
for ( int i = 0 ; i < m_vehicle - > getNumWheels ( ) ; i + + )
2015-04-16 16:55:32 +00:00
{
//synchronize the wheels with the (interpolated) chassis worldtransform
2018-09-23 21:17:31 +00:00
m_vehicle - > updateWheelTransform ( i , true ) ;
2015-04-16 16:55:32 +00:00
CommonRenderInterface * renderer = m_guiHelper - > getRenderInterface ( ) ;
if ( renderer )
{
btTransform tr = m_vehicle - > getWheelInfo ( i ) . m_worldTransform ;
2018-09-23 21:17:31 +00:00
btVector3 pos = tr . getOrigin ( ) ;
2015-04-16 16:55:32 +00:00
btQuaternion orn = tr . getRotation ( ) ;
2018-09-23 21:17:31 +00:00
renderer - > writeSingleInstanceTransformToCPU ( pos , orn , m_wheelInstances [ i ] ) ;
2015-04-16 16:55:32 +00:00
}
}
2015-04-20 22:28:52 +00:00
m_guiHelper - > render ( m_dynamicsWorld ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
ATTRIBUTE_ALIGNED16 ( btScalar )
m [ 16 ] ;
2015-04-16 16:55:32 +00:00
int i ;
2018-09-23 21:17:31 +00:00
btVector3 wheelColor ( 1 , 0 , 0 ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
btVector3 worldBoundsMin , worldBoundsMax ;
getDynamicsWorld ( ) - > getBroadphase ( ) - > getBroadphaseAabb ( worldBoundsMin , worldBoundsMax ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
for ( i = 0 ; i < m_vehicle - > getNumWheels ( ) ; i + + )
2015-04-16 16:55:32 +00:00
{
//synchronize the wheels with the (interpolated) chassis worldtransform
2018-09-23 21:17:31 +00:00
m_vehicle - > updateWheelTransform ( i , true ) ;
2015-04-16 16:55:32 +00:00
//draw wheels (cylinders)
m_vehicle - > getWheelInfo ( i ) . m_worldTransform . getOpenGLMatrix ( m ) ;
2018-09-23 21:17:31 +00:00
// m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
2015-04-16 16:55:32 +00:00
}
#if 0
int lineWidth = 400 ;
int xStart = m_glutScreenWidth - lineWidth ;
int yStart = 20 ;
if ( ( getDebugMode ( ) & btIDebugDraw : : DBG_NoHelpText ) = = 0 )
{
setOrthographicProjection ( ) ;
glDisable ( GL_LIGHTING ) ;
glColor3f ( 0 , 0 , 0 ) ;
char buf [ 124 ] ;
sprintf ( buf , " SHIFT+Cursor Left/Right - rotate lift " ) ;
GLDebugDrawString ( xStart , 20 , buf ) ;
yStart + = 20 ;
sprintf ( buf , " SHIFT+Cursor UP/Down - fork up/down " ) ;
yStart + = 20 ;
GLDebugDrawString ( xStart , yStart , buf ) ;
if ( m_useDefaultCamera )
{
sprintf ( buf , " F5 - camera mode (free) " ) ;
} else
{
sprintf ( buf , " F5 - camera mode (follow) " ) ;
}
yStart + = 20 ;
GLDebugDrawString ( xStart , yStart , buf ) ;
yStart + = 20 ;
if ( m_dynamicsWorld - > getConstraintSolver ( ) - > getSolverType ( ) = = BT_MLCP_SOLVER )
{
sprintf ( buf , " F6 - solver (direct MLCP) " ) ;
} else
{
sprintf ( buf , " F6 - solver (sequential impulse) " ) ;
}
GLDebugDrawString ( xStart , yStart , buf ) ;
btDiscreteDynamicsWorld * world = ( btDiscreteDynamicsWorld * ) m_dynamicsWorld ;
if ( world - > getLatencyMotionStateInterpolation ( ) )
{
sprintf ( buf , " F7 - motionstate interpolation (on) " ) ;
} else
{
sprintf ( buf , " F7 - motionstate interpolation (off) " ) ;
}
yStart + = 20 ;
GLDebugDrawString ( xStart , yStart , buf ) ;
sprintf ( buf , " Click window for keyboard focus " ) ;
yStart + = 20 ;
GLDebugDrawString ( xStart , yStart , buf ) ;
resetPerspectiveProjection ( ) ;
glEnable ( GL_LIGHTING ) ;
}
# endif
}
void ForkLiftDemo : : stepSimulation ( float deltaTime )
{
2018-09-23 21:17:31 +00:00
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
{
2015-04-16 16:55:32 +00:00
int wheelIndex = 2 ;
2018-09-23 21:17:31 +00:00
m_vehicle - > applyEngineForce ( gEngineForce , wheelIndex ) ;
m_vehicle - > setBrake ( gBreakingForce , wheelIndex ) ;
2015-04-16 16:55:32 +00:00
wheelIndex = 3 ;
2018-09-23 21:17:31 +00:00
m_vehicle - > applyEngineForce ( gEngineForce , wheelIndex ) ;
m_vehicle - > setBrake ( gBreakingForce , wheelIndex ) ;
2015-04-16 16:55:32 +00:00
wheelIndex = 0 ;
2018-09-23 21:17:31 +00:00
m_vehicle - > setSteeringValue ( gVehicleSteering , wheelIndex ) ;
2015-04-16 16:55:32 +00:00
wheelIndex = 1 ;
2018-09-23 21:17:31 +00:00
m_vehicle - > setSteeringValue ( gVehicleSteering , wheelIndex ) ;
2015-04-16 16:55:32 +00:00
}
float dt = deltaTime ;
2018-09-23 21:17:31 +00:00
2015-04-16 16:55:32 +00:00
if ( m_dynamicsWorld )
{
//during idle mode, just run 1 simulation step maximum
2018-09-23 21:17:31 +00:00
int maxSimSubSteps = 2 ;
2015-04-16 16:55:32 +00:00
int numSimSteps ;
2018-09-23 21:17:31 +00:00
numSimSteps = m_dynamicsWorld - > stepSimulation ( dt , maxSimSubSteps ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
if ( m_dynamicsWorld - > getConstraintSolver ( ) - > getSolverType ( ) = = BT_MLCP_SOLVER )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
btMLCPSolver * sol = ( btMLCPSolver * ) m_dynamicsWorld - > getConstraintSolver ( ) ;
2015-04-16 16:55:32 +00:00
int numFallbacks = sol - > getNumFallbacks ( ) ;
if ( numFallbacks )
{
static int totalFailures = 0 ;
2018-09-23 21:17:31 +00:00
totalFailures + = numFallbacks ;
printf ( " MLCP solver failed %d times, falling back to btSequentialImpulseSolver (SI) \n " , totalFailures ) ;
2015-04-16 16:55:32 +00:00
}
sol - > setNumFallbacks ( 0 ) ;
}
//#define VERBOSE_FEEDBACK
# ifdef VERBOSE_FEEDBACK
2018-09-23 21:17:31 +00:00
if ( ! numSimSteps )
2015-04-16 16:55:32 +00:00
printf ( " Interpolated transforms \n " ) ;
else
{
if ( numSimSteps > maxSimSubSteps )
{
//detect dropping frames
2018-09-23 21:17:31 +00:00
printf ( " Dropped (%i) simulation steps out of %i \n " , numSimSteps - maxSimSubSteps , numSimSteps ) ;
}
else
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
printf ( " Simulated (%i) steps \n " , numSimSteps ) ;
2015-04-16 16:55:32 +00:00
}
}
2018-09-23 21:17:31 +00:00
# endif //VERBOSE_FEEDBACK
2015-04-16 16:55:32 +00:00
}
}
2018-09-23 21:17:31 +00:00
void ForkLiftDemo : : displayCallback ( void )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2015-04-16 16:55:32 +00:00
//renderme();
2018-09-23 21:17:31 +00:00
//optional but useful: debug drawing
2015-04-16 16:55:32 +00:00
if ( m_dynamicsWorld )
m_dynamicsWorld - > debugDrawWorld ( ) ;
2018-09-23 21:17:31 +00:00
// glFlush();
// glutSwapBuffers();
2015-04-16 16:55:32 +00:00
}
void ForkLiftDemo : : clientResetScene ( )
{
exitPhysics ( ) ;
initPhysics ( ) ;
}
void ForkLiftDemo : : resetForklift ( )
{
gVehicleSteering = 0.f ;
gBreakingForce = defaultBreakingForce ;
gEngineForce = 0.f ;
m_carChassis - > setCenterOfMassTransform ( btTransform : : getIdentity ( ) ) ;
2018-09-23 21:17:31 +00:00
m_carChassis - > setLinearVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
m_carChassis - > setAngularVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
m_dynamicsWorld - > getBroadphase ( ) - > getOverlappingPairCache ( ) - > cleanProxyFromPairs ( m_carChassis - > getBroadphaseHandle ( ) , getDynamicsWorld ( ) - > getDispatcher ( ) ) ;
2015-04-16 16:55:32 +00:00
if ( m_vehicle )
{
m_vehicle - > resetSuspension ( ) ;
2018-09-23 21:17:31 +00:00
for ( int i = 0 ; i < m_vehicle - > getNumWheels ( ) ; i + + )
2015-04-16 16:55:32 +00:00
{
//synchronize the wheels with the (interpolated) chassis worldtransform
2018-09-23 21:17:31 +00:00
m_vehicle - > updateWheelTransform ( i , true ) ;
2015-04-16 16:55:32 +00:00
}
}
btTransform liftTrans ;
liftTrans . setIdentity ( ) ;
liftTrans . setOrigin ( m_liftStartPos ) ;
m_liftBody - > activate ( ) ;
m_liftBody - > setCenterOfMassTransform ( liftTrans ) ;
2018-09-23 21:17:31 +00:00
m_liftBody - > setLinearVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
m_liftBody - > setAngularVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
2015-04-16 16:55:32 +00:00
btTransform forkTrans ;
forkTrans . setIdentity ( ) ;
forkTrans . setOrigin ( m_forkStartPos ) ;
m_forkBody - > activate ( ) ;
m_forkBody - > setCenterOfMassTransform ( forkTrans ) ;
2018-09-23 21:17:31 +00:00
m_forkBody - > setLinearVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
m_forkBody - > setAngularVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
// m_liftHinge->setLimit(-LIFT_EPS, LIFT_EPS);
2015-04-16 16:55:32 +00:00
m_liftHinge - > setLimit ( 0.0f , 0.0f ) ;
m_liftHinge - > enableAngularMotor ( false , 0 , 0 ) ;
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 0.1f ) ;
m_forkSlider - > setPoweredLinMotor ( false ) ;
btTransform loadTrans ;
loadTrans . setIdentity ( ) ;
loadTrans . setOrigin ( m_loadStartPos ) ;
m_loadBody - > activate ( ) ;
m_loadBody - > setCenterOfMassTransform ( loadTrans ) ;
2018-09-23 21:17:31 +00:00
m_loadBody - > setLinearVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
m_loadBody - > setAngularVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
2015-04-16 16:55:32 +00:00
}
2018-09-23 21:17:31 +00:00
bool ForkLiftDemo : : keyboardCallback ( int key , int state )
2015-04-16 16:55:32 +00:00
{
bool handled = false ;
2015-04-17 00:35:34 +00:00
bool isShiftPressed = m_guiHelper - > getAppInterface ( ) - > m_window - > isModifierKeyPressed ( B3G_SHIFT ) ;
2015-04-16 16:55:32 +00:00
if ( state )
{
2018-09-23 21:17:31 +00:00
if ( isShiftPressed )
{
switch ( key )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
case B3G_LEFT_ARROW :
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
m_liftHinge - > setLimit ( - M_PI / 16.0f , M_PI / 8.0f ) ;
2015-04-16 16:55:32 +00:00
m_liftHinge - > enableAngularMotor ( true , - 0.1 , maxMotorImpulse ) ;
handled = true ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_RIGHT_ARROW :
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
m_liftHinge - > setLimit ( - M_PI / 16.0f , M_PI / 8.0f ) ;
2015-04-16 16:55:32 +00:00
m_liftHinge - > enableAngularMotor ( true , 0.1 , maxMotorImpulse ) ;
handled = true ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_UP_ARROW :
2015-04-16 16:55:32 +00:00
{
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 3.9f ) ;
m_forkSlider - > setPoweredLinMotor ( true ) ;
m_forkSlider - > setMaxLinMotorForce ( maxMotorImpulse ) ;
m_forkSlider - > setTargetLinMotorVelocity ( 1.0 ) ;
handled = true ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_DOWN_ARROW :
2015-04-16 16:55:32 +00:00
{
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 3.9f ) ;
m_forkSlider - > setPoweredLinMotor ( true ) ;
m_forkSlider - > setMaxLinMotorForce ( maxMotorImpulse ) ;
m_forkSlider - > setTargetLinMotorVelocity ( - 1.0 ) ;
handled = true ;
break ;
}
}
2018-09-23 21:17:31 +00:00
}
else
{
switch ( key )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
case B3G_LEFT_ARROW :
2015-04-16 16:55:32 +00:00
{
handled = true ;
gVehicleSteering + = steeringIncrement ;
2018-09-23 21:17:31 +00:00
if ( gVehicleSteering > steeringClamp )
2015-04-16 16:55:32 +00:00
gVehicleSteering = steeringClamp ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_RIGHT_ARROW :
2015-04-16 16:55:32 +00:00
{
handled = true ;
gVehicleSteering - = steeringIncrement ;
2018-09-23 21:17:31 +00:00
if ( gVehicleSteering < - steeringClamp )
2015-04-16 16:55:32 +00:00
gVehicleSteering = - steeringClamp ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_UP_ARROW :
2015-04-16 16:55:32 +00:00
{
handled = true ;
gEngineForce = maxEngineForce ;
gBreakingForce = 0.f ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_DOWN_ARROW :
2015-04-16 16:55:32 +00:00
{
handled = true ;
gEngineForce = - maxEngineForce ;
gBreakingForce = 0.f ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_F7 :
2015-04-16 16:55:32 +00:00
{
handled = true ;
btDiscreteDynamicsWorld * world = ( btDiscreteDynamicsWorld * ) m_dynamicsWorld ;
world - > setLatencyMotionStateInterpolation ( ! world - > getLatencyMotionStateInterpolation ( ) ) ;
printf ( " world latencyMotionStateInterpolation = %d \n " , world - > getLatencyMotionStateInterpolation ( ) ) ;
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_F6 :
2015-04-16 16:55:32 +00:00
{
handled = true ;
//switch solver (needs demo restart)
useMCLPSolver = ! useMCLPSolver ;
printf ( " switching to useMLCPSolver = %d \n " , useMCLPSolver ) ;
delete m_constraintSolver ;
if ( useMCLPSolver )
{
btDantzigSolver * mlcp = new btDantzigSolver ( ) ;
//btSolveProjectedGaussSeidel* mlcp = new btSolveProjectedGaussSeidel;
btMLCPSolver * sol = new btMLCPSolver ( mlcp ) ;
m_constraintSolver = sol ;
2018-09-23 21:17:31 +00:00
}
else
2015-04-16 16:55:32 +00:00
{
m_constraintSolver = new btSequentialImpulseConstraintSolver ( ) ;
}
m_dynamicsWorld - > setConstraintSolver ( m_constraintSolver ) ;
//exitPhysics();
//initPhysics();
break ;
}
2018-09-23 21:17:31 +00:00
case B3G_F5 :
handled = true ;
m_useDefaultCamera = ! m_useDefaultCamera ;
break ;
default :
break ;
2015-04-16 16:55:32 +00:00
}
2018-09-23 21:17:31 +00:00
}
2015-04-16 16:55:32 +00:00
}
2018-09-23 21:17:31 +00:00
else
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
switch ( key )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
case B3G_UP_ARROW :
2015-04-16 16:55:32 +00:00
{
lockForkSlider ( ) ;
gEngineForce = 0.f ;
2018-09-23 21:17:31 +00:00
gBreakingForce = defaultBreakingForce ;
handled = true ;
break ;
2015-04-16 16:55:32 +00:00
}
2018-09-23 21:17:31 +00:00
case B3G_DOWN_ARROW :
2015-04-16 16:55:32 +00:00
{
lockForkSlider ( ) ;
gEngineForce = 0.f ;
gBreakingForce = defaultBreakingForce ;
2018-09-23 21:17:31 +00:00
handled = true ;
break ;
2015-04-16 16:55:32 +00:00
}
2018-09-23 21:17:31 +00:00
case B3G_LEFT_ARROW :
case B3G_RIGHT_ARROW :
2015-04-16 16:55:32 +00:00
{
lockLiftHinge ( ) ;
2018-09-23 21:17:31 +00:00
handled = true ;
2015-04-16 16:55:32 +00:00
break ;
}
2018-09-23 21:17:31 +00:00
default :
break ;
2015-04-16 16:55:32 +00:00
}
}
return handled ;
}
void ForkLiftDemo : : specialKeyboardUp ( int key , int x , int y )
{
#if 0
2018-09-23 21:17:31 +00:00
2015-04-16 16:55:32 +00:00
# endif
}
void ForkLiftDemo : : specialKeyboard ( int key , int x , int y )
{
#if 0
if ( key = = GLUT_KEY_END )
return ;
// printf("key = %i x=%i y=%i\n",key,x,y);
int state ;
state = glutGetModifiers ( ) ;
if ( state & GLUT_ACTIVE_SHIFT )
{
switch ( key )
{
case GLUT_KEY_LEFT :
{
m_liftHinge - > setLimit ( - M_PI / 16.0f , M_PI / 8.0f ) ;
m_liftHinge - > enableAngularMotor ( true , - 0.1 , maxMotorImpulse ) ;
break ;
}
case GLUT_KEY_RIGHT :
{
m_liftHinge - > setLimit ( - M_PI / 16.0f , M_PI / 8.0f ) ;
m_liftHinge - > enableAngularMotor ( true , 0.1 , maxMotorImpulse ) ;
break ;
}
case GLUT_KEY_UP :
{
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 3.9f ) ;
m_forkSlider - > setPoweredLinMotor ( true ) ;
m_forkSlider - > setMaxLinMotorForce ( maxMotorImpulse ) ;
m_forkSlider - > setTargetLinMotorVelocity ( 1.0 ) ;
break ;
}
case GLUT_KEY_DOWN :
{
m_forkSlider - > setLowerLinLimit ( 0.1f ) ;
m_forkSlider - > setUpperLinLimit ( 3.9f ) ;
m_forkSlider - > setPoweredLinMotor ( true ) ;
m_forkSlider - > setMaxLinMotorForce ( maxMotorImpulse ) ;
m_forkSlider - > setTargetLinMotorVelocity ( - 1.0 ) ;
break ;
}
default :
DemoApplication : : specialKeyboard ( key , x , y ) ;
break ;
}
} else
{
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 ;
gBreakingForce = 0.f ;
break ;
}
case GLUT_KEY_DOWN :
{
gEngineForce = - maxEngineForce ;
gBreakingForce = 0.f ;
break ;
}
case GLUT_KEY_F7 :
{
btDiscreteDynamicsWorld * world = ( btDiscreteDynamicsWorld * ) m_dynamicsWorld ;
world - > setLatencyMotionStateInterpolation ( ! world - > getLatencyMotionStateInterpolation ( ) ) ;
printf ( " world latencyMotionStateInterpolation = %d \n " , world - > getLatencyMotionStateInterpolation ( ) ) ;
break ;
}
case GLUT_KEY_F6 :
{
//switch solver (needs demo restart)
useMCLPSolver = ! useMCLPSolver ;
printf ( " switching to useMLCPSolver = %d \n " , useMCLPSolver ) ;
delete m_constraintSolver ;
if ( useMCLPSolver )
{
btDantzigSolver * mlcp = new btDantzigSolver ( ) ;
//btSolveProjectedGaussSeidel* mlcp = new btSolveProjectedGaussSeidel;
btMLCPSolver * sol = new btMLCPSolver ( mlcp ) ;
m_constraintSolver = sol ;
} else
{
m_constraintSolver = new btSequentialImpulseConstraintSolver ( ) ;
}
m_dynamicsWorld - > setConstraintSolver ( m_constraintSolver ) ;
//exitPhysics();
//initPhysics();
break ;
}
case GLUT_KEY_F5 :
m_useDefaultCamera = ! m_useDefaultCamera ;
break ;
default :
DemoApplication : : specialKeyboard ( key , x , y ) ;
break ;
}
}
// glutPostRedisplay();
# endif
}
void ForkLiftDemo : : lockLiftHinge ( void )
{
btScalar hingeAngle = m_liftHinge - > getHingeAngle ( ) ;
btScalar lowLim = m_liftHinge - > getLowerLimit ( ) ;
btScalar hiLim = m_liftHinge - > getUpperLimit ( ) ;
m_liftHinge - > enableAngularMotor ( false , 0 , 0 ) ;
2018-09-23 21:17:31 +00:00
if ( hingeAngle < lowLim )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
// m_liftHinge->setLimit(lowLim, lowLim + LIFT_EPS);
2015-04-16 16:55:32 +00:00
m_liftHinge - > setLimit ( lowLim , lowLim ) ;
}
2018-09-23 21:17:31 +00:00
else if ( hingeAngle > hiLim )
2015-04-16 16:55:32 +00:00
{
2018-09-23 21:17:31 +00:00
// m_liftHinge->setLimit(hiLim - LIFT_EPS, hiLim);
2015-04-16 16:55:32 +00:00
m_liftHinge - > setLimit ( hiLim , hiLim ) ;
}
else
{
2018-09-23 21:17:31 +00:00
// m_liftHinge->setLimit(hingeAngle - LIFT_EPS, hingeAngle + LIFT_EPS);
2015-04-16 16:55:32 +00:00
m_liftHinge - > setLimit ( hingeAngle , hingeAngle ) ;
}
return ;
2018-09-23 21:17:31 +00:00
} // ForkLiftDemo::lockLiftHinge()
2015-04-16 16:55:32 +00:00
void ForkLiftDemo : : lockForkSlider ( void )
{
btScalar linDepth = m_forkSlider - > getLinearPos ( ) ;
btScalar lowLim = m_forkSlider - > getLowerLinLimit ( ) ;
btScalar hiLim = m_forkSlider - > getUpperLinLimit ( ) ;
m_forkSlider - > setPoweredLinMotor ( false ) ;
2018-09-23 21:17:31 +00:00
if ( linDepth < = lowLim )
2015-04-16 16:55:32 +00:00
{
m_forkSlider - > setLowerLinLimit ( lowLim ) ;
m_forkSlider - > setUpperLinLimit ( lowLim ) ;
}
2018-09-23 21:17:31 +00:00
else if ( linDepth > hiLim )
2015-04-16 16:55:32 +00:00
{
m_forkSlider - > setLowerLinLimit ( hiLim ) ;
m_forkSlider - > setUpperLinLimit ( hiLim ) ;
}
else
{
m_forkSlider - > setLowerLinLimit ( linDepth ) ;
m_forkSlider - > setUpperLinLimit ( linDepth ) ;
}
return ;
2018-09-23 21:17:31 +00:00
} // ForkLiftDemo::lockForkSlider()
2015-04-16 16:55:32 +00:00
btRigidBody * ForkLiftDemo : : localCreateRigidBody ( btScalar mass , const btTransform & startTransform , btCollisionShape * shape )
{
btAssert ( ( ! shape | | shape - > getShapeType ( ) ! = INVALID_SHAPE_PROXYTYPE ) ) ;
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = ( mass ! = 0.f ) ;
2018-09-23 21:17:31 +00:00
btVector3 localInertia ( 0 , 0 , 0 ) ;
2015-04-16 16:55:32 +00:00
if ( isDynamic )
2018-09-23 21:17:31 +00:00
shape - > calculateLocalInertia ( mass , localInertia ) ;
2015-04-16 16:55:32 +00:00
2018-09-23 21:17:31 +00:00
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
2015-04-16 16:55:32 +00:00
# define USE_MOTIONSTATE 1
# ifdef USE_MOTIONSTATE
btDefaultMotionState * myMotionState = new btDefaultMotionState ( startTransform ) ;
2018-09-23 21:17:31 +00:00
btRigidBody : : btRigidBodyConstructionInfo cInfo ( mass , myMotionState , shape , localInertia ) ;
2015-04-16 16:55:32 +00:00
btRigidBody * body = new btRigidBody ( cInfo ) ;
//body->setContactProcessingThreshold(m_defaultContactProcessingThreshold);
# else
2018-09-23 21:17:31 +00:00
btRigidBody * body = new btRigidBody ( mass , 0 , shape , localInertia ) ;
2015-04-16 16:55:32 +00:00
body - > setWorldTransform ( startTransform ) ;
2018-09-23 21:17:31 +00:00
# endif //
2015-04-16 16:55:32 +00:00
m_dynamicsWorld - > addRigidBody ( body ) ;
return body ;
}
2018-09-23 21:17:31 +00:00
CommonExampleInterface * ForkLiftCreateFunc ( struct CommonExampleOptions & options )
2015-04-16 16:55:32 +00:00
{
2015-05-01 18:42:14 +00:00
return new ForkLiftDemo ( options . m_guiHelper ) ;
2015-04-16 16:55:32 +00:00
}