Vehicle.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <Atomic/Input/Controls.h>
  25. #include <Atomic/Scene/LogicComponent.h>
  26. namespace Atomic
  27. {
  28. class Constraint;
  29. class Node;
  30. class RigidBody;
  31. }
  32. using namespace Atomic;
  33. const int CTRL_FORWARD = 1;
  34. const int CTRL_BACK = 2;
  35. const int CTRL_LEFT = 4;
  36. const int CTRL_RIGHT = 8;
  37. const float YAW_SENSITIVITY = 0.1f;
  38. const float ENGINE_POWER = 10.0f;
  39. const float DOWN_FORCE = 10.0f;
  40. const float MAX_WHEEL_ANGLE = 22.5f;
  41. /// Vehicle component, responsible for physical movement according to controls.
  42. class Vehicle : public LogicComponent
  43. {
  44. ATOMIC_OBJECT(Vehicle, LogicComponent)
  45. public:
  46. /// Construct.
  47. Vehicle(Context* context);
  48. /// Register object factory and attributes.
  49. static void RegisterObject(Context* context);
  50. /// Perform post-load after deserialization. Acquire the components from the scene nodes.
  51. virtual void ApplyAttributes();
  52. /// Handle physics world update. Called by LogicComponent base class.
  53. virtual void FixedUpdate(float timeStep);
  54. /// Initialize the vehicle. Create rendering and physics components. Called by the application.
  55. void Init();
  56. /// Movement controls.
  57. Controls controls_;
  58. private:
  59. /// Initialize a wheel and remember its scene node and ID.
  60. void InitWheel(const String& name, const Vector3& offset, WeakPtr<Node>& wheelNode, unsigned& wheelNodeID);
  61. /// Acquire wheel components from wheel scene nodes.
  62. void GetWheelComponents();
  63. // Wheel scene nodes.
  64. WeakPtr<Node> frontLeft_;
  65. WeakPtr<Node> frontRight_;
  66. WeakPtr<Node> rearLeft_;
  67. WeakPtr<Node> rearRight_;
  68. // Steering axle constraints.
  69. WeakPtr<Constraint> frontLeftAxis_;
  70. WeakPtr<Constraint> frontRightAxis_;
  71. // Hull and wheel rigid bodies.
  72. WeakPtr<RigidBody> hullBody_;
  73. WeakPtr<RigidBody> frontLeftBody_;
  74. WeakPtr<RigidBody> frontRightBody_;
  75. WeakPtr<RigidBody> rearLeftBody_;
  76. WeakPtr<RigidBody> rearRightBody_;
  77. // IDs of the wheel scene nodes for serialization.
  78. unsigned frontLeftID_;
  79. unsigned frontRightID_;
  80. unsigned rearLeftID_;
  81. unsigned rearRightID_;
  82. /// Current left/right steering amount (-1 to 1.)
  83. float steering_;
  84. };