Vehicle.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/Scene/LogicComponent.h>
  25. namespace Urho3D
  26. {
  27. class Constraint;
  28. class Node;
  29. class RigidBody;
  30. }
  31. using namespace Urho3D;
  32. const int CTRL_FORWARD = 1;
  33. const int CTRL_BACK = 2;
  34. const int CTRL_LEFT = 4;
  35. const int CTRL_RIGHT = 8;
  36. const float YAW_SENSITIVITY = 0.1f;
  37. const float ENGINE_POWER = 10.0f;
  38. const float DOWN_FORCE = 10.0f;
  39. const float MAX_WHEEL_ANGLE = 22.5f;
  40. /// Vehicle component, responsible for physical movement according to controls.
  41. class Vehicle : public LogicComponent
  42. {
  43. URHO3D_OBJECT(Vehicle, LogicComponent)
  44. public:
  45. /// Construct.
  46. Vehicle(Context* context);
  47. /// Register object factory and attributes.
  48. static void RegisterObject(Context* context);
  49. /// Perform post-load after deserialization. Acquire the components from the scene nodes.
  50. virtual void ApplyAttributes();
  51. /// Handle physics world update. Called by LogicComponent base class.
  52. virtual void FixedUpdate(float timeStep);
  53. /// Initialize the vehicle. Create rendering and physics components. Called by the application.
  54. void Init();
  55. /// Movement controls.
  56. Controls controls_;
  57. private:
  58. /// Initialize a wheel and remember its scene node and ID.
  59. void InitWheel(const String& name, const Vector3& offset, WeakPtr<Node>& wheelNode, unsigned& wheelNodeID);
  60. /// Acquire wheel components from wheel scene nodes.
  61. void GetWheelComponents();
  62. // Wheel scene nodes.
  63. WeakPtr<Node> frontLeft_;
  64. WeakPtr<Node> frontRight_;
  65. WeakPtr<Node> rearLeft_;
  66. WeakPtr<Node> rearRight_;
  67. // Steering axle constraints.
  68. WeakPtr<Constraint> frontLeftAxis_;
  69. WeakPtr<Constraint> frontRightAxis_;
  70. // Hull and wheel rigid bodies.
  71. WeakPtr<RigidBody> hullBody_;
  72. WeakPtr<RigidBody> frontLeftBody_;
  73. WeakPtr<RigidBody> frontRightBody_;
  74. WeakPtr<RigidBody> rearLeftBody_;
  75. WeakPtr<RigidBody> rearRightBody_;
  76. // IDs of the wheel scene nodes for serialization.
  77. unsigned frontLeftID_;
  78. unsigned frontRightID_;
  79. unsigned rearLeftID_;
  80. unsigned rearRightID_;
  81. /// Current left/right steering amount (-1 to 1.)
  82. float steering_;
  83. };