Vehicle.h 3.1 KB

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