RaycastVehicle.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  27. {
  28. struct RaycastVehicleData;
  29. class ATOMIC_API RaycastVehicle : public LogicComponent
  30. {
  31. ATOMIC_OBJECT(RaycastVehicle, LogicComponent)
  32. public:
  33. /// Construct.
  34. RaycastVehicle(Atomic::Context* context);
  35. /// Destruct.
  36. ~RaycastVehicle();
  37. /// Register object factory and attributes.
  38. static void RegisterObject(Context* context);
  39. /// Handle enabled/disabled state change.
  40. virtual void OnSetEnabled();
  41. /// Perform post-load after deserialization. Acquire the components from the scene nodes.
  42. virtual void ApplyAttributes();
  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(void);
  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 damping relaxation.
  54. void SetWheelDampingRelaxation(int wheel, float damping);
  55. /// Set wheel damping compression.
  56. void SetWheelDampingCompression(int wheel, float compression);
  57. /// Set wheel friction slip.
  58. void SetWheelFrictionSlip(int wheel, float slip);
  59. /// Set wheel roll influence.
  60. void SetWheelRollInfluence(int wheel, float rollInfluence);
  61. /// Set engine force for the wheel.
  62. void SetEngineForce(int wheel, float force);
  63. /// Set hand brake (wheel rotation blocking force.)
  64. void SetBrake(int wheel, float force);
  65. /// Set wheel radius.
  66. void SetWheelRadius(int wheel, float wheelRadius);
  67. /// Sets node initial positions.
  68. void ResetWheels();
  69. /// Set sliding factor 0 <= x <= 1. The less the value, more sliding.
  70. void SetWheelSkidInfo(int wheel, float factor);
  71. /// True if wheel touches ground (raycast hits something.)
  72. bool WheelIsGrounded(int wheel) const;
  73. /// Set maximum suspension travel value.
  74. void SetMaxSuspensionTravel(int wheel, float maxSuspensionTravel);
  75. /// Set wheel direction vector.
  76. void SetWheelDirection(int wheel, Vector3 direction);
  77. /// Set wheel axle vector.
  78. void SetWheelAxle(int wheel, Vector3 axle);
  79. /// Set side speed which is considered sliding.
  80. void SetMaxSideSlipSpeed(float speed);
  81. /// Set cumulative skid info.
  82. void SetWheelSkidInfoCumulative(int wheel, float skid);
  83. /// 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).
  84. void SetInAirRPM(float rpm);
  85. /// Init the vehicle component after creation
  86. void Init();
  87. /// Perform fixed step pre-update.
  88. void FixedUpdate(float timeStep);
  89. /// Perform fixed step post-update.
  90. void FixedPostUpdate(float timeStep);
  91. /// Perform variable step post-update.
  92. void PostUpdate(float timeStep);
  93. /// Get wheel position relative to RigidBody.
  94. Vector3 GetWheelPosition(int wheel);
  95. /// Get wheel rotation relative to RigidBody.
  96. Quaternion GetWheelRotation(int wheel);
  97. /// Get wheel connection point relative to RigidBody.
  98. Vector3 GetWheelConnectionPoint(int wheel) const;
  99. /// Get number of attached wheels.
  100. int GetNumWheels() const;
  101. /// Get node of the wheel.
  102. Node* GetWheelNode(int wheel) const;
  103. /// Get steering value of particular wheel.
  104. float GetSteeringValue(int wheel) const;
  105. /// Get suspension stiffness for particular wheel.
  106. float GetWheelSuspensionStiffness(int wheel) const;
  107. /// Get wheel damping relaxation.
  108. float GetWheelDampingRelaxation(int wheel) const;
  109. /// Get wheel damping compression.
  110. float GetWheelDampingCompression(int wheel) const;
  111. /// Get wheel friction slip.
  112. float GetWheelFrictionSlip(int wheel) const;
  113. /// Get wheel roll influence.
  114. float GetWheelRollInfluence(int wheel) const;
  115. /// Get engine force for the wheel.
  116. float GetEngineForce(int wheel) const;
  117. /// Get hand brake value.
  118. float GetBrake(int wheel) const;
  119. /// Get wheel radius.
  120. float GetWheelRadius(int wheel) const;
  121. /// Get wheel rest length.
  122. void SetWheelRestLength(int wheel, float length);
  123. /// Get wheel rest length.
  124. float GetWheelRestLength(int wheel) const;
  125. /// Get maximum suspension travel value.
  126. float GetMaxSuspensionTravel(int wheel);
  127. /// Get wheel axle vector.
  128. Vector3 GetWheelAxle(int wheel) const;
  129. /// Get wheel slide speed.
  130. float GetWheelSideSlipSpeed(int wheel) const;
  131. /// Get side speed which is considered sliding.
  132. float GetMaxSideSlipSpeed() const;
  133. /// Sliding factor 0 <= x <= 1.
  134. float GetWheelSkidInfo(int wheel) const;
  135. /// Get wheel direction vector.
  136. Vector3 GetWheelDirection(int wheel) const;
  137. /// Get cumulative skid info.
  138. float GetWheelSkidInfoCumulative(int wheel) const;
  139. /// True if front wheel, otherwise false.
  140. bool IsFrontWheel(int wheel) const;
  141. /// Get wheel contact position.
  142. Vector3 GetContactPosition(int wheel) const;
  143. /// Get contact normal.
  144. Vector3 GetContactNormal(int wheel) const;
  145. /// Get revolution per minute value for when wheel doesn't touch ground.
  146. float GetInAirRPM() const;
  147. /// Get wheel data attribute for serialization.
  148. VariantVector GetWheelDataAttr() const;
  149. /// Set wheel data attribute during loading.
  150. void SetWheelDataAttr(const VariantVector& value);
  151. private:
  152. /// If the RigidBody should be activated.
  153. bool activate_;
  154. /// Hull RigidBody
  155. WeakPtr<RigidBody> hullBody_;
  156. /// Opaque Bullet data hidden from public
  157. RaycastVehicleData* vehicleData_;
  158. /// Nodes of all wheels
  159. Vector<Node*> wheelNodes_;
  160. /// All wheels original rotations. These are applied in addition to wheel rotations by btRaycastVehicle
  161. Vector<Quaternion> origRotation_;
  162. /// Revolutions per minute value for in-air motor wheels. FIXME: set this one per wheel
  163. float inAirRPM_;
  164. /// Per-wheel extra settings.
  165. Vector<float> skidInfoCumulative_;
  166. /// Wheel side movement speed.
  167. Vector<float> wheelSideSlipSpeed_;
  168. /// Side slip speed threshold.
  169. float maxSideSlipSpeed_;
  170. /// Loaded data temporarily wait here for ApplyAttributes to come pick them up.
  171. VariantVector loadedWheelData_;
  172. };
  173. }