RaycastVehicle.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. #include "../Core/Context.h"
  23. #include <Bullet/BulletDynamics/Vehicle/btRaycastVehicle.h>
  24. #include <Bullet/BulletDynamics/Dynamics/btDynamicsWorld.h>
  25. #include "../Physics/PhysicsUtils.h"
  26. #include "../Physics/RigidBody.h"
  27. #include "../Physics/PhysicsWorld.h"
  28. #include "../Scene/Scene.h"
  29. #include "../IO/Log.h"
  30. #include "../Physics/RaycastVehicle.h"
  31. namespace Urho3D
  32. {
  33. struct RaycastVehicleData
  34. {
  35. RaycastVehicleData()
  36. {
  37. vehicleRayCaster_ = 0;
  38. vehicle_ = 0;
  39. }
  40. ~RaycastVehicleData()
  41. {
  42. if (vehicleRayCaster_)
  43. {
  44. delete vehicleRayCaster_;
  45. }
  46. vehicleRayCaster_ = 0;
  47. if (vehicle_)
  48. {
  49. delete vehicle_;
  50. }
  51. vehicle_ = 0;
  52. }
  53. btRaycastVehicle *Get()
  54. {
  55. return vehicle_;
  56. }
  57. void Init(Scene *scene, RigidBody *body)
  58. {
  59. int rightIndex = 0;
  60. int upIndex = 1;
  61. int forwardIndex = 2;
  62. PhysicsWorld *pPhysWorld = scene->GetComponent<PhysicsWorld>();
  63. btDynamicsWorld *pbtDynWorld = (btDynamicsWorld*) pPhysWorld->GetWorld();
  64. vehicleRayCaster_ = new btDefaultVehicleRaycaster(pbtDynWorld);
  65. btRigidBody *bthullBody = body->GetBody();
  66. vehicle_ = new btRaycastVehicle(tuning_, bthullBody,
  67. vehicleRayCaster_);
  68. pbtDynWorld->addVehicle(vehicle_);
  69. vehicle_->setCoordinateSystem(rightIndex, upIndex, forwardIndex);
  70. }
  71. btVehicleRaycaster *vehicleRayCaster_;
  72. btRaycastVehicle *vehicle_;
  73. btRaycastVehicle::btVehicleTuning tuning_;
  74. };
  75. RaycastVehicle::RaycastVehicle(Context* context)
  76. : LogicComponent(context)
  77. {
  78. // fixed update() for inputs and post update() to sync wheels for rendering
  79. SetUpdateEventMask(USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE | USE_POSTUPDATE);
  80. vehicleData_ = new RaycastVehicleData;
  81. wheelNodes_.Clear();
  82. activate_ = false;
  83. inAirRPM_ = 0.0f;
  84. maxSideSlipSpeed_ = 4.0f;
  85. }
  86. RaycastVehicle::~RaycastVehicle()
  87. {
  88. delete vehicleData_;
  89. wheelNodes_.Clear();
  90. }
  91. const char* wheelElementNames[] =
  92. {
  93. "Number of wheels",
  94. " Wheel node id",
  95. " Wheel direction",
  96. " Wheel axle",
  97. " Wheel rest length",
  98. " Wheel radius",
  99. " Wheel is front wheel",
  100. " Steering",
  101. " Connection point vector",
  102. " Original rotation",
  103. " Cumulative skid info",
  104. " Side skip speed",
  105. " Grounded",
  106. " Contact position",
  107. " Contact normal",
  108. " Suspension stiffness",
  109. " Damping relaxation",
  110. " Damping compression",
  111. " Friction slip",
  112. " Roll influence",
  113. " Engine force",
  114. " Brake",
  115. 0
  116. };
  117. void RaycastVehicle::RegisterObject(Context* context)
  118. {
  119. context->RegisterFactory<RaycastVehicle>();
  120. URHO3D_MIXED_ACCESSOR_VARIANT_VECTOR_STRUCTURE_ATTRIBUTE("Wheel data", GetWheelDataAttr, SetWheelDataAttr,
  121. VariantVector, Variant::emptyVariantVector,
  122. wheelElementNames, AM_DEFAULT);
  123. URHO3D_ATTRIBUTE("Maximum side slip threshold", float, maxSideSlipSpeed_, 4.0f, AM_DEFAULT);
  124. URHO3D_ATTRIBUTE("RPM for wheel motors in air (0=calculate)", float, inAirRPM_, 0.0f, AM_DEFAULT);
  125. }
  126. void RaycastVehicle::ApplyAttributes()
  127. {
  128. int index = 0;
  129. hullBody_ = node_->GetOrCreateComponent<RigidBody>();
  130. Scene* scene = GetScene();
  131. vehicleData_->Init(scene, hullBody_);
  132. VariantVector& value = loadedWheelData_;
  133. int numObjects = value[index++].GetInt();
  134. int wheelIndex = 0;
  135. origRotation_.Clear();
  136. skidInfoCumulative_.Clear();
  137. wheelSideSlipSpeed_.Clear();
  138. for (int i = 0; i < numObjects; i++)
  139. {
  140. int node_id = value[index++].GetInt();
  141. Vector3 direction = value[index++].GetVector3();
  142. Vector3 axle = value[index++].GetVector3();
  143. float restLength = value[index++].GetFloat();
  144. float radius = value[index++].GetFloat();
  145. bool isFrontWheel = value[index++].GetBool();
  146. float steering = value[index++].GetFloat();
  147. Vector3 connectionPoint = value[index++].GetVector3();
  148. Quaternion origRotation = value[index++].GetQuaternion();
  149. float skidInfoC = value[index++].GetFloat();
  150. float sideSlipSpeed = value[index++].GetFloat();
  151. bool isContact = value[index++].GetBool();
  152. Vector3 contactPosition = value[index++].GetVector3();
  153. Vector3 contactNormal = value[index++].GetVector3();
  154. float suspensionStiffness = value[index++].GetFloat();
  155. float dampingRelaxation = value[index++].GetFloat();
  156. float dampingCompression = value[index++].GetFloat();
  157. float frictionSlip = value[index++].GetFloat();
  158. float rollInfluence = value[index++].GetFloat();
  159. float engineForce = value[index++].GetFloat();
  160. float brake = value[index++].GetFloat();
  161. float skidInfo = value[index++].GetFloat();
  162. Node *wheelNode = GetScene()->GetNode(node_id);
  163. if (!wheelNode)
  164. {
  165. URHO3D_LOGERROR("RaycastVehicle: Incorrect node id = " + String(node_id) + " index: " + String(index));
  166. continue;
  167. }
  168. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  169. int id = GetNumWheels();
  170. btVector3 connectionPointCS0(connectionPoint.x_, connectionPoint.y_, connectionPoint.z_);
  171. btVector3 wheelDirectionCS0(direction.x_, direction.y_, direction.z_);
  172. btVector3 wheelAxleCS(axle.x_, axle.y_, axle.z_);
  173. btWheelInfo& wheel = vehicle_->addWheel(connectionPointCS0,
  174. wheelDirectionCS0,
  175. wheelAxleCS,
  176. restLength,
  177. radius,
  178. vehicleData_->tuning_,
  179. isFrontWheel);
  180. wheelNodes_.Push(wheelNode);
  181. origRotation_.Push(origRotation);
  182. skidInfoCumulative_.Push(skidInfoC);
  183. wheelSideSlipSpeed_.Push(sideSlipSpeed);
  184. SetSteeringValue(wheelIndex, steering);
  185. wheel.m_raycastInfo.m_isInContact = isContact;
  186. wheel.m_raycastInfo.m_contactNormalWS = btVector3(contactNormal.x_, contactNormal.y_, contactNormal.z_);
  187. wheel.m_raycastInfo.m_contactPointWS = btVector3(contactPosition.x_, contactPosition.y_, contactPosition.z_);
  188. wheel.m_suspensionStiffness = suspensionStiffness;
  189. wheel.m_wheelsDampingRelaxation = dampingRelaxation;
  190. wheel.m_wheelsDampingCompression = dampingCompression;
  191. wheel.m_frictionSlip = frictionSlip;
  192. wheel.m_rollInfluence = rollInfluence;
  193. wheel.m_engineForce = engineForce;
  194. wheel.m_brake = brake;
  195. wheel.m_skidInfo = skidInfo;
  196. wheelIndex++;
  197. }
  198. URHO3D_LOGDEBUG("maxSideSlipSpeed_ value: " + String(maxSideSlipSpeed_));
  199. URHO3D_LOGDEBUG("loaded items: " + String(index));
  200. URHO3D_LOGDEBUG("loaded wheels: " + String(GetNumWheels()));
  201. }
  202. void RaycastVehicle::Init()
  203. {
  204. hullBody_ = node_->GetOrCreateComponent<RigidBody>();
  205. Scene* scene = GetScene();
  206. vehicleData_->Init(scene, hullBody_);
  207. }
  208. void RaycastVehicle::FixedUpdate(float timeStep)
  209. {
  210. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  211. for (int i = 0; i < GetNumWheels(); i++)
  212. {
  213. btWheelInfo whInfo = vehicle_->getWheelInfo(i);
  214. if (whInfo.m_engineForce != 0.0f || whInfo.m_steering != 0.0f)
  215. {
  216. hullBody_->Activate();
  217. break;
  218. }
  219. }
  220. }
  221. void RaycastVehicle::PostUpdate(float timeStep)
  222. {
  223. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  224. for (int i = 0; i < GetNumWheels(); i++)
  225. {
  226. vehicle_->updateWheelTransform(i, true);
  227. btTransform transform = vehicle_->getWheelTransformWS(i);
  228. Vector3 origin = ToVector3(transform.getOrigin());
  229. Quaternion qRot = ToQuaternion(transform.getRotation());
  230. Node *pWheel = wheelNodes_[i];
  231. pWheel->SetWorldPosition(origin);
  232. pWheel->SetWorldRotation(qRot * origRotation_[i]);
  233. }
  234. }
  235. void RaycastVehicle::FixedPostUpdate(float timeStep)
  236. {
  237. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  238. Vector3 velocity = hullBody_->GetLinearVelocity();
  239. for (int i = 0; i < GetNumWheels(); i++)
  240. {
  241. btWheelInfo& whInfo = vehicle_->getWheelInfo(i);
  242. if (!WheelIsGrounded(i) && GetEngineForce(i) != 0.0f)
  243. {
  244. float delta;
  245. if (inAirRPM_ != 0.0f)
  246. {
  247. delta = inAirRPM_ * timeStep / 60.0f;
  248. }
  249. else
  250. {
  251. delta = 8.0f * GetEngineForce(i) * timeStep / (hullBody_->GetMass() * GetWheelRadius(i));
  252. }
  253. if (Abs(whInfo.m_deltaRotation) < Abs(delta))
  254. {
  255. whInfo.m_rotation += delta - whInfo.m_deltaRotation;
  256. whInfo.m_deltaRotation = delta;
  257. }
  258. if (skidInfoCumulative_[i] > 0.05f)
  259. {
  260. skidInfoCumulative_[i] -= 0.002;
  261. }
  262. }
  263. else
  264. {
  265. skidInfoCumulative_[i] = GetWheelSkidInfo(i);
  266. }
  267. wheelSideSlipSpeed_[i] = Abs(ToVector3(whInfo.m_raycastInfo.m_wheelAxleWS).DotProduct(velocity));
  268. if (wheelSideSlipSpeed_[i] > maxSideSlipSpeed_)
  269. {
  270. skidInfoCumulative_[i] = Clamp(skidInfoCumulative_[i], 0.0f, 0.89f);
  271. }
  272. }
  273. }
  274. void RaycastVehicle::SetMaxSideSlipSpeed(float speed)
  275. {
  276. maxSideSlipSpeed_ = speed;
  277. }
  278. float RaycastVehicle::GetMaxSideSlipSpeed() const
  279. {
  280. return maxSideSlipSpeed_;
  281. }
  282. void RaycastVehicle::SetWheelSkidInfoCumulative(int wheel, float skid)
  283. {
  284. skidInfoCumulative_[wheel] = skid;
  285. }
  286. float RaycastVehicle::GetWheelSkidInfoCumulative(int wheel) const
  287. {
  288. return skidInfoCumulative_[wheel];
  289. }
  290. void RaycastVehicle::AddWheel(Node *wheelNode,
  291. Vector3 wheelDirection, Vector3 wheelAxle,
  292. float restLength, float wheelRadius,
  293. bool frontWheel)
  294. {
  295. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  296. int id = GetNumWheels();
  297. Vector3 connectionPoint = wheelNode->GetWorldPosition() - node_->GetWorldPosition();
  298. btVector3 connectionPointCS0(connectionPoint.x_, connectionPoint.y_, connectionPoint.z_);
  299. btVector3 wheelDirectionCS0(wheelDirection.x_, wheelDirection.y_, wheelDirection.z_);
  300. btVector3 wheelAxleCS(wheelAxle.x_, wheelAxle.y_, wheelAxle.z_);
  301. btWheelInfo& wheel = vehicle_->addWheel(connectionPointCS0,
  302. wheelDirectionCS0,
  303. wheelAxleCS,
  304. restLength,
  305. wheelRadius,
  306. vehicleData_->tuning_,
  307. frontWheel);
  308. wheelNodes_.Push(wheelNode);
  309. origRotation_.Push(wheelNode->GetWorldRotation());
  310. skidInfoCumulative_.Push(1.0f);
  311. wheelSideSlipSpeed_.Push(0.0f);
  312. wheel.m_raycastInfo.m_isInContact = false;
  313. }
  314. void RaycastVehicle::ResetSuspension()
  315. {
  316. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  317. vehicle_->resetSuspension();
  318. }
  319. void RaycastVehicle::UpdateWheelTransform(int wheel, bool interpolated)
  320. {
  321. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  322. vehicle_->updateWheelTransform(wheel, interpolated);
  323. }
  324. Vector3 RaycastVehicle::GetWheelPosition(int wheel)
  325. {
  326. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  327. btTransform transform = vehicle_->getWheelTransformWS(wheel);
  328. Vector3 origin = ToVector3(transform.getOrigin());
  329. return origin;
  330. }
  331. Quaternion RaycastVehicle::GetWheelRotation(int wheel)
  332. {
  333. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  334. btTransform transform = vehicle_->getWheelTransformWS(wheel);
  335. Quaternion rotation = ToQuaternion(transform.getRotation());
  336. return rotation;
  337. }
  338. Vector3 RaycastVehicle::GetWheelConnectionPoint(int wheel) const
  339. {
  340. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  341. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  342. return ToVector3(whInfo.m_chassisConnectionPointCS);
  343. }
  344. void RaycastVehicle::SetSteeringValue(int wheel, float steeringValue)
  345. {
  346. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  347. vehicle_->setSteeringValue(steeringValue, wheel);
  348. }
  349. float RaycastVehicle::GetSteeringValue(int wheel) const
  350. {
  351. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  352. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  353. return whInfo.m_steering;
  354. }
  355. void RaycastVehicle::SetWheelSuspensionStiffness(int wheel, float stiffness)
  356. {
  357. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  358. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  359. whInfo.m_suspensionStiffness = stiffness;
  360. }
  361. float RaycastVehicle::GetWheelSuspensionStiffness(int wheel) const
  362. {
  363. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  364. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  365. return whInfo.m_suspensionStiffness;
  366. }
  367. void RaycastVehicle::SetWheelDampingRelaxation(int wheel, float damping)
  368. {
  369. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  370. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  371. whInfo.m_wheelsDampingRelaxation = damping;
  372. }
  373. float RaycastVehicle::GetWheelDampingRelaxation(int wheel) const
  374. {
  375. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  376. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  377. return whInfo.m_wheelsDampingRelaxation;
  378. }
  379. void RaycastVehicle::SetWheelDampingCompression(int wheel, float compression)
  380. {
  381. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  382. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  383. whInfo.m_wheelsDampingCompression = compression;
  384. }
  385. float RaycastVehicle::GetWheelDampingCompression(int wheel) const
  386. {
  387. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  388. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  389. return whInfo.m_wheelsDampingCompression;
  390. }
  391. void RaycastVehicle::SetWheelFrictionSlip(int wheel, float slip)
  392. {
  393. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  394. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  395. whInfo.m_frictionSlip = slip;
  396. }
  397. float RaycastVehicle::GetWheelFrictionSlip(int wheel) const
  398. {
  399. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  400. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  401. return whInfo.m_frictionSlip;
  402. }
  403. void RaycastVehicle::SetWheelRollInfluence(int wheel, float rollInfluence)
  404. {
  405. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  406. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  407. whInfo.m_rollInfluence = rollInfluence;
  408. }
  409. Vector3 RaycastVehicle::GetContactPosition(int wheel) const
  410. {
  411. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  412. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  413. return ToVector3(whInfo.m_raycastInfo.m_contactPointWS);
  414. }
  415. Vector3 RaycastVehicle::GetContactNormal(int wheel) const
  416. {
  417. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  418. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  419. return ToVector3(whInfo.m_raycastInfo.m_contactNormalWS);
  420. }
  421. float RaycastVehicle::GetWheelSideSlipSpeed(int wheel) const
  422. {
  423. return wheelSideSlipSpeed_[wheel];
  424. }
  425. float RaycastVehicle::GetWheelRollInfluence(int wheel) const
  426. {
  427. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  428. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  429. return whInfo.m_rollInfluence;
  430. }
  431. void RaycastVehicle::SetWheelRadius(int wheel, float wheelRadius)
  432. {
  433. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  434. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  435. whInfo.m_wheelsRadius = wheelRadius;
  436. }
  437. float RaycastVehicle::GetWheelRadius(int wheel) const
  438. {
  439. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  440. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  441. return whInfo.m_wheelsRadius;
  442. }
  443. void RaycastVehicle::SetEngineForce(int wheel, float force)
  444. {
  445. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  446. vehicle_->applyEngineForce(force, wheel);
  447. }
  448. float RaycastVehicle::GetEngineForce(int wheel) const
  449. {
  450. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  451. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  452. return whInfo.m_engineForce;
  453. }
  454. void RaycastVehicle::SetBrake(int wheel, float force)
  455. {
  456. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  457. vehicle_->setBrake(force, wheel);
  458. }
  459. float RaycastVehicle::GetBrake(int wheel) const
  460. {
  461. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  462. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  463. return whInfo.m_brake;
  464. }
  465. int RaycastVehicle::GetNumWheels() const
  466. {
  467. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  468. return vehicle_->getNumWheels();
  469. }
  470. Node *RaycastVehicle::GetWheelNode(int wheel) const
  471. {
  472. return wheelNodes_[wheel];
  473. }
  474. void RaycastVehicle::SetMaxSuspensionTravel(int wheel, float maxSuspensionTravel)
  475. {
  476. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  477. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  478. whInfo.m_maxSuspensionTravelCm = maxSuspensionTravel;
  479. }
  480. float RaycastVehicle::GetMaxSuspensionTravel(int wheel)
  481. {
  482. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  483. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  484. return whInfo.m_maxSuspensionTravelCm;
  485. }
  486. void RaycastVehicle::SetWheelDirection(int wheel, Vector3 direction)
  487. {
  488. btVector3 dir(direction.x_, direction.y_, direction.z_);
  489. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  490. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  491. whInfo.m_wheelDirectionCS = dir;
  492. }
  493. Vector3 RaycastVehicle::GetWheelDirection(int wheel) const
  494. {
  495. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  496. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  497. return ToVector3(whInfo.m_wheelDirectionCS);
  498. }
  499. void RaycastVehicle::SetWheelAxle(int wheel, Vector3 axle)
  500. {
  501. btVector3 ax(axle.x_, axle.y_, axle.z_);
  502. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  503. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  504. whInfo.m_wheelAxleCS = ax;
  505. }
  506. Vector3 RaycastVehicle::GetWheelAxle(int wheel) const
  507. {
  508. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  509. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  510. return ToVector3(whInfo.m_wheelAxleCS);
  511. }
  512. void RaycastVehicle::SetWheelRestLength(int wheel, float length)
  513. {
  514. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  515. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  516. whInfo.m_suspensionRestLength1 = length;
  517. }
  518. float RaycastVehicle::GetWheelRestLength(int wheel) const
  519. {
  520. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  521. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  522. return whInfo.m_suspensionRestLength1;
  523. }
  524. void RaycastVehicle::SetWheelSkidInfo(int wheel, float factor)
  525. {
  526. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  527. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  528. whInfo.m_skidInfo = factor;
  529. }
  530. float RaycastVehicle::GetWheelSkidInfo(int wheel) const
  531. {
  532. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  533. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  534. return whInfo.m_skidInfo;
  535. }
  536. bool RaycastVehicle::IsFrontWheel(int wheel) const
  537. {
  538. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  539. btWheelInfo& whInfo = vehicle_->getWheelInfo(wheel);
  540. return whInfo.m_bIsFrontWheel;
  541. }
  542. bool RaycastVehicle::WheelIsGrounded(int wheel) const
  543. {
  544. btRaycastVehicle *vehicle_ = vehicleData_->Get();
  545. btWheelInfo whInfo = vehicle_->getWheelInfo(wheel);
  546. return whInfo.m_raycastInfo.m_isInContact;
  547. }
  548. void RaycastVehicle::SetInAirRPM(float rpm)
  549. {
  550. inAirRPM_ = rpm;
  551. }
  552. float RaycastVehicle::GetInAirRPM() const
  553. {
  554. return inAirRPM_;
  555. }
  556. void RaycastVehicle::ResetWheels()
  557. {
  558. ResetSuspension();
  559. for (int i = 0; i < GetNumWheels(); i++)
  560. {
  561. UpdateWheelTransform(i, true);
  562. Vector3 origin = GetWheelPosition(i);
  563. Node *wheelNode = GetWheelNode(i);
  564. wheelNode->SetWorldPosition(origin);
  565. }
  566. }
  567. VariantVector RaycastVehicle::GetWheelDataAttr() const
  568. {
  569. VariantVector ret;
  570. ret.Reserve(GetNumWheels() * 22 + 1);
  571. ret.Push(GetNumWheels());
  572. for (int i = 0; i < GetNumWheels(); i++)
  573. {
  574. Node *wNode = GetWheelNode(i);
  575. int node_id = wNode->GetID();
  576. URHO3D_LOGDEBUG("RaycastVehicle: Saving node id = " + String(node_id));
  577. ret.Push(node_id);
  578. ret.Push(GetWheelDirection(i));
  579. ret.Push(GetWheelAxle(i));
  580. ret.Push(GetWheelRestLength(i));
  581. ret.Push(GetWheelRadius(i));
  582. ret.Push(IsFrontWheel(i));
  583. ret.Push(GetSteeringValue(i));
  584. ret.Push(GetWheelConnectionPoint(i));
  585. ret.Push(origRotation_[i]);
  586. ret.Push(GetWheelSkidInfoCumulative(i));
  587. ret.Push(GetWheelSideSlipSpeed(i));
  588. ret.Push(WheelIsGrounded(i));
  589. ret.Push(GetContactPosition(i));
  590. ret.Push(GetContactNormal(i)); // 14
  591. ret.Push(GetWheelSuspensionStiffness(i));
  592. ret.Push(GetWheelDampingRelaxation(i));
  593. ret.Push(GetWheelDampingCompression(i));
  594. ret.Push(GetWheelFrictionSlip(i));
  595. ret.Push(GetWheelRollInfluence(i));
  596. ret.Push(GetEngineForce(i));
  597. ret.Push(GetBrake(i));
  598. ret.Push(GetWheelSkidInfo(i));
  599. }
  600. URHO3D_LOGDEBUG("RaycastVehicle: saved items: " + String(ret.Size()));
  601. URHO3D_LOGDEBUG("maxSideSlipSpeed_ value save: " + String(maxSideSlipSpeed_));
  602. return ret;
  603. }
  604. void RaycastVehicle::SetWheelDataAttr(const VariantVector& value)
  605. {
  606. if (!vehicleData_)
  607. {
  608. URHO3D_LOGERROR("RaycastVehicle: vehicleData doesn't exist");
  609. return;
  610. }
  611. if (value.Size() < 2)
  612. {
  613. URHO3D_LOGERROR("RaycastVehicle: Incorrect vehicleData");
  614. return;
  615. }
  616. loadedWheelData_ = value;
  617. }
  618. } // namespace Urho3D