[1963] | 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 | |
---|
| 21 | class btVehicleTuning; |
---|
| 22 | |
---|
| 23 | ///rayCast vehicle, very special constraint that turn a rigidbody into a vehicle. |
---|
| 24 | class btRaycastVehicle : public btTypedConstraint |
---|
| 25 | { |
---|
| 26 | |
---|
| 27 | btAlignedObjectArray<btVector3> m_forwardWS; |
---|
| 28 | btAlignedObjectArray<btVector3> m_axle; |
---|
| 29 | btAlignedObjectArray<btScalar> m_forwardImpulse; |
---|
| 30 | btAlignedObjectArray<btScalar> m_sideImpulse; |
---|
| 31 | |
---|
| 32 | public: |
---|
| 33 | class btVehicleTuning |
---|
| 34 | { |
---|
| 35 | public: |
---|
| 36 | |
---|
| 37 | btVehicleTuning() |
---|
| 38 | :m_suspensionStiffness(btScalar(5.88)), |
---|
| 39 | m_suspensionCompression(btScalar(0.83)), |
---|
| 40 | m_suspensionDamping(btScalar(0.88)), |
---|
| 41 | m_maxSuspensionTravelCm(btScalar(500.)), |
---|
| 42 | m_frictionSlip(btScalar(10.5)) |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | btScalar m_suspensionStiffness; |
---|
| 46 | btScalar m_suspensionCompression; |
---|
| 47 | btScalar m_suspensionDamping; |
---|
| 48 | btScalar m_maxSuspensionTravelCm; |
---|
| 49 | btScalar m_frictionSlip; |
---|
| 50 | |
---|
| 51 | }; |
---|
| 52 | private: |
---|
| 53 | |
---|
| 54 | btScalar m_tau; |
---|
| 55 | btScalar m_damping; |
---|
| 56 | btVehicleRaycaster* m_vehicleRaycaster; |
---|
| 57 | btScalar m_pitchControl; |
---|
| 58 | btScalar m_steeringValue; |
---|
| 59 | btScalar m_currentVehicleSpeedKmHour; |
---|
| 60 | |
---|
| 61 | btRigidBody* m_chassisBody; |
---|
| 62 | |
---|
| 63 | int m_indexRightAxis; |
---|
| 64 | int m_indexUpAxis; |
---|
| 65 | int m_indexForwardAxis; |
---|
| 66 | |
---|
| 67 | void defaultInit(const btVehicleTuning& tuning); |
---|
| 68 | |
---|
| 69 | public: |
---|
| 70 | |
---|
| 71 | //constructor to create a car from an existing rigidbody |
---|
| 72 | btRaycastVehicle(const btVehicleTuning& tuning,btRigidBody* chassis, btVehicleRaycaster* raycaster ); |
---|
| 73 | |
---|
| 74 | virtual ~btRaycastVehicle() ; |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | const btTransform& getChassisWorldTransform() const; |
---|
| 78 | |
---|
| 79 | btScalar rayCast(btWheelInfo& wheel); |
---|
| 80 | |
---|
| 81 | virtual void updateVehicle(btScalar step); |
---|
| 82 | |
---|
| 83 | void resetSuspension(); |
---|
| 84 | |
---|
| 85 | btScalar getSteeringValue(int wheel) const; |
---|
| 86 | |
---|
| 87 | void setSteeringValue(btScalar steering,int wheel); |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | void applyEngineForce(btScalar force, int wheel); |
---|
| 91 | |
---|
| 92 | const btTransform& getWheelTransformWS( int wheelIndex ) const; |
---|
| 93 | |
---|
| 94 | void updateWheelTransform( int wheelIndex, bool interpolatedTransform = true ); |
---|
| 95 | |
---|
| 96 | void setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth); |
---|
| 97 | |
---|
| 98 | btWheelInfo& addWheel( const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0,const btVector3& wheelAxleCS,btScalar suspensionRestLength,btScalar wheelRadius,const btVehicleTuning& tuning, bool isFrontWheel); |
---|
| 99 | |
---|
| 100 | inline int getNumWheels() const { |
---|
| 101 | return int (m_wheelInfo.size()); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | btAlignedObjectArray<btWheelInfo> m_wheelInfo; |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | const btWheelInfo& getWheelInfo(int index) const; |
---|
| 108 | |
---|
| 109 | btWheelInfo& getWheelInfo(int index); |
---|
| 110 | |
---|
| 111 | void updateWheelTransformsWS(btWheelInfo& wheel , bool interpolatedTransform = true); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | void setBrake(btScalar brake,int wheelIndex); |
---|
| 115 | |
---|
| 116 | void setPitchControl(btScalar pitch) |
---|
| 117 | { |
---|
| 118 | m_pitchControl = pitch; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void updateSuspension(btScalar deltaTime); |
---|
| 122 | |
---|
| 123 | virtual void updateFriction(btScalar timeStep); |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | inline btRigidBody* getRigidBody() |
---|
| 128 | { |
---|
| 129 | return m_chassisBody; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | const btRigidBody* getRigidBody() const |
---|
| 133 | { |
---|
| 134 | return m_chassisBody; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | inline int getRightAxis() const |
---|
| 138 | { |
---|
| 139 | return m_indexRightAxis; |
---|
| 140 | } |
---|
| 141 | inline int getUpAxis() const |
---|
| 142 | { |
---|
| 143 | return m_indexUpAxis; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | inline int getForwardAxis() const |
---|
| 147 | { |
---|
| 148 | return m_indexForwardAxis; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | ///Worldspace forward vector |
---|
| 153 | btVector3 getForwardVector() const |
---|
| 154 | { |
---|
| 155 | const btTransform& chassisTrans = getChassisWorldTransform(); |
---|
| 156 | |
---|
| 157 | btVector3 forwardW ( |
---|
| 158 | chassisTrans.getBasis()[0][m_indexForwardAxis], |
---|
| 159 | chassisTrans.getBasis()[1][m_indexForwardAxis], |
---|
| 160 | chassisTrans.getBasis()[2][m_indexForwardAxis]); |
---|
| 161 | |
---|
| 162 | return forwardW; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | ///Velocity of vehicle (positive if velocity vector has same direction as foward vector) |
---|
| 166 | btScalar getCurrentSpeedKmHour() const |
---|
| 167 | { |
---|
| 168 | return m_currentVehicleSpeedKmHour; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | virtual void setCoordinateSystem(int rightIndex,int upIndex,int forwardIndex) |
---|
| 172 | { |
---|
| 173 | m_indexRightAxis = rightIndex; |
---|
| 174 | m_indexUpAxis = upIndex; |
---|
| 175 | m_indexForwardAxis = forwardIndex; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | virtual void buildJacobian() |
---|
| 179 | { |
---|
| 180 | //not yet |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | virtual void solveConstraint(btScalar timeStep) |
---|
| 184 | { |
---|
| 185 | (void)timeStep; |
---|
| 186 | //not yet |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | class btDefaultVehicleRaycaster : public btVehicleRaycaster |
---|
| 193 | { |
---|
| 194 | btDynamicsWorld* m_dynamicsWorld; |
---|
| 195 | public: |
---|
| 196 | btDefaultVehicleRaycaster(btDynamicsWorld* world) |
---|
| 197 | :m_dynamicsWorld(world) |
---|
| 198 | { |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result); |
---|
| 202 | |
---|
| 203 | }; |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | #endif //RAYCASTVEHICLE_H |
---|
| 207 | |
---|