PhysicsVehicleWheel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "PhysicsVehicle.h"
  4. #include "PhysicsVehicleWheel.h"
  5. namespace gameplay
  6. {
  7. PhysicsVehicleWheel::PhysicsVehicleWheel(Node* node, const PhysicsCollisionShape::Definition& shape, const PhysicsRigidBody::Parameters& parameters)
  8. : PhysicsCollisionObject(node)
  9. {
  10. // Note that the constructor for PhysicsRigidBody calls addCollisionObject and so
  11. // that is where the rigid body gets added to the dynamics world.
  12. _rigidBody = new PhysicsRigidBody(node, shape, parameters);
  13. findAncestorAndBind();
  14. }
  15. PhysicsVehicleWheel::PhysicsVehicleWheel(Node* node, PhysicsRigidBody* rigidBody)
  16. : PhysicsCollisionObject(node)
  17. {
  18. _rigidBody = rigidBody;
  19. findAncestorAndBind();
  20. }
  21. PhysicsVehicleWheel* PhysicsVehicleWheel::create(Node* node, Properties* properties)
  22. {
  23. // Note that the constructor for PhysicsRigidBody calls addCollisionObject and so
  24. // that is where the rigid body gets added to the dynamics world.
  25. PhysicsRigidBody* rigidBody = PhysicsRigidBody::create(node, properties, "VEHICLE_WHEEL");
  26. PhysicsVehicleWheel* wheel = new PhysicsVehicleWheel(node, rigidBody);
  27. // Load the defined wheel parameters.
  28. properties->rewind();
  29. Vector3 v;
  30. const char* name;
  31. while ((name = properties->getNextProperty()) != NULL)
  32. {
  33. if (strcmp(name, "steerable") == 0)
  34. {
  35. wheel->setSteerable(properties->getBool(name));
  36. }
  37. else if (strcmp(name, "wheelDirection") == 0 && properties->getVector3(name, &v))
  38. {
  39. wheel->setWheelDirection(v);
  40. }
  41. else if (strcmp(name, "wheelAxle") == 0 && properties->getVector3(name, &v))
  42. {
  43. wheel->setWheelAxle(v);
  44. }
  45. else if (strcmp(name, "strutConnectionOffset") == 0 && properties->getVector3(name, &v))
  46. {
  47. wheel->setStrutConnectionOffset(v);
  48. }
  49. else if (strcmp(name, "strutRestLength") == 0)
  50. {
  51. wheel->setStrutRestLength(properties->getFloat(name));
  52. }
  53. else if (strcmp(name, "strutTravelMax") == 0)
  54. {
  55. wheel->setStrutTravelMax(properties->getFloat(name));
  56. }
  57. else if (strcmp(name, "strutStiffness") == 0)
  58. {
  59. wheel->setStrutStiffness(properties->getFloat(name));
  60. }
  61. else if (strcmp(name, "strutDampingCompression") == 0)
  62. {
  63. wheel->setStrutDampingCompression(properties->getFloat(name));
  64. }
  65. else if (strcmp(name, "strutDampingRelaxation") == 0)
  66. {
  67. wheel->setStrutDampingRelaxation(properties->getFloat(name));
  68. }
  69. else if (strcmp(name, "strutForceMax") == 0)
  70. {
  71. wheel->setStrutForceMax(properties->getFloat(name));
  72. }
  73. else if (strcmp(name, "frictionBreakout") == 0)
  74. {
  75. wheel->setFrictionBreakout(properties->getFloat(name));
  76. }
  77. else if (strcmp(name, "wheelRadius") == 0)
  78. {
  79. wheel->setWheelRadius(properties->getFloat(name));
  80. }
  81. else if (strcmp(name, "rollInfluence") == 0)
  82. {
  83. wheel->setRollInfluence(properties->getFloat(name));
  84. }
  85. else
  86. {
  87. // Ignore this case (we've already parsed the rigid body parameters).
  88. }
  89. }
  90. return wheel;
  91. }
  92. PhysicsVehicleWheel::~PhysicsVehicleWheel()
  93. {
  94. SAFE_DELETE(_rigidBody);
  95. }
  96. btCollisionObject* PhysicsVehicleWheel::getCollisionObject() const
  97. {
  98. GP_ASSERT(_rigidBody);
  99. return _rigidBody->getCollisionObject();
  100. }
  101. PhysicsCollisionObject::Type PhysicsVehicleWheel::getType() const
  102. {
  103. return PhysicsCollisionObject::VEHICLE_WHEEL;
  104. }
  105. void PhysicsVehicleWheel::setEnabled(bool enable)
  106. {
  107. GP_ERROR("Operation not supported (PhysicsVehicleWheel::setEnabled(bool)). Use host vehicle instead.");
  108. }
  109. void PhysicsVehicleWheel::findAncestorAndBind()
  110. {
  111. GP_ASSERT(getNode());
  112. // This is not an efficient algorithm if the number of advertised
  113. // descendants gets large. In fact, this search is O(n*m) in the
  114. // worst case with n nodes and m advertised descendants per node.
  115. // But (1) we are only visiting ancestor nodes, and (2) the number
  116. // of advertised descendants is expected to be small since this
  117. // mechanism is currently only used for binding wheels onto a vehicle.
  118. //
  119. // TODO: revisit if the advertised descendants mechanism becomes popular.
  120. PhysicsVehicle* host = NULL;
  121. PhysicsCollisionObject* collisionObject;
  122. Node* m;
  123. for (Node* n = getNode()->getParent(); n && !host; n = n->getParent())
  124. {
  125. for (unsigned int i = 0; i < n->getNumAdvertisedDescendants() && !host; i++)
  126. {
  127. m = n->getAdvertisedDescendant(i);
  128. collisionObject = m->getCollisionObject();
  129. if (collisionObject && collisionObject->getType() == PhysicsCollisionObject::VEHICLE)
  130. {
  131. host = static_cast<PhysicsVehicle*>(collisionObject);
  132. }
  133. }
  134. }
  135. // Note: Currently this method is silent on failure to find a host.
  136. if (host)
  137. {
  138. host->addWheel(this);
  139. _initialOffset = _node->getTranslation() - host->_node->getTranslation();
  140. }
  141. }
  142. void PhysicsVehicleWheel::setHost(PhysicsVehicle* host, unsigned int indexInHost)
  143. {
  144. _host = host;
  145. _indexInHost = indexInHost;
  146. }
  147. void PhysicsVehicleWheel::addToVehicle(btRaycastVehicle* vehicle)
  148. {
  149. GP_ASSERT(_host);
  150. GP_ASSERT(_host->getNumWheels() == vehicle->getNumWheels() + 1);
  151. // Use safe defaults for now. Properties are assigned elsewhere.
  152. btRaycastVehicle::btVehicleTuning tuning;
  153. vehicle->addWheel(
  154. btVector3(0, 0, 0),
  155. btVector3(0, -1, 0),
  156. btVector3(-1, 0, 0),
  157. 0.6f,
  158. 0.5f,
  159. tuning,
  160. false);
  161. }
  162. void PhysicsVehicleWheel::transform(Node* node) const
  163. {
  164. GP_ASSERT(_host);
  165. GP_ASSERT(_host->_node);
  166. node->setRotation(_orientation);
  167. // Use only the component parallel to the defined strut line
  168. Vector3 strutLine;
  169. getWheelDirection(&strutLine);
  170. _host->_node->getMatrix().transformVector(&strutLine);
  171. Vector3 wheelPos;
  172. getWheelPos(&wheelPos);
  173. node->setTranslation(wheelPos + strutLine*(strutLine.dot(_positionDelta) / strutLine.lengthSquared()));
  174. }
  175. void PhysicsVehicleWheel::update(float elapsedTime)
  176. {
  177. GP_ASSERT(_host);
  178. GP_ASSERT(_host->_vehicle);
  179. const btTransform& trans = _host->_vehicle->getWheelInfo(_indexInHost).m_worldTransform;
  180. const btQuaternion& rot = trans.getRotation();
  181. const btVector3& pos = trans.getOrigin();
  182. _orientation.set(rot.x(), rot.y(), rot.z(), rot.w());
  183. Vector3 commandedPosition(pos.x(), pos.y(), pos.z());
  184. Vector3 wheelPos;
  185. getWheelPos(&wheelPos);
  186. commandedPosition -= wheelPos;
  187. // Filter out noise from Bullet
  188. Vector3 delta(_positionDelta, commandedPosition);
  189. float threshold = getStrutRestLength() * 2.0f;
  190. float responseTime = (delta.lengthSquared() > threshold*threshold) ? 0 : 60;
  191. _positionDelta.smooth(commandedPosition, elapsedTime, responseTime);
  192. }
  193. void PhysicsVehicleWheel::getConnectionDefault(Vector3* result) const
  194. {
  195. // projected strut length
  196. getWheelDirection(result);
  197. result->normalize();
  198. float length = 0.58f * getStrutRestLength();
  199. *result *= -length;
  200. // nudge wheel contact point to outer edge of tire for stability
  201. Vector3 nudge;
  202. getWheelAxle(&nudge);
  203. nudge *= nudge.dot(_initialOffset);
  204. nudge.normalize();
  205. *result += nudge * 0.068f * getWheelRadius(); // rough-in for tire width
  206. // offset at bind time
  207. *result += _initialOffset;
  208. }
  209. void PhysicsVehicleWheel::getWheelPos(Vector3* result) const
  210. {
  211. GP_ASSERT(_host);
  212. GP_ASSERT(_host->_node);
  213. *result = _initialOffset;
  214. _host->_node->getMatrix().transformPoint(result);
  215. }
  216. bool PhysicsVehicleWheel::isSteerable() const
  217. {
  218. GP_ASSERT(_host);
  219. GP_ASSERT(_host->_vehicle);
  220. return _host->_vehicle->getWheelInfo(_indexInHost).m_bIsFrontWheel;
  221. }
  222. void PhysicsVehicleWheel::setSteerable(bool steerable)
  223. {
  224. GP_ASSERT(_host);
  225. GP_ASSERT(_host->_vehicle);
  226. _host->_vehicle->getWheelInfo(_indexInHost).m_bIsFrontWheel = steerable;
  227. }
  228. void PhysicsVehicleWheel::getWheelDirection(Vector3* wheelDirection) const
  229. {
  230. GP_ASSERT(_host);
  231. GP_ASSERT(_host->_vehicle);
  232. const btVector3& v = _host->_vehicle->getWheelInfo(_indexInHost).m_wheelDirectionCS;
  233. wheelDirection->set(v.x(), v.y(), v.z());
  234. }
  235. void PhysicsVehicleWheel::setWheelDirection(const Vector3& wheelDirection)
  236. {
  237. GP_ASSERT(_host);
  238. GP_ASSERT(_host->_vehicle);
  239. _host->_vehicle->getWheelInfo(_indexInHost).m_wheelDirectionCS.setValue(wheelDirection.x, wheelDirection.y, wheelDirection.z);
  240. }
  241. void PhysicsVehicleWheel::getWheelAxle(Vector3* wheelAxle) const
  242. {
  243. GP_ASSERT(_host);
  244. GP_ASSERT(_host->_vehicle);
  245. const btVector3& v = _host->_vehicle->getWheelInfo(_indexInHost).m_wheelAxleCS;
  246. wheelAxle->set(v.x(), v.y(), v.z());
  247. }
  248. void PhysicsVehicleWheel::setWheelAxle(const Vector3& wheelAxle)
  249. {
  250. GP_ASSERT(_host);
  251. GP_ASSERT(_host->_vehicle);
  252. _host->_vehicle->getWheelInfo(_indexInHost).m_wheelAxleCS.setValue( wheelAxle.x, wheelAxle.y, wheelAxle.z);
  253. }
  254. void PhysicsVehicleWheel::getStrutConnectionOffset(Vector3* strutConnectionOffset) const
  255. {
  256. GP_ASSERT(_host);
  257. GP_ASSERT(_host->_vehicle);
  258. const btVector3& v = _host->_vehicle->getWheelInfo(_indexInHost).m_chassisConnectionPointCS;
  259. strutConnectionOffset->set(v.x(), v.y(), v.z());
  260. Vector3 strutConnectionDefault;
  261. getConnectionDefault(&strutConnectionDefault);
  262. *strutConnectionOffset -= strutConnectionDefault;
  263. }
  264. void PhysicsVehicleWheel::setStrutConnectionOffset(const Vector3& strutConnectionOffset)
  265. {
  266. GP_ASSERT(_host);
  267. GP_ASSERT(_host->_vehicle);
  268. Vector3 strutConnectionPoint;
  269. getConnectionDefault(&strutConnectionPoint);
  270. strutConnectionPoint += strutConnectionOffset;
  271. _host->_vehicle->getWheelInfo(_indexInHost).m_chassisConnectionPointCS.setValue(strutConnectionPoint.x,
  272. strutConnectionPoint.y,
  273. strutConnectionPoint.z);
  274. }
  275. float PhysicsVehicleWheel::getStrutRestLength() const
  276. {
  277. GP_ASSERT(_host);
  278. GP_ASSERT(_host->_vehicle);
  279. return _host->_vehicle->getWheelInfo(_indexInHost).m_suspensionRestLength1;
  280. }
  281. void PhysicsVehicleWheel::setStrutRestLength(float strutRestLength)
  282. {
  283. GP_ASSERT(_host);
  284. GP_ASSERT(_host->_vehicle);
  285. _host->_vehicle->getWheelInfo(_indexInHost).m_suspensionRestLength1 = strutRestLength;
  286. }
  287. float PhysicsVehicleWheel::getStrutTravelMax() const
  288. {
  289. GP_ASSERT(_host);
  290. GP_ASSERT(_host->_vehicle);
  291. return _host->_vehicle->getWheelInfo(_indexInHost).m_maxSuspensionTravelCm / 100.0f;
  292. }
  293. void PhysicsVehicleWheel::setStrutTravelMax(float strutTravelMax)
  294. {
  295. GP_ASSERT(_host);
  296. GP_ASSERT(_host->_vehicle);
  297. _host->_vehicle->getWheelInfo(_indexInHost).m_maxSuspensionTravelCm = strutTravelMax * 100.0f;
  298. }
  299. float PhysicsVehicleWheel::getStrutStiffness() const
  300. {
  301. GP_ASSERT(_host);
  302. GP_ASSERT(_host->_vehicle);
  303. return _host->_vehicle->getWheelInfo(_indexInHost).m_suspensionStiffness;
  304. }
  305. void PhysicsVehicleWheel::setStrutStiffness(float strutStiffness)
  306. {
  307. GP_ASSERT(_host);
  308. GP_ASSERT(_host->_vehicle);
  309. _host->_vehicle->getWheelInfo(_indexInHost).m_suspensionStiffness = strutStiffness;
  310. }
  311. float PhysicsVehicleWheel::getStrutDampingCompression() const
  312. {
  313. GP_ASSERT(_host);
  314. GP_ASSERT(_host->_vehicle);
  315. return _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsDampingCompression;
  316. }
  317. void PhysicsVehicleWheel::setStrutDampingCompression(float strutDampingCompression)
  318. {
  319. GP_ASSERT(_host);
  320. GP_ASSERT(_host->_vehicle);
  321. _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsDampingCompression = strutDampingCompression;
  322. }
  323. float PhysicsVehicleWheel::getStrutDampingRelaxation() const
  324. {
  325. GP_ASSERT(_host);
  326. GP_ASSERT(_host->_vehicle);
  327. return _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsDampingRelaxation;
  328. }
  329. void PhysicsVehicleWheel::setStrutDampingRelaxation(float strutDampingRelaxation)
  330. {
  331. GP_ASSERT(_host);
  332. GP_ASSERT(_host->_vehicle);
  333. _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsDampingRelaxation = strutDampingRelaxation;
  334. }
  335. float PhysicsVehicleWheel::getStrutForceMax() const
  336. {
  337. GP_ASSERT(_host);
  338. GP_ASSERT(_host->_vehicle);
  339. return _host->_vehicle->getWheelInfo(_indexInHost).m_maxSuspensionForce;
  340. }
  341. void PhysicsVehicleWheel::setStrutForceMax(float strutForceMax)
  342. {
  343. GP_ASSERT(_host);
  344. GP_ASSERT(_host->_vehicle);
  345. _host->_vehicle->getWheelInfo(_indexInHost).m_maxSuspensionForce = strutForceMax;
  346. }
  347. float PhysicsVehicleWheel::getFrictionBreakout() const
  348. {
  349. GP_ASSERT(_host);
  350. GP_ASSERT(_host->_vehicle);
  351. return _host->_vehicle->getWheelInfo(_indexInHost).m_frictionSlip;
  352. }
  353. void PhysicsVehicleWheel::setFrictionBreakout(float frictionBreakout)
  354. {
  355. GP_ASSERT(_host);
  356. GP_ASSERT(_host->_vehicle);
  357. _host->_vehicle->getWheelInfo(_indexInHost).m_frictionSlip = frictionBreakout;
  358. }
  359. float PhysicsVehicleWheel::getWheelRadius() const
  360. {
  361. GP_ASSERT(_host);
  362. GP_ASSERT(_host->_vehicle);
  363. return _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsRadius;
  364. }
  365. void PhysicsVehicleWheel::setWheelRadius(float wheelRadius)
  366. {
  367. GP_ASSERT(_host);
  368. GP_ASSERT(_host->_vehicle);
  369. _host->_vehicle->getWheelInfo(_indexInHost).m_wheelsRadius = wheelRadius;
  370. }
  371. float PhysicsVehicleWheel::getRollInfluence() const
  372. {
  373. GP_ASSERT(_host);
  374. GP_ASSERT(_host->_vehicle);
  375. return _host->_vehicle->getWheelInfo(_indexInHost).m_rollInfluence;
  376. }
  377. void PhysicsVehicleWheel::setRollInfluence(float rollInfluence)
  378. {
  379. GP_ASSERT(_host);
  380. GP_ASSERT(_host->_vehicle);
  381. _host->_vehicle->getWheelInfo(_indexInHost).m_rollInfluence = rollInfluence;
  382. }
  383. }