RaycastVehicle.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Scene/LogicComponent.h"
  24. #include "../Physics/PhysicsUtils.h"
  25. #include "../Physics/RigidBody.h"
  26. namespace Urho3D
  27. {
  28. struct RaycastVehicleData;
  29. class URHO3D_API RaycastVehicle : public LogicComponent
  30. {
  31. URHO3D_OBJECT(RaycastVehicle, LogicComponent);
  32. public:
  33. /// Construct.
  34. explicit RaycastVehicle(Urho3D::Context* context);
  35. /// Destruct.
  36. ~RaycastVehicle() override;
  37. /// Register object factory and attributes.
  38. static void RegisterObject(Context* context);
  39. /// Handle enabled/disabled state change.
  40. void OnSetEnabled() override;
  41. /// Perform post-load after deserialization. Acquire the components from the scene nodes.
  42. void ApplyAttributes() override;
  43. /// Add a wheel. All parameters are relative to RigidBody / node.
  44. void AddWheel(Node* wheelNode, Vector3 wheelDirection, Vector3 wheelAxle, float restLength, float wheelRadius, bool frontWheel);
  45. /// Reset all suspension.
  46. void ResetSuspension();
  47. /// Update transform for particular wheel.
  48. void UpdateWheelTransform(int wheel, bool interpolated);
  49. /// Set steering value of particular wheel.
  50. void SetSteeringValue(int wheel, float steeringValue);
  51. /// Set suspension stiffness for particular wheel.
  52. void SetWheelSuspensionStiffness(int wheel, float stiffness);
  53. /// Set wheel max suspension force. Good results are often obtained by a value that is about 3x to 4x the vehicle weight.
  54. void SetWheelMaxSuspensionForce(int wheel, float force);
  55. /// Set wheel damping relaxation.
  56. void SetWheelDampingRelaxation(int wheel, float damping);
  57. /// Set wheel damping compression.
  58. void SetWheelDampingCompression(int wheel, float compression);
  59. /// Set wheel friction slip.
  60. void SetWheelFrictionSlip(int wheel, float slip);
  61. /// Set wheel roll influence.
  62. void SetWheelRollInfluence(int wheel, float rollInfluence);
  63. /// Set engine force for the wheel.
  64. void SetEngineForce(int wheel, float force);
  65. /// Set hand brake (wheel rotation blocking force).
  66. void SetBrake(int wheel, float force);
  67. /// Set wheel radius.
  68. void SetWheelRadius(int wheel, float wheelRadius);
  69. /// Sets node initial positions.
  70. void ResetWheels();
  71. /// Set sliding factor 0 <= x <= 1. The less the value, more sliding.
  72. void SetWheelSkidInfo(int wheel, float factor);
  73. /// True if wheel touches ground (raycast hits something).
  74. bool WheelIsGrounded(int wheel) const;
  75. /// Set maximum suspension travel value.
  76. void SetMaxSuspensionTravel(int wheel, float maxSuspensionTravel);
  77. /// Set wheel direction vector.
  78. void SetWheelDirection(int wheel, Vector3 direction);
  79. /// Set wheel axle vector.
  80. void SetWheelAxle(int wheel, Vector3 axle);
  81. /// Set side speed which is considered sliding.
  82. /// @property
  83. void SetMaxSideSlipSpeed(float speed);
  84. /// Set cumulative skid info.
  85. void SetWheelSkidInfoCumulative(int wheel, float skid);
  86. /// Set revolution per minute value for when wheel doesn't touch ground. If set to 0 (or not set), calculated from engine force (probably not what you want).
  87. /// @property
  88. void SetInAirRPM(float rpm);
  89. /// Set the coordinate system. The default is (0, 1, 2).
  90. /// @property
  91. void SetCoordinateSystem(const IntVector3& coordinateSystem = RIGHT_FORWARD_UP);
  92. /// Init the vehicle component after creation.
  93. void Init();
  94. /// Perform fixed step pre-update.
  95. void FixedUpdate(float timeStep) override;
  96. /// Perform fixed step post-update.
  97. void FixedPostUpdate(float timeStep) override;
  98. /// Perform variable step post-update.
  99. void PostUpdate(float timeStep) override;
  100. /// Get wheel position relative to RigidBody.
  101. Vector3 GetWheelPosition(int wheel);
  102. /// Get wheel rotation relative to RigidBody.
  103. Quaternion GetWheelRotation(int wheel);
  104. /// Get wheel connection point relative to RigidBody.
  105. Vector3 GetWheelConnectionPoint(int wheel) const;
  106. /// Get number of attached wheels.
  107. /// @property
  108. int GetNumWheels() const;
  109. /// Get node of the wheel.
  110. Node* GetWheelNode(int wheel) const;
  111. /// Get steering value of particular wheel.
  112. float GetSteeringValue(int wheel) const;
  113. /// Get suspension stiffness for particular wheel.
  114. float GetWheelSuspensionStiffness(int wheel) const;
  115. /// Get wheel max suspension force.
  116. float GetWheelMaxSuspensionForce(int wheel) const;
  117. /// Get wheel damping relaxation.
  118. float GetWheelDampingRelaxation(int wheel) const;
  119. /// Get wheel damping compression.
  120. float GetWheelDampingCompression(int wheel) const;
  121. /// Get wheel friction slip.
  122. float GetWheelFrictionSlip(int wheel) const;
  123. /// Get wheel roll influence.
  124. float GetWheelRollInfluence(int wheel) const;
  125. /// Get engine force for the wheel.
  126. float GetEngineForce(int wheel) const;
  127. /// Get hand brake value.
  128. float GetBrake(int wheel) const;
  129. /// Get wheel radius.
  130. float GetWheelRadius(int wheel) const;
  131. /// Get wheel rest length.
  132. void SetWheelRestLength(int wheel, float length);
  133. /// Get wheel rest length.
  134. float GetWheelRestLength(int wheel) const;
  135. /// Get maximum suspension travel value.
  136. float GetMaxSuspensionTravel(int wheel);
  137. /// Get wheel axle vector.
  138. Vector3 GetWheelAxle(int wheel) const;
  139. /// Get wheel slide speed.
  140. float GetWheelSideSlipSpeed(int wheel) const;
  141. /// Get side speed which is considered sliding.
  142. /// @property
  143. float GetMaxSideSlipSpeed() const;
  144. /// Sliding factor 0 <= x <= 1.
  145. float GetWheelSkidInfo(int wheel) const;
  146. /// Get wheel direction vector.
  147. Vector3 GetWheelDirection(int wheel) const;
  148. /// Get cumulative skid info.
  149. float GetWheelSkidInfoCumulative(int wheel) const;
  150. /// True if front wheel, otherwise false.
  151. bool IsFrontWheel(int wheel) const;
  152. /// Get wheel contact position.
  153. Vector3 GetContactPosition(int wheel) const;
  154. /// Get contact normal.
  155. Vector3 GetContactNormal(int wheel) const;
  156. /// Get revolution per minute value for when wheel doesn't touch ground.
  157. /// @property
  158. float GetInAirRPM() const;
  159. /// Get the coordinate system.
  160. /// @property
  161. IntVector3 GetCoordinateSystem() const { return coordinateSystem_; }
  162. /// Get wheel data attribute for serialization.
  163. VariantVector GetWheelDataAttr() const;
  164. /// Set wheel data attribute during loading.
  165. void SetWheelDataAttr(const VariantVector& value);
  166. /// (0, 1, 2) coordinate system (default).
  167. static const IntVector3 RIGHT_UP_FORWARD;
  168. /// (0, 2, 1) coordinate system.
  169. static const IntVector3 RIGHT_FORWARD_UP;
  170. /// (1, 2, 0) coordinate system.
  171. static const IntVector3 UP_FORWARD_RIGHT;
  172. /// (1, 0, 2) coordinate system.
  173. static const IntVector3 UP_RIGHT_FORWARD;
  174. /// (2, 0, 1) coordinate system.
  175. static const IntVector3 FORWARD_RIGHT_UP;
  176. /// (2, 1, 0) coordinate system.
  177. static const IntVector3 FORWARD_UP_RIGHT;
  178. private:
  179. /// If the RigidBody should be activated.
  180. bool activate_;
  181. /// Hull RigidBody.
  182. WeakPtr<RigidBody> hullBody_;
  183. /// Opaque Bullet data hidden from public.
  184. RaycastVehicleData* vehicleData_;
  185. /// Coordinate system.
  186. IntVector3 coordinateSystem_;
  187. /// Nodes of all wheels.
  188. Vector<Node*> wheelNodes_;
  189. /// All wheels original rotations. These are applied in addition to wheel rotations by btRaycastVehicle.
  190. Vector<Quaternion> origRotation_;
  191. /// Revolutions per minute value for in-air motor wheels. FIXME: set this one per wheel.
  192. float inAirRPM_;
  193. /// Per-wheel extra settings.
  194. Vector<float> skidInfoCumulative_;
  195. /// Wheel side movement speed.
  196. Vector<float> wheelSideSlipSpeed_;
  197. /// Side slip speed threshold.
  198. float maxSideSlipSpeed_;
  199. /// Loaded data temporarily wait here for ApplyAttributes to come pick them up.
  200. VariantVector loadedWheelData_;
  201. };
  202. }