update Bullet 2.x with latest changes, notice that the integration is not finished...
[blender.git] / extern / bullet2 / src / BulletDynamics / Vehicle / btRaycastVehicle.h
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
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 public:
27         class btVehicleTuning
28                 {
29                         public:
30
31                         btVehicleTuning()
32                                 :m_suspensionStiffness(5.88f),
33                                 m_suspensionCompression(0.83f),
34                                 m_suspensionDamping(0.88f),
35                                 m_maxSuspensionTravelCm(500.f),
36                                 m_frictionSlip(10.5f)
37                         {
38                         }
39                         float   m_suspensionStiffness;
40                         float   m_suspensionCompression;
41                         float   m_suspensionDamping;
42                         float   m_maxSuspensionTravelCm;
43                         float   m_frictionSlip;
44
45                 };
46 private:
47
48         btScalar        m_tau;
49         btScalar        m_damping;
50         btVehicleRaycaster*     m_vehicleRaycaster;
51         float           m_pitchControl;
52         float   m_steeringValue; 
53         float m_currentVehicleSpeedKmHour;
54
55         btRigidBody* m_chassisBody;
56
57         int m_indexRightAxis;
58         int m_indexUpAxis;
59         int     m_indexForwardAxis;
60
61         void defaultInit(const btVehicleTuning& tuning);
62
63 public:
64
65         //constructor to create a car from an existing rigidbody
66         btRaycastVehicle(const btVehicleTuning& tuning,btRigidBody* chassis,    btVehicleRaycaster* raycaster );
67
68         virtual ~btRaycastVehicle() ;
69
70                 
71
72
73
74         btScalar rayCast(btWheelInfo& wheel);
75
76         virtual void updateVehicle(btScalar step);
77
78         void resetSuspension();
79
80         btScalar        getSteeringValue(int wheel) const;
81
82         void    setSteeringValue(btScalar steering,int wheel);
83
84
85         void    applyEngineForce(btScalar force, int wheel);
86
87         const btTransform&      getWheelTransformWS( int wheelIndex ) const;
88
89         void    updateWheelTransform( int wheelIndex );
90         
91         void    setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth);
92
93         btWheelInfo&    addWheel( const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0,const btVector3& wheelAxleCS,btScalar suspensionRestLength,btScalar wheelRadius,const btVehicleTuning& tuning, bool isFrontWheel);
94
95         inline int              getNumWheels() const {
96                 return int (m_wheelInfo.size());
97         }
98         
99         std::vector<btWheelInfo>        m_wheelInfo;
100
101
102         const btWheelInfo&      getWheelInfo(int index) const;
103
104         btWheelInfo&    getWheelInfo(int index);
105
106         void    updateWheelTransformsWS(btWheelInfo& wheel );
107
108         
109         void setBrake(float brake,int wheelIndex);
110
111         void    setPitchControl(float pitch)
112         {
113                 m_pitchControl = pitch;
114         }
115         
116         void    updateSuspension(btScalar deltaTime);
117
118         void    updateFriction(btScalar timeStep);
119
120
121
122         inline btRigidBody* getRigidBody()
123         {
124                 return m_chassisBody;
125         }
126
127         const btRigidBody* getRigidBody() const
128         {
129                 return m_chassisBody;
130         }
131
132         inline int      getRightAxis() const
133         {
134                 return m_indexRightAxis;
135         }
136         inline int getUpAxis() const
137         {
138                 return m_indexUpAxis;
139         }
140
141         inline int getForwardAxis() const
142         {
143                 return m_indexForwardAxis;
144         }
145
146         virtual void    setCoordinateSystem(int rightIndex,int upIndex,int forwardIndex)
147         {
148                 m_indexRightAxis = rightIndex;
149                 m_indexUpAxis = upIndex;
150                 m_indexForwardAxis = forwardIndex;
151         }
152
153         virtual void    buildJacobian()
154         {
155                 //not yet
156         }
157
158         virtual void    solveConstraint(btScalar        timeStep)
159         {
160                 //not yet
161         }
162
163
164 };
165
166 class btDefaultVehicleRaycaster : public btVehicleRaycaster
167 {
168         btDynamicsWorld*        m_dynamicsWorld;
169 public:
170         btDefaultVehicleRaycaster(btDynamicsWorld* world)
171                 :m_dynamicsWorld(world)
172         {
173         }
174
175         virtual void* castRay(const btVector3& from,const btVector3& to, btVehicleRaycasterResult& result);
176
177 };
178
179
180 #endif //RAYCASTVEHICLE_H
181