2 * Copyright (c) 2005 Erwin Coumans https://bulletphysics.org
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.
11 #ifndef BT_RAYCASTVEHICLE_H
12 #define BT_RAYCASTVEHICLE_H
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"
22 //class btVehicleTuning;
24 ///rayCast vehicle, very special constraint that turn a rigidbody into a vehicle.
25 class btRaycastVehicle : public btActionInterface
27 btAlignedObjectArray<btVector3> m_forwardWS;
28 btAlignedObjectArray<btVector3> m_axle;
29 btAlignedObjectArray<btScalar> m_forwardImpulse;
30 btAlignedObjectArray<btScalar> m_sideImpulse;
32 ///backwards compatibility
33 int m_userConstraintType;
34 int m_userConstraintId;
41 : m_suspensionStiffness(btScalar(5.88)),
42 m_suspensionCompression(btScalar(0.83)),
43 m_suspensionDamping(btScalar(0.88)),
44 m_maxSuspensionTravelCm(btScalar(500.)),
45 m_frictionSlip(btScalar(10.5)),
46 m_maxSuspensionForce(btScalar(6000.))
49 btScalar m_suspensionStiffness;
50 btScalar m_suspensionCompression;
51 btScalar m_suspensionDamping;
52 btScalar m_maxSuspensionTravelCm;
53 btScalar m_frictionSlip;
54 btScalar m_maxSuspensionForce;
58 btVehicleRaycaster* m_vehicleRaycaster;
59 btScalar m_pitchControl;
60 btScalar m_steeringValue;
61 btScalar m_currentVehicleSpeedKmHour;
63 btRigidBody* m_chassisBody;
67 int m_indexForwardAxis;
69 void defaultInit(const btVehicleTuning& tuning);
72 //constructor to create a car from an existing rigidbody
73 btRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis, btVehicleRaycaster* raycaster);
75 virtual ~btRaycastVehicle();
77 ///btActionInterface interface
78 virtual void updateAction(btCollisionWorld* collisionWorld, btScalar step)
84 ///btActionInterface interface
85 void debugDraw(btIDebugDraw* debugDrawer);
87 const btTransform& getChassisWorldTransform() const;
89 btScalar rayCast(btWheelInfo& wheel);
91 virtual void updateVehicle(btScalar step);
93 void resetSuspension();
95 btScalar getSteeringValue(int wheel) const;
97 void setSteeringValue(btScalar steering, int wheel);
99 void applyEngineForce(btScalar force, int wheel);
101 const btTransform& getWheelTransformWS(int wheelIndex) const;
103 void updateWheelTransform(int wheelIndex, bool interpolatedTransform = true);
105 // void setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth);
107 btWheelInfo& addWheel(const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0, const btVector3& wheelAxleCS, btScalar suspensionRestLength, btScalar wheelRadius, const btVehicleTuning& tuning, bool isFrontWheel);
109 inline int getNumWheels() const
111 return int(m_wheelInfo.size());
114 btAlignedObjectArray<btWheelInfo> m_wheelInfo;
116 const btWheelInfo& getWheelInfo(int index) const;
118 btWheelInfo& getWheelInfo(int index);
120 void updateWheelTransformsWS(btWheelInfo& wheel, bool interpolatedTransform = true);
122 void setBrake(btScalar brake, int wheelIndex);
124 void setPitchControl(btScalar pitch)
126 m_pitchControl = pitch;
129 void updateSuspension(btScalar deltaTime);
131 virtual void updateFriction(btScalar timeStep);
133 inline btRigidBody* getRigidBody()
135 return m_chassisBody;
138 const btRigidBody* getRigidBody() const
140 return m_chassisBody;
143 inline int getRightAxis() const
145 return m_indexRightAxis;
147 inline int getUpAxis() const
149 return m_indexUpAxis;
152 inline int getForwardAxis() const
154 return m_indexForwardAxis;
157 ///Worldspace forward vector
158 btVector3 getForwardVector() const
160 const btTransform& chassisTrans = getChassisWorldTransform();
163 chassisTrans.getBasis()[0][m_indexForwardAxis],
164 chassisTrans.getBasis()[1][m_indexForwardAxis],
165 chassisTrans.getBasis()[2][m_indexForwardAxis]);
170 ///Velocity of vehicle (positive if velocity vector has same direction as foward vector)
171 btScalar getCurrentSpeedKmHour() const
173 return m_currentVehicleSpeedKmHour;
176 virtual void setCoordinateSystem(int rightIndex, int upIndex, int forwardIndex)
178 m_indexRightAxis = rightIndex;
179 m_indexUpAxis = upIndex;
180 m_indexForwardAxis = forwardIndex;
183 ///backwards compatibility
184 int getUserConstraintType() const
186 return m_userConstraintType;
189 void setUserConstraintType(int userConstraintType)
191 m_userConstraintType = userConstraintType;
194 void setUserConstraintId(int uid)
196 m_userConstraintId = uid;
199 int getUserConstraintId() const
201 return m_userConstraintId;
205 class btDefaultVehicleRaycaster : public btVehicleRaycaster
207 btDynamicsWorld* m_dynamicsWorld;
210 btDefaultVehicleRaycaster(btDynamicsWorld* world)
211 : m_dynamicsWorld(world)
215 virtual void* castRay(const btVector3& from, const btVector3& to, btVehicleRaycasterResult& result);
218 #endif //BT_RAYCASTVEHICLE_H