Vehicle.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <Urho3D/Input/Controls.h>
  24. #include <Urho3D/Physics/PhysicsUtils.h>
  25. #include <Urho3D/Scene/LogicComponent.h>
  26. namespace Urho3D
  27. {
  28. class Constraint;
  29. class Node;
  30. class RigidBody;
  31. }
  32. using namespace Urho3D;
  33. const int CTRL_FORWARD = (1 << 0);
  34. const int CTRL_BACK = (1 << 1);
  35. const int CTRL_LEFT = (1 << 2);
  36. const int CTRL_RIGHT = (1 << 3);
  37. const int CTRL_BRAKE = (1 << 4);
  38. const float YAW_SENSITIVITY = 0.1f;
  39. const float ENGINE_POWER = 10.0f;
  40. const float MAX_WHEEL_ANGLE = 22.5f;
  41. // Vehicle component, responsible for physical movement according to controls.
  42. // Encapsulates RaycastVehicle
  43. class Vehicle : public LogicComponent
  44. {
  45. URHO3D_OBJECT(Vehicle, LogicComponent)
  46. public :
  47. /// Construct.
  48. Vehicle(Context* context);
  49. /// Destruct.
  50. virtual ~Vehicle() override;
  51. /// Register object factory and attributes.
  52. static void RegisterObject(Context* context);
  53. /// Perform post-load after deserialization. Acquire the components from the scene nodes.
  54. virtual void ApplyAttributes() override;
  55. /// Initialize the vehicle. Create rendering and physics components. Called by the application.
  56. void Init();
  57. /// Handle physics world update. Called by LogicComponent base class.
  58. virtual void FixedUpdate(float timeStep) override;
  59. /// Updating wheel effects here.
  60. virtual void PostUpdate(float timeStep) override;
  61. /// Movement controls.
  62. Controls controls_;
  63. /// Get steering value.
  64. float GetSteering() { return steering_; }
  65. /// Set steering value.
  66. void SetSteering(float steering) { steering_ = steering; }
  67. /// Get wheel radius.
  68. float GetWheelRadius() { return wheelRadius_; }
  69. /// Get wheel width.
  70. float GetWheelWidth() { return wheelWidth_; }
  71. private:
  72. /// Creates particle emitter.
  73. void CreateEmitter(Vector3 place);
  74. /// Current left/right steering amount (-1 to 1.)
  75. float steering_;
  76. /// Tmp storage for steering
  77. float vehicleSteering_;
  78. /// Linear momentum supplied by engine to RigidBody
  79. float engineForce_;
  80. /// Rotational momentum preventing (dampening) wheels rotation
  81. float brakingForce_;
  82. /// Maximum linear momentum supplied by engine to RigidBody
  83. float maxEngineForce_;
  84. /// Stored wheel radius
  85. float wheelRadius_;
  86. /// Suspension rest length (in meters)
  87. float suspensionRestLength_;
  88. /// Width of wheel (used only in calculation of wheel placement)
  89. float wheelWidth_;
  90. /// Suspension stiffness
  91. float suspensionStiffness_;
  92. /// Suspension damping
  93. float suspensionDamping_;
  94. /// Suspension compression
  95. float suspensionCompression_;
  96. /// Wheel friction
  97. float wheelFriction_;
  98. /// Wheel roll influence (how much car will turn sidewise)
  99. float rollInfluence_;
  100. /// Emitter data for saving.
  101. Vector<Node*> particleEmitterNodeList_;
  102. /// Value to calculate acceleration.
  103. Vector3 prevVelocity_;
  104. /// Storing points for emitters
  105. Vector3 connectionPoints_[4];
  106. /// Do not recreate emitters if they are already created.
  107. bool emittersCreated;
  108. };