vehicle_body.h
1 #ifndef VEHICLE_BODY_H
2 #define VEHICLE_BODY_H
3 
4 #include "scene/3d/physics_body.h"
5 
6 class VehicleBody;
7 
8 class VehicleWheel : public Spatial {
9 
10  OBJ_TYPE(VehicleWheel,Spatial);
11 
12 friend class VehicleBody;
13 
14 
15  Transform m_worldTransform;
16  Transform local_xform;
17  bool engine_traction;
18  bool steers;
19 
20 
21  Vector3 m_chassisConnectionPointCS; //const
22  Vector3 m_wheelDirectionCS;//const
23  Vector3 m_wheelAxleCS; // const or modified by steering
24 
25  real_t m_suspensionRestLength;
26  real_t m_maxSuspensionTravelCm;
27  real_t m_wheelRadius;
28 
29  real_t m_suspensionStiffness;
30  real_t m_wheelsDampingCompression;
31  real_t m_wheelsDampingRelaxation;
32  real_t m_frictionSlip;
33  real_t m_maxSuspensionForce;
34  bool m_bIsFrontWheel;
35 
36  VehicleBody *body;
37 
38 // btVector3 m_wheelAxleCS; // const or modified by steering ?
39 
40  real_t m_steering;
41  real_t m_rotation;
42  real_t m_deltaRotation;
43  real_t m_rollInfluence;
44  //real_t m_engineForce;
45  real_t m_brake;
46 
47  real_t m_clippedInvContactDotSuspension;
48  real_t m_suspensionRelativeVelocity;
49  //calculated by suspension
50  real_t m_wheelsSuspensionForce;
51  real_t m_skidInfo;
52 
53 
54  struct RaycastInfo {
55  //set by raycaster
56  Vector3 m_contactNormalWS;//contactnormal
57  Vector3 m_contactPointWS;//raycast hitpoint
58  real_t m_suspensionLength;
59  Vector3 m_hardPointWS;//raycast starting point
60  Vector3 m_wheelDirectionWS; //direction in worldspace
61  Vector3 m_wheelAxleWS; // axle in worldspace
62  bool m_isInContact;
63  PhysicsBody* m_groundObject; //could be general void* ptr
64  } m_raycastInfo;
65 
66  void _update(PhysicsDirectBodyState *s);
67 
68 protected:
69  void _notification(int p_what);
70  static void _bind_methods();
71 
72 public:
73 
74  void set_radius(float p_radius);
75  float get_radius() const;
76 
77  void set_suspension_rest_length(float p_length);
78  float get_suspension_rest_length() const;
79 
80  void set_suspension_travel(float p_length);
81  float get_suspension_travel() const;
82 
83  void set_suspension_stiffness(float p_value);
84  float get_suspension_stiffness() const;
85 
86  void set_suspension_max_force(float p_value);
87  float get_suspension_max_force() const;
88 
89  void set_damping_compression(float p_value);
90  float get_damping_compression() const;
91 
92  void set_damping_relaxation(float p_value);
93  float get_damping_relaxation() const;
94 
95  void set_friction_slip(float p_value);
96  float get_friction_slip() const;
97 
98  void set_use_as_traction(bool p_enable);
99  bool is_used_as_traction() const;
100 
101  void set_use_as_steering(bool p_enabled);
102  bool is_used_as_steering() const;
103 
104  VehicleWheel();
105 
106 };
107 
108 
109 class VehicleBody : public PhysicsBody {
110 
111  OBJ_TYPE(VehicleBody,PhysicsBody);
112 
113  real_t mass;
114  real_t friction;
115 
116  float engine_force;
117  float brake;
118 
119  Vector3 linear_velocity;
120  Vector3 angular_velocity;
121  bool ccd;
122 
123  real_t m_pitchControl;
124  real_t m_steeringValue;
125  real_t m_currentVehicleSpeedKmHour;
126 
127  Set<RID> exclude;
128 
129  Vector<Vector3> m_forwardWS;
130  Vector<Vector3> m_axle;
131  Vector<real_t> m_forwardImpulse;
132  Vector<real_t> m_sideImpulse;
133 
134  struct btVehicleWheelContactPoint {
135  PhysicsDirectBodyState *m_s;
136  PhysicsBody* m_body1;
137  Vector3 m_frictionPositionWorld;
138  Vector3 m_frictionDirectionWorld;
139  real_t m_jacDiagABInv;
140  real_t m_maxImpulse;
141 
142 
143  btVehicleWheelContactPoint(PhysicsDirectBodyState *s,PhysicsBody* body1,const Vector3& frictionPosWorld,const Vector3& frictionDirectionWorld, real_t maxImpulse);
144  };
145 
146  void _resolve_single_bilateral(PhysicsDirectBodyState *s, const Vector3& pos1, PhysicsBody* body2, const Vector3& pos2, const Vector3& normal, real_t& impulse);
147  real_t _calc_rolling_friction(btVehicleWheelContactPoint& contactPoint);
148 
149  void _update_friction(PhysicsDirectBodyState *s);
150  void _update_suspension(PhysicsDirectBodyState *s);
151  real_t _ray_cast(int p_idx,PhysicsDirectBodyState *s);
152  void _update_wheel_transform(VehicleWheel& wheel ,PhysicsDirectBodyState *s);
153  void _update_wheel(int p_idx,PhysicsDirectBodyState *s);
154 
155 
156 
157 friend class VehicleWheel;
158  Vector<VehicleWheel*> wheels;
159 
160  static void _bind_methods();
161 
162  void _direct_state_changed(Object *p_state);
163 public:
164 
165 
166  void set_mass(real_t p_mass);
167  real_t get_mass() const;
168 
169  void set_friction(real_t p_friction);
170  real_t get_friction() const;
171 
172  void set_engine_force(float p_engine_force);
173  float get_engine_force() const;
174 
175  void set_brake(float p_force);
176  float get_brake() const;
177 
178  void set_steering(float p_steering);
179  float get_steering() const;
180 
181 
182  VehicleBody();
183 };
184 
185 #endif // VEHICLE_BODY_H
Definition: vector3.h:38
Definition: transform.h:38
Definition: vehicle_body.h:109
Definition: object.h:317
Definition: vehicle_body.h:8
Definition: spatial.h:56
Definition: physics_body.h:37