Back to index

supertuxkart  0.5+dfsg1
btRaycastVehicle.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Erwin Coumans http://continuousphysics.com/Bullet/
00003  *
00004  * Permission to use, copy, modify, distribute and sell this software
00005  * and its documentation for any purpose is hereby granted without fee,
00006  * provided that the above copyright notice appear in all copies.
00007  * Erwin Coumans makes no representations about the suitability 
00008  * of this software for any purpose.  
00009  * It is provided "as is" without express or implied warranty.
00010 */
00011 #ifndef RAYCASTVEHICLE_H
00012 #define RAYCASTVEHICLE_H
00013 
00014 #include "BulletDynamics/Dynamics/btRigidBody.h"
00015 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
00016 #include "btVehicleRaycaster.h"
00017 class btDynamicsWorld;
00018 #include "LinearMath/btAlignedObjectArray.h"
00019 #include "btWheelInfo.h"
00020 
00021 class btVehicleTuning;
00022 
00024 class btRaycastVehicle : public btTypedConstraint
00025 {
00026 
00027               btAlignedObjectArray<btVector3>    m_forwardWS;
00028               btAlignedObjectArray<btVector3>    m_axle;
00029               btAlignedObjectArray<btScalar>     m_forwardImpulse;
00030               btAlignedObjectArray<btScalar>     m_sideImpulse;
00031 
00032 public:
00033        class btVehicleTuning
00034               {
00035                      public:
00036 
00037                      btVehicleTuning()
00038                             :m_suspensionStiffness(btScalar(5.88)),
00039                             m_suspensionCompression(btScalar(0.83)),
00040                             m_suspensionDamping(btScalar(0.88)),
00041                             m_maxSuspensionTravelCm(btScalar(500.)),
00042                             m_frictionSlip(btScalar(10.5))
00043                      {
00044                      }
00045                      btScalar      m_suspensionStiffness;
00046                      btScalar      m_suspensionCompression;
00047                      btScalar      m_suspensionDamping;
00048                      btScalar      m_maxSuspensionTravelCm;
00049                      btScalar      m_frictionSlip;
00050 
00051               };
00052 private:
00053 
00054        btScalar      m_tau;
00055        btScalar      m_damping;
00056        btVehicleRaycaster*  m_vehicleRaycaster;
00057        btScalar             m_pitchControl;
00058        btScalar      m_steeringValue; 
00059        btScalar m_currentVehicleSpeedKmHour;
00060 
00061        btRigidBody* m_chassisBody;
00062 
00063        int m_indexRightAxis;
00064        int m_indexUpAxis;
00065        int    m_indexForwardAxis;
00066 
00067        void defaultInit(const btVehicleTuning& tuning);
00068 
00069 public:
00070 
00071        //constructor to create a car from an existing rigidbody
00072        btRaycastVehicle(const btVehicleTuning& tuning,btRigidBody* chassis,  btVehicleRaycaster* raycaster );
00073 
00074        virtual ~btRaycastVehicle() ;
00075 
00076               
00077        const btTransform& getChassisWorldTransform() const;
00078        
00079        btScalar rayCast(btWheelInfo& wheel);
00080 
00081        virtual void updateVehicle(btScalar step);
00082 
00083        void resetSuspension();
00084 
00085        btScalar      getSteeringValue(int wheel) const;
00086 
00087        void   setSteeringValue(btScalar steering,int wheel);
00088 
00089 
00090        void   applyEngineForce(btScalar force, int wheel);
00091 
00092        const btTransform&   getWheelTransformWS( int wheelIndex ) const;
00093 
00094        void   updateWheelTransform( int wheelIndex, bool interpolatedTransform = true );
00095        
00096        void   setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth);
00097 
00098        btWheelInfo&  addWheel( const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0,const btVector3& wheelAxleCS,btScalar suspensionRestLength,btScalar wheelRadius,const btVehicleTuning& tuning, bool isFrontWheel);
00099 
00100        inline int           getNumWheels() const {
00101               return int (m_wheelInfo.size());
00102        }
00103        
00104        btAlignedObjectArray<btWheelInfo>  m_wheelInfo;
00105 
00106 
00107        const btWheelInfo&   getWheelInfo(int index) const;
00108 
00109        btWheelInfo&  getWheelInfo(int index);
00110 
00111        void   updateWheelTransformsWS(btWheelInfo& wheel , bool interpolatedTransform = true);
00112 
00113        
00114        void setBrake(btScalar brake,int wheelIndex);
00115 
00116        void   setPitchControl(btScalar pitch)
00117        {
00118               m_pitchControl = pitch;
00119        }
00120        
00121        void   updateSuspension(btScalar deltaTime);
00122 
00123        virtual void  updateFriction(btScalar     timeStep);
00124 
00125 
00126 
00127        inline btRigidBody* getRigidBody()
00128        {
00129               return m_chassisBody;
00130        }
00131 
00132        const btRigidBody* getRigidBody() const
00133        {
00134               return m_chassisBody;
00135        }
00136 
00137        inline int    getRightAxis() const
00138        {
00139               return m_indexRightAxis;
00140        }
00141        inline int getUpAxis() const
00142        {
00143               return m_indexUpAxis;
00144        }
00145 
00146        inline int getForwardAxis() const
00147        {
00148               return m_indexForwardAxis;
00149        }
00150 
00151        
00153        btVector3 getForwardVector() const
00154        {
00155               const btTransform& chassisTrans = getChassisWorldTransform(); 
00156 
00157               btVector3 forwardW ( 
00158                        chassisTrans.getBasis()[0][m_indexForwardAxis], 
00159                        chassisTrans.getBasis()[1][m_indexForwardAxis], 
00160                        chassisTrans.getBasis()[2][m_indexForwardAxis]); 
00161 
00162               return forwardW;
00163        }
00164 
00166        btScalar      getCurrentSpeedKmHour() const
00167        {
00168               return m_currentVehicleSpeedKmHour;
00169        }
00170 
00171        virtual void  setCoordinateSystem(int rightIndex,int upIndex,int forwardIndex)
00172        {
00173               m_indexRightAxis = rightIndex;
00174               m_indexUpAxis = upIndex;
00175               m_indexForwardAxis = forwardIndex;
00176        }
00177 
00178        virtual void  buildJacobian()
00179        {
00180               //not yet
00181        }
00182 
00183        virtual       void   solveConstraint(btScalar    timeStep)
00184        {
00185               (void)timeStep;
00186               //not yet
00187        }
00188 
00189 
00190 };
00191 
00192 class btDefaultVehicleRaycaster : public btVehicleRaycaster
00193 {
00194        btDynamicsWorld*     m_dynamicsWorld;
00195 public:
00196        btDefaultVehicleRaycaster(btDynamicsWorld* world)
00197               :m_dynamicsWorld(world)
00198        {
00199        }
00200 
00201        virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result);
00202 
00203 };
00204 
00205 
00206 #endif //RAYCASTVEHICLE_H
00207