SuperTuxKart
btKart.hpp
1 /*
2  * Copyright (C) 2005-2015 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 BT_KART_HPP
12 #define BT_KART_HPP
13 
14 #include "BulletDynamics/Dynamics/btRigidBody.h"
15 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
16 #include "physics/btKartRaycast.hpp"
17 class btDynamicsWorld;
18 #include "LinearMath/btAlignedObjectArray.h"
19 #include "BulletDynamics/Vehicle/btWheelInfo.h"
20 #include "BulletDynamics/Dynamics/btActionInterface.h"
21 
22 #include "config/stk_config.hpp"
23 
24 class btVehicleTuning;
25 class Kart;
26 struct btWheelContactPoint;
27 
31 class btKart : public btActionInterface
32 {
33 public:
35  {
36  public:
37 
39  :m_suspensionStiffness(btScalar(5.88)),
40  m_suspensionCompression(btScalar(0.83)),
41  m_suspensionDamping(btScalar(0.88)),
42  m_maxSuspensionTravel(btScalar(5.)),
43  m_frictionSlip(btScalar(10.5)),
44  m_maxSuspensionForce(btScalar(6000.))
45  {
46  } // btVehicleTuning
47 
48  btScalar m_suspensionStiffness;
49  btScalar m_suspensionCompression;
50  btScalar m_suspensionDamping;
51  btScalar m_maxSuspensionTravel;
52  btScalar m_frictionSlip;
53  btScalar m_maxSuspensionForce;
54 
55  }; // class btVehicleTuning
56 
57 private:
58 
59  btAlignedObjectArray<btVector3> m_forwardWS;
60  btAlignedObjectArray<btVector3> m_axle;
61  btAlignedObjectArray<btScalar> m_forwardImpulse;
62  btAlignedObjectArray<btScalar> m_sideImpulse;
63 
66  int m_userConstraintId;
67 
68  static btRigidBody& getFixedBody();
69  btScalar calcRollingFriction(btWheelContactPoint& contactPoint);
70 
71  btScalar m_damping;
72  btVehicleRaycaster *m_vehicleRaycaster;
73 
79 
82 
85 
88 
91 
93  btRigidBody *m_chassisBody;
94 
96  btRigidBody m_fixed_body;
97 
100 
107 
112 
116  btScalar m_min_speed;
117 
120  btScalar m_max_speed;
121 
124 
125  btAlignedObjectArray<btWheelInfo> m_wheelInfo;
126 
127  void defaultInit();
128  btScalar rayCast(btWheelInfo& wheel, const btVector3& ray);
129  void updateWheelTransformsWS(btWheelInfo& wheel,
130  btTransform chassis_trans,
131  bool interpolatedTransform=true,
132  float fraction = 1.0f);
133 
134 public:
135 
142  btKart(btRigidBody* chassis,
143  btVehicleRaycaster* raycaster,
144  Kart *kart);
145  virtual ~btKart();
146  void reset();
147  void debugDraw(btIDebugDraw* debugDrawer);
148  const btTransform& getChassisWorldTransform() const;
149  btScalar rayCast(unsigned int index, float fraction=1.0f);
150  virtual void updateVehicle(btScalar step);
151  void resetSuspension();
152  btScalar getSteeringValue(int wheel) const;
153  void setSteeringValue(btScalar steering,int wheel);
154  void applyEngineForce(btScalar force, int wheel);
155  const btTransform& getWheelTransformWS( int wheelIndex ) const;
156  void updateWheelTransform(int wheelIndex,
157  bool interpolatedTransform=true);
158  btWheelInfo& addWheel(const btVector3& connectionPointCS0,
159  const btVector3& wheelDirectionCS0,
160  const btVector3& wheelAxleCS,
161  btScalar suspensionRestLength,
162  btScalar wheelRadius,
163  const btVehicleTuning& tuning,
164  bool isFrontWheel);
165  const btWheelInfo& getWheelInfo(int index) const;
166  btWheelInfo& getWheelInfo(int index);
168  void setAllBrakes(btScalar brake);
169  void updateSuspension(btScalar deltaTime);
170  virtual void updateFriction(btScalar timeStep);
171  void setSliding(bool active);
172  void instantSpeedIncreaseTo(btScalar speed);
173  void adjustSpeed(btScalar min_speed, btScalar max_speed);
174  void updateAllWheelPositions();
175  void getVisualContactPoint(const btTransform& chassis_trans,
176  btVector3 *left, btVector3 *right);
177  // ------------------------------------------------------------------------
180  {
182  } // visualWheelsTouchGround
183  // ------------------------------------------------------------------------
185  virtual void updateAction(btCollisionWorld* collisionWorld,
186  btScalar step)
187  {
188  (void) collisionWorld;
189  updateVehicle(step);
190  } // updateAction
191  // ------------------------------------------------------------------------
193  inline int getNumWheels() const { return int(m_wheelInfo.size());}
194  // ------------------------------------------------------------------------
196  inline btRigidBody* getRigidBody() { return m_chassisBody; }
197  // ------------------------------------------------------------------------
199  const btRigidBody* getRigidBody() const { return m_chassisBody; }
200  // ------------------------------------------------------------------------
202  inline int getRightAxis() const { return m_indexRightAxis; }
203  // ------------------------------------------------------------------------
205  inline int getUpAxis() const { return m_indexUpAxis; }
206  // ------------------------------------------------------------------------
208  inline int getForwardAxis() const { return m_indexForwardAxis; }
209  // ------------------------------------------------------------------------
212  // ------------------------------------------------------------------------
213  void setUserConstraintType(int userConstraintType)
214  {
215  m_userConstraintType = userConstraintType;
216  } // setUserConstraintType
217  // ------------------------------------------------------------------------
218  void setUserConstraintId(int uid) { m_userConstraintId = uid; }
219  // ------------------------------------------------------------------------
220  int getUserConstraintId() const { return m_userConstraintId; }
221  // ------------------------------------------------------------------------
223  unsigned int getNumWheelsOnGround() const {return m_num_wheels_on_ground;}
224  // ------------------------------------------------------------------------
228  void setTimedCentralImpulse(uint16_t t, const btVector3 &imp,
229  bool rewind = false)
230  {
231  // Only add impulse if no other impulse is active.
232  if (m_ticks_additional_impulse > 0 && !rewind) return;
233  m_additional_impulse = imp;
235  } // setTimedImpulse
236  // ------------------------------------------------------------------------
238  uint16_t getCentralImpulseTicks() const
239  { return m_ticks_additional_impulse; }
240  // ------------------------------------------------------------------------
241  const btVector3& getAdditionalImpulse() const
242  { return m_additional_impulse; }
243  // ------------------------------------------------------------------------
248  void setTimedRotation(uint16_t t, float rot_in_y_axis)
249  {
250  if (t > 0)
251  {
253  rot_in_y_axis / (stk_config->ticks2Time(t));
254  }
256  } // setTimedTorque
257  // ------------------------------------------------------------------------
258  float getTimedRotation() const { return m_additional_rotation; }
259  // ------------------------------------------------------------------------
260  uint16_t getTimedRotationTicks() const
261  { return m_ticks_additional_rotation; }
262  // ------------------------------------------------------------------------
264  void setMaxSpeed(float new_max_speed)
265  {
266  // Only change m_max_speed if it has not been set (<0), or
267  // the new value is smaller than the current maximum. For example,
268  // overworld will set the max_speed to 0 in case of teleporting to
269  // a bubble, but set it again later (based on zipper etc activated).
270  // We need to make sure that the 0 is maintained.
271  if(m_max_speed <0 || m_max_speed > new_max_speed)
272  m_max_speed = new_max_speed;
273  } // setMaxSpeed
274  // ------------------------------------------------------------------------
277  virtual void resetMaxSpeed() { m_max_speed = -1.0f; }
278  // ------------------------------------------------------------------------
279  virtual void resetMinSpeed() { m_min_speed = 0.0f; }
280  // ------------------------------------------------------------------------
284  void setMinSpeed(float s)
285  {
286  if(s > m_min_speed) m_min_speed = s;
287  }
288  // ------------------------------------------------------------------------
290  btScalar getMinSpeed() const { return m_min_speed; }
291 }; // class btKart
292 
293 #endif //BT_KART_HPP
294 
The main kart class.
Definition: kart.hpp:72
float ticks2Time(int ticks)
Converts a tick value (in physics time step size) into seconds.
Definition: stk_config.hpp:314
Definition: btKart.hpp:35
rayCast vehicle, very special constraint that turn a rigidbody into a vehicle.
Definition: btKart.hpp:32
btRigidBody * m_chassisBody
The rigid body that is the chassis of the kart.
Definition: btKart.hpp:93
btScalar m_max_speed
Maximum speed for the kart.
Definition: btKart.hpp:120
virtual void updateAction(btCollisionWorld *collisionWorld, btScalar step)
btActionInterface interface.
Definition: btKart.hpp:185
btScalar getMinSpeed() const
Returns the minimum speed for this kart.
Definition: btKart.hpp:290
void setTimedRotation(uint16_t t, float rot_in_y_axis)
Sets a rotation that is applied over a certain amount of time (to avoid a too rapid changes in the ka...
Definition: btKart.hpp:248
const btRigidBody * getRigidBody() const
Returns the chassis (rigid) body.
Definition: btKart.hpp:199
void setMaxSpeed(float new_max_speed)
Sets the maximum speed for this kart.
Definition: btKart.hpp:264
btRigidBody * getRigidBody()
Returns the chassis (rigid) body.
Definition: btKart.hpp:196
void reset()
Resets the kart before a (re)start, to make sure all physics variable are properly defined.
Definition: btKart.cpp:105
btRigidBody m_fixed_body
Used to replace the ground object.
Definition: btKart.hpp:96
int getRightAxis() const
Returns the index of the right axis.
Definition: btKart.hpp:202
bool m_visual_wheels_touch_ground
True if the visual wheels touch the ground.
Definition: btKart.hpp:123
btScalar m_min_speed
Minimum speed for the kart.
Definition: btKart.hpp:116
Kart * m_kart
The STK kart object which uses this vehicle.
Definition: btKart.hpp:111
void updateAllWheelTransformsWS()
Updates all wheel transform informations.
Definition: btKart.cpp:212
int m_userConstraintType
backwards compatibility
Definition: btKart.hpp:65
uint16_t getCentralImpulseTicks() const
Returns the time an additional impulse is activated.
Definition: btKart.hpp:238
int getNumWheels() const
Returns the number of wheels of this vehicle.
Definition: btKart.hpp:193
void getVisualContactPoint(const btTransform &chassis_trans, btVector3 *left, btVector3 *right)
Returns the contact point of a visual wheel.
Definition: btKart.cpp:366
bool visualWheelsTouchGround() const
Returns true if both rear visual wheels touch the ground.
Definition: btKart.hpp:179
uint16_t m_ticks_additional_rotation
Duration over which the additional rotation is applied.
Definition: btKart.hpp:90
void setTimedCentralImpulse(uint16_t t, const btVector3 &imp, bool rewind=false)
Sets an impulse that is applied for a certain amount of time.
Definition: btKart.hpp:228
virtual void resetMaxSpeed()
Resets the maximum so any new maximum value from the application will be accepted.
Definition: btKart.hpp:277
int getForwardAxis() const
Returns the index of the forward axis.
Definition: btKart.hpp:208
int m_indexRightAxis
Index of the right axis.
Definition: btKart.hpp:102
void setMinSpeed(float s)
Sets the minimum speed for this kart, only if the new value is higher.
Definition: btKart.hpp:284
int m_indexUpAxis
Index of the up axis.
Definition: btKart.hpp:104
float m_additional_rotation
Additional rotation in y-axis that is applied over a certain amount of time.
Definition: btKart.hpp:87
int getUserConstraintType() const
Backwards compatibility.
Definition: btKart.hpp:211
uint16_t m_ticks_additional_impulse
The time the additional impulse should be applied.
Definition: btKart.hpp:84
int m_num_wheels_on_ground
Number of wheels that touch the ground.
Definition: btKart.hpp:99
int m_indexForwardAxis
Index of the forward axis.
Definition: btKart.hpp:106
btKart(btRigidBody *chassis, btVehicleRaycaster *raycaster, Kart *kart)
Constructor to create a car from an existing rigidbody.
Definition: btKart.cpp:36
btVector3 m_additional_impulse
An additional impulse that is applied for a certain amount of time.
Definition: btKart.hpp:81
unsigned int getNumWheelsOnGround() const
Returns the number of wheels on the ground.
Definition: btKart.hpp:223
void adjustSpeed(btScalar min_speed, btScalar max_speed)
Adjusts the velocity of this kart to be at least the specified minimum, and less than or equal to the...
Definition: btKart.cpp:963
bool m_allow_sliding
Sliding (skidding) will only be permited when this is true.
Definition: btKart.hpp:78
void setSliding(bool active)
Enables or disables sliding.
Definition: btKart.cpp:951
int getUpAxis() const
Returns the index of the up axis.
Definition: btKart.hpp:205
Definition: btKart.cpp:643