Vehicle.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  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. using System;
  24. using AtomicEngine;
  25. namespace FeatureExamples
  26. {
  27. /// <summary>
  28. /// Vehicle component, responsible for physical movement according to controls.
  29. /// </summary>
  30. public class Vehicle : CSComponent
  31. {
  32. public const int CtrlForward = 1;
  33. public const int CtrlBack = 2;
  34. public const int CtrlLeft = 4;
  35. public const int CtrlRight = 8;
  36. public const float YawSensitivity = 0.1f;
  37. public const float EnginePower = 10.0f;
  38. public const float DownForce = 10.0f;
  39. public const float MaxWheelAngle = 22.5f;
  40. // Movement controls.
  41. public Controls Controls { get; set; } = new Controls();
  42. // Wheel scene nodes.
  43. Node frontLeft;
  44. Node frontRight;
  45. Node rearLeft;
  46. Node rearRight;
  47. // Steering axle constraints.
  48. Constraint frontLeftAxis;
  49. Constraint frontRightAxis;
  50. // Hull and wheel rigid bodies.
  51. RigidBody hullBody;
  52. RigidBody frontLeftBody;
  53. RigidBody frontRightBody;
  54. RigidBody rearLeftBody;
  55. RigidBody rearRightBody;
  56. // IDs of the wheel scene nodes for serialization.
  57. uint frontLeftId;
  58. uint frontRightId;
  59. uint rearLeftId;
  60. uint rearRightId;
  61. /// Current left/right steering amount (-1 to 1.)
  62. float steering;
  63. public void PhysicsPreStep(float timeStep)
  64. {
  65. float newSteering = 0.0f;
  66. float accelerator = 0.0f;
  67. // Read controls
  68. if (Controls.IsDown(CtrlLeft))
  69. newSteering = -1.0f;
  70. if (Controls.IsDown(CtrlRight))
  71. newSteering = 1.0f;
  72. if (Controls.IsDown(CtrlForward))
  73. accelerator = 1.0f;
  74. if (Controls.IsDown(CtrlBack))
  75. accelerator = -0.5f;
  76. // When steering, wake up the wheel rigidbodies so that their orientation is updated
  77. if (newSteering != 0.0f)
  78. {
  79. frontLeftBody.Activate();
  80. frontRightBody.Activate();
  81. steering = steering * 0.95f + newSteering * 0.05f;
  82. }
  83. else
  84. steering = steering * 0.8f + newSteering * 0.2f;
  85. // Set front wheel angles
  86. Quaternion steeringRot = new Quaternion(0, steering * MaxWheelAngle, 0);
  87. frontLeftAxis.SetOtherAxis(steeringRot * new Vector3(-1f, 0f, 0f));
  88. frontRightAxis.SetOtherAxis(steeringRot * Vector3.UnitX);
  89. Quaternion hullRot = hullBody.Rotation;
  90. if (accelerator != 0.0f)
  91. {
  92. // Torques are applied in world space, so need to take the vehicle & wheel rotation into account
  93. Vector3 torqueVec = new Vector3(EnginePower * accelerator, 0.0f, 0.0f);
  94. frontLeftBody.ApplyTorque(hullRot * steeringRot * torqueVec);
  95. frontRightBody.ApplyTorque(hullRot * steeringRot * torqueVec);
  96. rearLeftBody.ApplyTorque(hullRot * torqueVec);
  97. rearRightBody.ApplyTorque(hullRot * torqueVec);
  98. }
  99. // Apply downforce proportional to velocity
  100. Vector3 localVelocity = Quaternion.Invert(hullRot) * hullBody.LinearVelocity;
  101. hullBody.ApplyForce(hullRot * new Vector3(0f, -1f, 0f) * Math.Abs(localVelocity.Z) * DownForce);
  102. }
  103. public void Init()
  104. {
  105. var cache = GetSubsystem<ResourceCache>();
  106. // This function is called only from the main program when initially creating the vehicle, not on scene load
  107. var node = Node;
  108. StaticModel hullObject = node.CreateComponent<StaticModel>();
  109. hullBody = node.CreateComponent<RigidBody>();
  110. CollisionShape hullShape = node.CreateComponent<CollisionShape>();
  111. node.Scale = new Vector3(1.5f, 1.0f, 3.0f);
  112. hullObject.Model = cache.Get<Model>("Models/Box.mdl");
  113. hullObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  114. hullObject.CastShadows = true;
  115. hullShape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
  116. hullBody.Mass = 4.0f;
  117. hullBody.LinearDamping = 0.2f; // Some air resistance
  118. hullBody.AngularDamping = 0.5f;
  119. hullBody.CollisionLayer = 1;
  120. InitWheel("FrontLeft", new Vector3(-0.6f, -0.4f, 0.3f), out frontLeft, out frontLeftId);
  121. InitWheel("FrontRight", new Vector3(0.6f, -0.4f, 0.3f), out frontRight, out frontRightId);
  122. InitWheel("RearLeft", new Vector3(-0.6f, -0.4f, -0.3f), out rearLeft, out rearLeftId);
  123. InitWheel("RearRight", new Vector3(0.6f, -0.4f, -0.3f), out rearRight, out rearRightId);
  124. GetWheelComponents();
  125. }
  126. void InitWheel(string name, Vector3 offset, out Node wheelNode, out uint wheelNodeId)
  127. {
  128. var cache = GetSubsystem<ResourceCache>();
  129. // Note: do not parent the wheel to the hull scene node. Instead create it on the root level and let the physics
  130. // constraint keep it together
  131. wheelNode = Scene.CreateChild(name);
  132. wheelNode.Position = Node.LocalToWorld(offset);
  133. wheelNode.Rotation = Node.Rotation * (offset.X >= 0.0 ? new Quaternion(0.0f, 0.0f, -90.0f) : new Quaternion(0.0f, 0.0f, 90.0f));
  134. wheelNode.Scale = new Vector3(0.8f, 0.5f, 0.8f);
  135. // Remember the ID for serialization
  136. wheelNodeId = wheelNode.ID;
  137. StaticModel wheelObject = wheelNode.CreateComponent<StaticModel>();
  138. RigidBody wheelBody = wheelNode.CreateComponent<RigidBody>();
  139. CollisionShape wheelShape = wheelNode.CreateComponent<CollisionShape>();
  140. Constraint wheelConstraint = wheelNode.CreateComponent<Constraint>();
  141. wheelObject.Model = (cache.Get<Model>("Models/Cylinder.mdl"));
  142. wheelObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  143. wheelObject.CastShadows = true;
  144. wheelShape.SetSphere(1.0f, Vector3.Zero, Quaternion.Identity);
  145. wheelBody.Friction = (1.0f);
  146. wheelBody.Mass = 1.0f;
  147. wheelBody.LinearDamping = 0.2f; // Some air resistance
  148. wheelBody.AngularDamping = 0.75f; // Could also use rolling friction
  149. wheelBody.CollisionLayer = 1;
  150. wheelConstraint.ConstraintType = ConstraintType.CONSTRAINT_HINGE;
  151. wheelConstraint.OtherBody = GetComponent<RigidBody>(); // Connect to the hull body
  152. wheelConstraint.SetWorldPosition(wheelNode.Position); // Set constraint's both ends at wheel's location
  153. wheelConstraint.SetAxis(Vector3.UnitY); // Wheel rotates around its local Y-axis
  154. wheelConstraint.SetOtherAxis(offset.X >= 0.0 ? Vector3.UnitX : new Vector3(-1f, 0f, 0f)); // Wheel's hull axis points either left or right
  155. wheelConstraint.LowLimit = new Vector2(-180.0f, 0.0f); // Let the wheel rotate freely around the axis
  156. wheelConstraint.HighLimit = new Vector2(180.0f, 0.0f);
  157. wheelConstraint.DisableCollision = true; // Let the wheel intersect the vehicle hull
  158. }
  159. void GetWheelComponents()
  160. {
  161. frontLeftAxis = frontLeft.GetComponent<Constraint>();
  162. frontRightAxis = frontRight.GetComponent<Constraint>();
  163. frontLeftBody = frontLeft.GetComponent<RigidBody>();
  164. frontRightBody = frontRight.GetComponent<RigidBody>();
  165. rearLeftBody = rearLeft.GetComponent<RigidBody>();
  166. rearRightBody = rearRight.GetComponent<RigidBody>();
  167. }
  168. }
  169. }