| 1 | /* | 
|---|
| 2 | * Copyright (c) 2005 Erwin Coumans http://continuousphysics.com/Bullet/ | 
|---|
| 3 | * | 
|---|
| 4 | * Permission to use, copy, modify, distribute and sell this software | 
|---|
| 5 | * and its documentation for any purpose is hereby granted without fee, | 
|---|
| 6 | * provided that the above copyright notice appear in all copies. | 
|---|
| 7 | * Erwin Coumans makes no representations about the suitability | 
|---|
| 8 | * of this software for any purpose. | 
|---|
| 9 | * It is provided "as is" without express or implied warranty. | 
|---|
| 10 | */ | 
|---|
| 11 | #ifndef RAYCASTVEHICLE_H | 
|---|
| 12 | #define RAYCASTVEHICLE_H | 
|---|
| 13 |  | 
|---|
| 14 | #include "BulletDynamics/Dynamics/btRigidBody.h" | 
|---|
| 15 | #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h" | 
|---|
| 16 | #include "btVehicleRaycaster.h" | 
|---|
| 17 | class btDynamicsWorld; | 
|---|
| 18 | #include "LinearMath/btAlignedObjectArray.h" | 
|---|
| 19 | #include "btWheelInfo.h" | 
|---|
| 20 | #include "BulletDynamics/Dynamics/btActionInterface.h" | 
|---|
| 21 |  | 
|---|
| 22 | class btVehicleTuning; | 
|---|
| 23 |  | 
|---|
| 24 | ///rayCast vehicle, very special constraint that turn a rigidbody into a vehicle. | 
|---|
| 25 | class btRaycastVehicle : public btActionInterface | 
|---|
| 26 | { | 
|---|
| 27 |  | 
|---|
| 28 | btAlignedObjectArray<btVector3> m_forwardWS; | 
|---|
| 29 | btAlignedObjectArray<btVector3> m_axle; | 
|---|
| 30 | btAlignedObjectArray<btScalar>  m_forwardImpulse; | 
|---|
| 31 | btAlignedObjectArray<btScalar>  m_sideImpulse; | 
|---|
| 32 |  | 
|---|
| 33 | public: | 
|---|
| 34 | class btVehicleTuning | 
|---|
| 35 | { | 
|---|
| 36 | public: | 
|---|
| 37 |  | 
|---|
| 38 | btVehicleTuning() | 
|---|
| 39 | :m_suspensionStiffness(btScalar(5.88)), | 
|---|
| 40 | m_suspensionCompression(btScalar(0.83)), | 
|---|
| 41 | m_suspensionDamping(btScalar(0.88)), | 
|---|
| 42 | m_maxSuspensionTravelCm(btScalar(500.)), | 
|---|
| 43 | m_frictionSlip(btScalar(10.5)) | 
|---|
| 44 | { | 
|---|
| 45 | } | 
|---|
| 46 | btScalar        m_suspensionStiffness; | 
|---|
| 47 | btScalar        m_suspensionCompression; | 
|---|
| 48 | btScalar        m_suspensionDamping; | 
|---|
| 49 | btScalar        m_maxSuspensionTravelCm; | 
|---|
| 50 | btScalar        m_frictionSlip; | 
|---|
| 51 |  | 
|---|
| 52 | }; | 
|---|
| 53 | private: | 
|---|
| 54 |  | 
|---|
| 55 | btScalar        m_tau; | 
|---|
| 56 | btScalar        m_damping; | 
|---|
| 57 | btVehicleRaycaster*     m_vehicleRaycaster; | 
|---|
| 58 | btScalar                m_pitchControl; | 
|---|
| 59 | btScalar        m_steeringValue; | 
|---|
| 60 | btScalar m_currentVehicleSpeedKmHour; | 
|---|
| 61 |  | 
|---|
| 62 | btRigidBody* m_chassisBody; | 
|---|
| 63 |  | 
|---|
| 64 | int m_indexRightAxis; | 
|---|
| 65 | int m_indexUpAxis; | 
|---|
| 66 | int     m_indexForwardAxis; | 
|---|
| 67 |  | 
|---|
| 68 | void defaultInit(const btVehicleTuning& tuning); | 
|---|
| 69 |  | 
|---|
| 70 | public: | 
|---|
| 71 |  | 
|---|
| 72 | //constructor to create a car from an existing rigidbody | 
|---|
| 73 | btRaycastVehicle(const btVehicleTuning& tuning,btRigidBody* chassis,    btVehicleRaycaster* raycaster ); | 
|---|
| 74 |  | 
|---|
| 75 | virtual ~btRaycastVehicle() ; | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | ///btActionInterface interface | 
|---|
| 79 | virtual void updateAction( btCollisionWorld* collisionWorld, btScalar step) | 
|---|
| 80 | { | 
|---|
| 81 | updateVehicle(step); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | ///btActionInterface interface | 
|---|
| 86 | void    debugDraw(btIDebugDraw* debugDrawer); | 
|---|
| 87 |  | 
|---|
| 88 | const btTransform& getChassisWorldTransform() const; | 
|---|
| 89 |  | 
|---|
| 90 | btScalar rayCast(btWheelInfo& wheel); | 
|---|
| 91 |  | 
|---|
| 92 | virtual void updateVehicle(btScalar step); | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | void resetSuspension(); | 
|---|
| 96 |  | 
|---|
| 97 | btScalar        getSteeringValue(int wheel) const; | 
|---|
| 98 |  | 
|---|
| 99 | void    setSteeringValue(btScalar steering,int wheel); | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | void    applyEngineForce(btScalar force, int wheel); | 
|---|
| 103 |  | 
|---|
| 104 | const btTransform&      getWheelTransformWS( int wheelIndex ) const; | 
|---|
| 105 |  | 
|---|
| 106 | void    updateWheelTransform( int wheelIndex, bool interpolatedTransform = true ); | 
|---|
| 107 |  | 
|---|
| 108 | void    setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth); | 
|---|
| 109 |  | 
|---|
| 110 | btWheelInfo&    addWheel( const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0,const btVector3& wheelAxleCS,btScalar suspensionRestLength,btScalar wheelRadius,const btVehicleTuning& tuning, bool isFrontWheel); | 
|---|
| 111 |  | 
|---|
| 112 | inline int              getNumWheels() const { | 
|---|
| 113 | return int (m_wheelInfo.size()); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | btAlignedObjectArray<btWheelInfo>       m_wheelInfo; | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | const btWheelInfo&      getWheelInfo(int index) const; | 
|---|
| 120 |  | 
|---|
| 121 | btWheelInfo&    getWheelInfo(int index); | 
|---|
| 122 |  | 
|---|
| 123 | void    updateWheelTransformsWS(btWheelInfo& wheel , bool interpolatedTransform = true); | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | void setBrake(btScalar brake,int wheelIndex); | 
|---|
| 127 |  | 
|---|
| 128 | void    setPitchControl(btScalar pitch) | 
|---|
| 129 | { | 
|---|
| 130 | m_pitchControl = pitch; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void    updateSuspension(btScalar deltaTime); | 
|---|
| 134 |  | 
|---|
| 135 | virtual void    updateFriction(btScalar timeStep); | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 | inline btRigidBody* getRigidBody() | 
|---|
| 140 | { | 
|---|
| 141 | return m_chassisBody; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | const btRigidBody* getRigidBody() const | 
|---|
| 145 | { | 
|---|
| 146 | return m_chassisBody; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | inline int      getRightAxis() const | 
|---|
| 150 | { | 
|---|
| 151 | return m_indexRightAxis; | 
|---|
| 152 | } | 
|---|
| 153 | inline int getUpAxis() const | 
|---|
| 154 | { | 
|---|
| 155 | return m_indexUpAxis; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | inline int getForwardAxis() const | 
|---|
| 159 | { | 
|---|
| 160 | return m_indexForwardAxis; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | ///Worldspace forward vector | 
|---|
| 165 | btVector3 getForwardVector() const | 
|---|
| 166 | { | 
|---|
| 167 | const btTransform& chassisTrans = getChassisWorldTransform(); | 
|---|
| 168 |  | 
|---|
| 169 | btVector3 forwardW ( | 
|---|
| 170 | chassisTrans.getBasis()[0][m_indexForwardAxis], | 
|---|
| 171 | chassisTrans.getBasis()[1][m_indexForwardAxis], | 
|---|
| 172 | chassisTrans.getBasis()[2][m_indexForwardAxis]); | 
|---|
| 173 |  | 
|---|
| 174 | return forwardW; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | ///Velocity of vehicle (positive if velocity vector has same direction as foward vector) | 
|---|
| 178 | btScalar        getCurrentSpeedKmHour() const | 
|---|
| 179 | { | 
|---|
| 180 | return m_currentVehicleSpeedKmHour; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | virtual void    setCoordinateSystem(int rightIndex,int upIndex,int forwardIndex) | 
|---|
| 184 | { | 
|---|
| 185 | m_indexRightAxis = rightIndex; | 
|---|
| 186 | m_indexUpAxis = upIndex; | 
|---|
| 187 | m_indexForwardAxis = forwardIndex; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | }; | 
|---|
| 193 |  | 
|---|
| 194 | class btDefaultVehicleRaycaster : public btVehicleRaycaster | 
|---|
| 195 | { | 
|---|
| 196 | btDynamicsWorld*        m_dynamicsWorld; | 
|---|
| 197 | public: | 
|---|
| 198 | btDefaultVehicleRaycaster(btDynamicsWorld* world) | 
|---|
| 199 | :m_dynamicsWorld(world) | 
|---|
| 200 | { | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result); | 
|---|
| 204 |  | 
|---|
| 205 | }; | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | #endif //RAYCASTVEHICLE_H | 
|---|
| 209 |  | 
|---|