RaycastVehicle.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void SetMaxSideSlipSpeed(float speed);
  83. /// Set cumulative skid info.
  84. void SetWheelSkidInfoCumulative(int wheel, float skid);
  85. /// 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).
  86. void SetInAirRPM(float rpm);
  87. /// Set the coordinate system. The default is (0, 1, 2).
  88. void SetCoordinateSystem(const IntVector3& coordinateSystem = RIGHT_FORWARD_UP);
  89. /// Init the vehicle component after creation.
  90. void Init();
  91. /// Perform fixed step pre-update.
  92. void FixedUpdate(float timeStep) override;
  93. /// Perform fixed step post-update.
  94. void FixedPostUpdate(float timeStep) override;
  95. /// Perform variable step post-update.
  96. void PostUpdate(float timeStep) override;
  97. /// Get wheel position relative to RigidBody.
  98. Vector3 GetWheelPosition(int wheel);
  99. /// Get wheel rotation relative to RigidBody.
  100. Quaternion GetWheelRotation(int wheel);
  101. /// Get wheel connection point relative to RigidBody.
  102. Vector3 GetWheelConnectionPoint(int wheel) const;
  103. /// Get number of attached wheels.
  104. int GetNumWheels() const;
  105. /// Get node of the wheel.
  106. Node* GetWheelNode(int wheel) const;
  107. /// Get steering value of particular wheel.
  108. float GetSteeringValue(int wheel) const;
  109. /// Get suspension stiffness for particular wheel.
  110. float GetWheelSuspensionStiffness(int wheel) const;
  111. /// Get wheel max suspension force.
  112. float GetWheelMaxSuspensionForce(int wheel) const;
  113. /// Get wheel damping relaxation.
  114. float GetWheelDampingRelaxation(int wheel) const;
  115. /// Get wheel damping compression.
  116. float GetWheelDampingCompression(int wheel) const;
  117. /// Get wheel friction slip.
  118. float GetWheelFrictionSlip(int wheel) const;
  119. /// Get wheel roll influence.
  120. float GetWheelRollInfluence(int wheel) const;
  121. /// Get engine force for the wheel.
  122. float GetEngineForce(int wheel) const;
  123. /// Get hand brake value.
  124. float GetBrake(int wheel) const;
  125. /// Get wheel radius.
  126. float GetWheelRadius(int wheel) const;
  127. /// Get wheel rest length.
  128. void SetWheelRestLength(int wheel, float length);
  129. /// Get wheel rest length.
  130. float GetWheelRestLength(int wheel) const;
  131. /// Get maximum suspension travel value.
  132. float GetMaxSuspensionTravel(int wheel);
  133. /// Get wheel axle vector.
  134. Vector3 GetWheelAxle(int wheel) const;
  135. /// Get wheel slide speed.
  136. float GetWheelSideSlipSpeed(int wheel) const;
  137. /// Get side speed which is considered sliding.
  138. float GetMaxSideSlipSpeed() const;
  139. /// Sliding factor 0 <= x <= 1.
  140. float GetWheelSkidInfo(int wheel) const;
  141. /// Get wheel direction vector.
  142. Vector3 GetWheelDirection(int wheel) const;
  143. /// Get cumulative skid info.
  144. float GetWheelSkidInfoCumulative(int wheel) const;
  145. /// True if front wheel, otherwise false.
  146. bool IsFrontWheel(int wheel) const;
  147. /// Get wheel contact position.
  148. Vector3 GetContactPosition(int wheel) const;
  149. /// Get contact normal.
  150. Vector3 GetContactNormal(int wheel) const;
  151. /// Get revolution per minute value for when wheel doesn't touch ground.
  152. float GetInAirRPM() const;
  153. /// Get the coordinate system.
  154. IntVector3 GetCoordinateSystem() const { return coordinateSystem_; }
  155. /// Get wheel data attribute for serialization.
  156. VariantVector GetWheelDataAttr() const;
  157. /// Set wheel data attribute during loading.
  158. void SetWheelDataAttr(const VariantVector& value);
  159. /// (0, 1, 2) coordinate system (default).
  160. static const IntVector3 RIGHT_UP_FORWARD;
  161. /// (0, 2, 1) coordinate system.
  162. static const IntVector3 RIGHT_FORWARD_UP;
  163. /// (1, 2, 0) coordinate system.
  164. static const IntVector3 UP_FORWARD_RIGHT;
  165. /// (1, 0, 2) coordinate system.
  166. static const IntVector3 UP_RIGHT_FORWARD;
  167. /// (2, 0, 1) coordinate system.
  168. static const IntVector3 FORWARD_RIGHT_UP;
  169. /// (2, 1, 0) coordinate system.
  170. static const IntVector3 FORWARD_UP_RIGHT;
  171. private:
  172. /// If the RigidBody should be activated.
  173. bool activate_;
  174. /// Hull RigidBody.
  175. WeakPtr<RigidBody> hullBody_;
  176. /// Opaque Bullet data hidden from public.
  177. RaycastVehicleData* vehicleData_;
  178. /// Coordinate system.
  179. IntVector3 coordinateSystem_;
  180. /// Nodes of all wheels.
  181. Vector<Node*> wheelNodes_;
  182. /// All wheels original rotations. These are applied in addition to wheel rotations by btRaycastVehicle.
  183. Vector<Quaternion> origRotation_;
  184. /// Revolutions per minute value for in-air motor wheels. FIXME: set this one per wheel.
  185. float inAirRPM_;
  186. /// Per-wheel extra settings.
  187. Vector<float> skidInfoCumulative_;
  188. /// Wheel side movement speed.
  189. Vector<float> wheelSideSlipSpeed_;
  190. /// Side slip speed threshold.
  191. float maxSideSlipSpeed_;
  192. /// Loaded data temporarily wait here for ApplyAttributes to come pick them up.
  193. VariantVector loadedWheelData_;
  194. };
  195. }