Vehicle.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "Vehicle.h"
  4. #include <Urho3D/Core/Context.h>
  5. #include <Urho3D/Graphics/DebugRenderer.h>
  6. #include <Urho3D/Graphics/DecalSet.h>
  7. #include <Urho3D/Graphics/Material.h>
  8. #include <Urho3D/Graphics/Model.h>
  9. #include <Urho3D/Graphics/ParticleEffect.h>
  10. #include <Urho3D/Graphics/ParticleEmitter.h>
  11. #include <Urho3D/Graphics/StaticModel.h>
  12. #include <Urho3D/IO/Log.h>
  13. #include <Urho3D/Physics/CollisionShape.h>
  14. #include <Urho3D/Physics/Constraint.h>
  15. #include <Urho3D/Physics/PhysicsEvents.h>
  16. #include <Urho3D/Physics/PhysicsWorld.h>
  17. #include <Urho3D/Physics/RaycastVehicle.h>
  18. #include <Urho3D/Resource/ResourceCache.h>
  19. #include <Urho3D/Scene/Scene.h>
  20. using namespace Urho3D;
  21. const float CHASSIS_WIDTH = 2.6f;
  22. void Vehicle::RegisterObject(Context* context)
  23. {
  24. context->RegisterFactory<Vehicle>();
  25. URHO3D_ATTRIBUTE("Steering", steering_, 0.0f, AM_DEFAULT);
  26. URHO3D_ATTRIBUTE("Controls Yaw", controls_.yaw_, 0.0f, AM_DEFAULT);
  27. URHO3D_ATTRIBUTE("Controls Pitch", controls_.pitch_, 0.0f, AM_DEFAULT);
  28. }
  29. Vehicle::Vehicle(Urho3D::Context* context)
  30. : LogicComponent(context),
  31. steering_(0.0f)
  32. {
  33. SetUpdateEventMask(LogicComponentEvents::FixedUpdate | LogicComponentEvents::PostUpdate);
  34. engineForce_ = 0.0f;
  35. brakingForce_ = 50.0f;
  36. vehicleSteering_ = 0.0f;
  37. maxEngineForce_ = 2500.0f;
  38. wheelRadius_ = 0.5f;
  39. suspensionRestLength_ = 0.6f;
  40. wheelWidth_ = 0.4f;
  41. suspensionStiffness_ = 14.0f;
  42. suspensionDamping_ = 2.0f;
  43. suspensionCompression_ = 4.0f;
  44. wheelFriction_ = 1000.0f;
  45. rollInfluence_ = 0.12f;
  46. emittersCreated = false;
  47. }
  48. Vehicle::~Vehicle() = default;
  49. void Vehicle::Init()
  50. {
  51. auto* vehicle = node_->CreateComponent<RaycastVehicle>();
  52. vehicle->Init();
  53. auto* hullBody = node_->GetComponent<RigidBody>();
  54. hullBody->SetMass(800.0f);
  55. hullBody->SetLinearDamping(0.2f); // Some air resistance
  56. hullBody->SetAngularDamping(0.5f);
  57. hullBody->SetCollisionLayer(1);
  58. // This function is called only from the main program when initially creating the vehicle, not on scene load
  59. auto* cache = GetSubsystem<ResourceCache>();
  60. auto* hullObject = node_->CreateComponent<StaticModel>();
  61. // Setting-up collision shape
  62. auto* hullColShape = node_->CreateComponent<CollisionShape>();
  63. Vector3 v3BoxExtents = Vector3::ONE;
  64. hullColShape->SetBox(v3BoxExtents);
  65. node_->SetScale(Vector3(2.3f, 1.0f, 4.0f));
  66. hullObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  67. hullObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  68. hullObject->SetCastShadows(true);
  69. float connectionHeight = -0.4f;
  70. bool isFrontWheel = true;
  71. Vector3 wheelDirection(0, -1, 0);
  72. Vector3 wheelAxle(-1, 0, 0);
  73. // We use not scaled coordinates here as everything will be scaled.
  74. // Wheels are on bottom at edges of the chassis
  75. // Note we don't set wheel nodes as children of hull (while we could) to avoid scaling to affect them.
  76. float wheelX = CHASSIS_WIDTH / 2.0f - wheelWidth_;
  77. // Front left
  78. connectionPoints_[0] = Vector3(-wheelX, connectionHeight, 2.5f - GetWheelRadius() * 2.0f);
  79. // Front right
  80. connectionPoints_[1] = Vector3(wheelX, connectionHeight, 2.5f - GetWheelRadius() * 2.0f);
  81. // Back left
  82. connectionPoints_[2] = Vector3(-wheelX, connectionHeight, -2.5f + GetWheelRadius() * 2.0f);
  83. // Back right
  84. connectionPoints_[3] = Vector3(wheelX, connectionHeight, -2.5f + GetWheelRadius() * 2.0f);
  85. const Color LtBrown(0.972f, 0.780f, 0.412f);
  86. for (int id = 0; id < sizeof(connectionPoints_) / sizeof(connectionPoints_[0]); id++)
  87. {
  88. Node* wheelNode = GetScene()->CreateChild();
  89. Vector3 connectionPoint = connectionPoints_[id];
  90. // Front wheels are at front (z > 0)
  91. // back wheels are at z < 0
  92. // Setting rotation according to wheel position
  93. bool isFrontWheel = connectionPoints_[id].z_ > 0.0f;
  94. wheelNode->SetRotation(connectionPoint.x_ >= 0.0 ? Quaternion(0.0f, 0.0f, -90.0f) : Quaternion(0.0f, 0.0f, 90.0f));
  95. wheelNode->SetWorldPosition(node_->GetWorldPosition() + node_->GetWorldRotation() * connectionPoints_[id]);
  96. vehicle->AddWheel(wheelNode, wheelDirection, wheelAxle, suspensionRestLength_, wheelRadius_, isFrontWheel);
  97. vehicle->SetWheelSuspensionStiffness(id, suspensionStiffness_);
  98. vehicle->SetWheelDampingRelaxation(id, suspensionDamping_);
  99. vehicle->SetWheelDampingCompression(id, suspensionCompression_);
  100. vehicle->SetWheelFrictionSlip(id, wheelFriction_);
  101. vehicle->SetWheelRollInfluence(id, rollInfluence_);
  102. wheelNode->SetScale(Vector3(1.0f, 0.65f, 1.0f));
  103. auto* pWheel = wheelNode->CreateComponent<StaticModel>();
  104. pWheel->SetModel(cache->GetResource<Model>("Models/Cylinder.mdl"));
  105. pWheel->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  106. pWheel->SetCastShadows(true);
  107. CreateEmitter(connectionPoints_[id]);
  108. }
  109. emittersCreated = true;
  110. vehicle->ResetWheels();
  111. }
  112. void Vehicle::CreateEmitter(Vector3 place)
  113. {
  114. auto* cache = GetSubsystem<ResourceCache>();
  115. Node* emitter = GetScene()->CreateChild();
  116. emitter->SetWorldPosition(node_->GetWorldPosition() + node_->GetWorldRotation() * place + Vector3(0, -wheelRadius_, 0));
  117. auto* particleEmitter = emitter->CreateComponent<ParticleEmitter>();
  118. particleEmitter->SetEffect(cache->GetResource<ParticleEffect>("Particle/Dust.xml"));
  119. particleEmitter->SetEmitting(false);
  120. particleEmitterNodeList_.Push(emitter);
  121. emitter->SetTemporary(true);
  122. }
  123. /// Applying attributes
  124. void Vehicle::ApplyAttributes()
  125. {
  126. auto* vehicle = node_->GetOrCreateComponent<RaycastVehicle>();
  127. if (emittersCreated)
  128. return;
  129. for (const auto& connectionPoint : connectionPoints_)
  130. {
  131. CreateEmitter(connectionPoint);
  132. }
  133. emittersCreated = true;
  134. }
  135. void Vehicle::FixedUpdate(float timeStep)
  136. {
  137. float newSteering = 0.0f;
  138. float accelerator = 0.0f;
  139. bool brake = false;
  140. auto* vehicle = node_->GetComponent<RaycastVehicle>();
  141. // Read controls
  142. if (controls_.buttons_ & CTRL_LEFT)
  143. {
  144. newSteering = -1.0f;
  145. }
  146. if (controls_.buttons_ & CTRL_RIGHT)
  147. {
  148. newSteering = 1.0f;
  149. }
  150. if (controls_.buttons_ & CTRL_FORWARD)
  151. {
  152. accelerator = 1.0f;
  153. }
  154. if (controls_.buttons_ & CTRL_BACK)
  155. {
  156. accelerator = -0.5f;
  157. }
  158. if (controls_.buttons_ & CTRL_BRAKE)
  159. {
  160. brake = true;
  161. }
  162. // When steering, wake up the wheel rigidbodies so that their orientation is updated
  163. if (newSteering != 0.0f)
  164. {
  165. SetSteering(GetSteering() * 0.95f + newSteering * 0.05f);
  166. }
  167. else
  168. {
  169. SetSteering(GetSteering() * 0.8f + newSteering * 0.2f);
  170. }
  171. // Set front wheel angles
  172. vehicleSteering_ = steering_;
  173. int wheelIndex = 0;
  174. vehicle->SetSteeringValue(wheelIndex, vehicleSteering_);
  175. wheelIndex = 1;
  176. vehicle->SetSteeringValue(wheelIndex, vehicleSteering_);
  177. // apply forces
  178. engineForce_ = maxEngineForce_ * accelerator;
  179. // 2x wheel drive
  180. vehicle->SetEngineForce(2, engineForce_);
  181. vehicle->SetEngineForce(3, engineForce_);
  182. for (int i = 0; i < vehicle->GetNumWheels(); i++)
  183. {
  184. if (brake)
  185. {
  186. vehicle->SetBrake(i, brakingForce_);
  187. }
  188. else
  189. {
  190. vehicle->SetBrake(i, 0.0f);
  191. }
  192. }
  193. }
  194. void Vehicle::PostUpdate(float timeStep)
  195. {
  196. auto* vehicle = node_->GetComponent<RaycastVehicle>();
  197. auto* vehicleBody = node_->GetComponent<RigidBody>();
  198. Vector3 velocity = vehicleBody->GetLinearVelocity();
  199. Vector3 accel = (velocity - prevVelocity_) / timeStep;
  200. float planeAccel = Vector3(accel.x_, 0.0f, accel.z_).Length();
  201. for (int i = 0; i < vehicle->GetNumWheels(); i++)
  202. {
  203. Node* emitter = particleEmitterNodeList_[i];
  204. auto* particleEmitter = emitter->GetComponent<ParticleEmitter>();
  205. if (vehicle->WheelIsGrounded(i) && (vehicle->GetWheelSkidInfoCumulative(i) < 0.9f || vehicle->GetBrake(i) > 2.0f ||
  206. planeAccel > 15.0f))
  207. {
  208. particleEmitterNodeList_[i]->SetWorldPosition(vehicle->GetContactPosition(i));
  209. if (!particleEmitter->IsEmitting())
  210. {
  211. particleEmitter->SetEmitting(true);
  212. }
  213. URHO3D_LOGDEBUG("GetWheelSkidInfoCumulative() = " +
  214. String(vehicle->GetWheelSkidInfoCumulative(i)) + " " +
  215. String(vehicle->GetMaxSideSlipSpeed()));
  216. /* TODO: Add skid marks here */
  217. }
  218. else if (particleEmitter->IsEmitting())
  219. {
  220. particleEmitter->SetEmitting(false);
  221. }
  222. }
  223. prevVelocity_ = velocity;
  224. }