Joint.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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. #include "Precompiled.h"
  24. #include "Joint.h"
  25. #include "Log.h"
  26. #include "PhysicsWorld.h"
  27. #include "ReplicationUtils.h"
  28. #include "RigidBody.h"
  29. #include "Scene.h"
  30. #include "StringUtils.h"
  31. #include <ode/ode.h>
  32. #include "DebugNew.h"
  33. static const std::string typeNames[] =
  34. {
  35. "none",
  36. "ball",
  37. "hinge",
  38. };
  39. Joint::Joint(PhysicsWorld* world, const std::string& name) :
  40. Component(name),
  41. mWorld(world),
  42. mType(JOINT_NONE),
  43. mJoint(0),
  44. mNetDirty(false)
  45. {
  46. if (!mWorld)
  47. EXCEPTION("Null physics world for Joint");
  48. }
  49. Joint::~Joint()
  50. {
  51. reset();
  52. }
  53. void Joint::save(Serializer& dest)
  54. {
  55. // Write Component properties
  56. Component::save(dest);
  57. // Write Joint properties
  58. dest.writeUByte((unsigned char)mType);
  59. ComponentRef bodyARef(mBodyA);
  60. ComponentRef bodyBRef(mBodyB);
  61. bodyARef.write(dest);
  62. bodyBRef.write(dest);
  63. switch (mType)
  64. {
  65. case JOINT_BALL:
  66. dest.writeVector3(getPosition());
  67. break;
  68. case JOINT_HINGE:
  69. dest.writeVector3(getPosition());
  70. dest.writeVector3(getAxis());
  71. break;
  72. }
  73. }
  74. void Joint::load(Deserializer& source, ResourceCache* cache)
  75. {
  76. // Read Component properties
  77. Component::load(source, cache);
  78. // Read Joint properties
  79. mType = (JointType)source.readUByte();
  80. mBodyARef.read(source);
  81. mBodyBRef.read(source);
  82. switch (mType)
  83. {
  84. case JOINT_BALL:
  85. mPosition = source.readVector3();
  86. break;
  87. case JOINT_HINGE:
  88. mPosition = source.readVector3();
  89. mAxis = source.readVector3();
  90. break;
  91. }
  92. }
  93. void Joint::saveXML(XMLElement& dest)
  94. {
  95. // Write Component properties
  96. Component::saveXML(dest);
  97. // Write Joint properties
  98. XMLElement jointElem = dest.createChildElement("joint");
  99. jointElem.setString("type", typeNames[mType]);
  100. XMLElement bodyAElem = dest.createChildElement("body_a");
  101. XMLElement bodyBElem = dest.createChildElement("body_b");
  102. ComponentRef bodyARef(mBodyA, true);
  103. ComponentRef bodyBRef(mBodyB, true);
  104. bodyARef.writeXML(bodyAElem);
  105. bodyBRef.writeXML(bodyBElem);
  106. switch (mType)
  107. {
  108. case JOINT_BALL:
  109. jointElem.setVector3("pos", getPosition());
  110. break;
  111. case JOINT_HINGE:
  112. jointElem.setVector3("pos", getPosition());
  113. jointElem.setVector3("axis", getAxis());
  114. break;
  115. }
  116. }
  117. void Joint::loadXML(const XMLElement& source, ResourceCache* cache)
  118. {
  119. // Read Component properties
  120. Component::loadXML(source, cache);
  121. // Read Joint properties
  122. XMLElement jointElem = source.getChildElement("joint");
  123. XMLElement bodyAElem = source.getChildElement("body_a");
  124. XMLElement bodyBElem = source.getChildElement("body_b");
  125. mType = (JointType)getIndexFromStringList(jointElem.getStringLower("type"), typeNames, 3, 0);
  126. mBodyARef.readXML(bodyAElem);
  127. mBodyBRef.readXML(bodyBElem);
  128. switch (mType)
  129. {
  130. case JOINT_BALL:
  131. mPosition = jointElem.getVector3("pos");
  132. break;
  133. case JOINT_HINGE:
  134. mPosition = jointElem.getVector3("pos");
  135. mAxis = jointElem.getVector3("axis");
  136. break;
  137. }
  138. }
  139. void Joint::postLoad(ResourceCache* cache)
  140. {
  141. RigidBody* bodyA = static_cast<RigidBody*>(mEntity->getScene()->getComponent(mBodyARef));
  142. RigidBody* bodyB = static_cast<RigidBody*>(mEntity->getScene()->getComponent(mBodyBRef));
  143. switch (mType)
  144. {
  145. case JOINT_BALL:
  146. if (!setBall(mPosition, bodyA, bodyB))
  147. mType = JOINT_NONE;
  148. break;
  149. case JOINT_HINGE:
  150. if (!setHinge(mPosition, mAxis, bodyA, bodyB))
  151. mType = JOINT_NONE;
  152. break;
  153. }
  154. }
  155. bool Joint::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  156. {
  157. unsigned char bits = 0;
  158. ComponentRef bodyARef(mBodyA);
  159. ComponentRef bodyBRef(mBodyB);
  160. JointType type = mType;
  161. checkUByte((unsigned char)type, (unsigned char)JOINT_NONE, baseRevision, bits, 1);
  162. checkComponentRef(bodyARef, ComponentRef(), baseRevision, bits, 2);
  163. checkComponentRef(bodyBRef, ComponentRef(), baseRevision, bits, 2);
  164. switch (type)
  165. {
  166. case JOINT_BALL:
  167. checkVector3(getPosition(), baseRevision, bits, 4);
  168. // If whole joint will be recreated, also send position
  169. if (bits & 3)
  170. bits |= 4;
  171. break;
  172. case JOINT_HINGE:
  173. checkVector3(getPosition(), baseRevision, bits, 4);
  174. checkVector3(getAxis(), baseRevision, bits, 8);
  175. // If whole joint will be recreated, also send position and axis
  176. if (bits & 3)
  177. bits |= 12;
  178. break;
  179. }
  180. // Update replication state fully, and network stream by delta
  181. dest.writeUByte(bits);
  182. writeUByteDelta((unsigned char)type, dest, destRevision, bits & 1);
  183. writeComponentRefDelta(bodyARef, dest, destRevision, bits & 2);
  184. writeComponentRefDelta(bodyBRef, dest, destRevision, bits & 2);
  185. writeVector3Delta(getPosition(), dest, destRevision, bits & 4);
  186. writeVector3Delta(getAxis(), dest, destRevision, bits & 8);
  187. return bits != 0;
  188. }
  189. void Joint::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  190. {
  191. unsigned char bits = source.readUByte();
  192. if (bits & 1)
  193. mType = (JointType)source.readUByte();
  194. if (bits & 2)
  195. {
  196. mBodyARef.readPacked(source);
  197. mBodyBRef.readPacked(source);
  198. }
  199. if (bits & 4)
  200. mPosition = source.readVector3();
  201. if (bits & 8)
  202. mAxis = source.readVector3();
  203. // If joint type or body refs changed, recreate whole joint
  204. if (bits & 3)
  205. mNetDirty = true;
  206. else
  207. {
  208. // Otherwise just reset the position or axis as applicable
  209. if (bits & 4)
  210. setPosition(mPosition);
  211. if (bits & 8)
  212. setAxis(mAxis);
  213. }
  214. }
  215. void Joint::postNetUpdate(ResourceCache* cache)
  216. {
  217. if (mNetDirty)
  218. {
  219. postLoad(cache);
  220. mNetDirty = false;
  221. }
  222. }
  223. void Joint::getComponentRefs(std::vector<ComponentRef>& dest)
  224. {
  225. if (mBodyA)
  226. dest.push_back(ComponentRef(mBodyA));
  227. if (mBodyB)
  228. dest.push_back(ComponentRef(mBodyB));
  229. }
  230. void Joint::reset()
  231. {
  232. if (mJoint)
  233. {
  234. dJointDestroy(mJoint);
  235. mJoint = 0;
  236. }
  237. mBodyA.reset();
  238. mBodyB.reset();
  239. mType = JOINT_NONE;
  240. }
  241. bool Joint::setBall(const Vector3& position, RigidBody* bodyA, RigidBody* bodyB)
  242. {
  243. reset();
  244. if ((!bodyA) && (!bodyB))
  245. return false;
  246. mJoint = dJointCreateBall(mWorld->getWorld(), 0);
  247. dJointSetData(mJoint, this);
  248. dJointSetBallAnchor(mJoint, position.mX, position.mY, position.mZ);
  249. dJointAttach(mJoint, bodyA ? bodyA->getBody() : 0, bodyB ? bodyB->getBody() : 0);
  250. mType = JOINT_BALL;
  251. mBodyA = bodyA;
  252. mBodyB = bodyB;
  253. return true;
  254. }
  255. bool Joint::setHinge(const Vector3& position, const Vector3& axis, RigidBody* bodyA, RigidBody* bodyB)
  256. {
  257. reset();
  258. if ((!bodyA) && (!bodyB))
  259. return false;
  260. Vector3 normalizedAxis = axis.getNormalized();
  261. mJoint = dJointCreateHinge(mWorld->getWorld(), 0);
  262. dJointSetData(mJoint, this);
  263. dJointSetHingeAnchor(mJoint, position.mX, position.mY, position.mZ);
  264. dJointSetHingeAxis(mJoint, normalizedAxis.mX, normalizedAxis.mY, normalizedAxis.mZ);
  265. dJointAttach(mJoint, bodyA ? bodyA->getBody() : 0, bodyB ? bodyB->getBody() : 0);
  266. mType = JOINT_HINGE;
  267. mBodyA = bodyA;
  268. mBodyB = bodyB;
  269. return true;
  270. }
  271. void Joint::setPosition(const Vector3& position)
  272. {
  273. switch (mType)
  274. {
  275. case JOINT_BALL:
  276. dJointSetBallAnchor(mJoint, position.mX, position.mY, position.mZ);
  277. break;
  278. case JOINT_HINGE:
  279. dJointSetHingeAnchor(mJoint, position.mX, position.mY, position.mZ);
  280. break;
  281. }
  282. }
  283. void Joint::setAxis(const Vector3& axis)
  284. {
  285. switch (mType)
  286. {
  287. case JOINT_HINGE:
  288. dJointSetHingeAxis(mJoint, axis.mX, axis.mY, axis.mZ);
  289. break;
  290. }
  291. }
  292. Vector3 Joint::getPosition() const
  293. {
  294. dVector3 pos;
  295. switch (mType)
  296. {
  297. case JOINT_BALL:
  298. dJointGetBallAnchor(mJoint, pos);
  299. return Vector3(pos[0], pos[1], pos[2]);
  300. case JOINT_HINGE:
  301. dJointGetHingeAnchor(mJoint, pos);
  302. return Vector3(pos[0], pos[1], pos[2]);
  303. }
  304. return Vector3::sZero;
  305. }
  306. Vector3 Joint::getAxis() const
  307. {
  308. dVector3 axis;
  309. switch (mType)
  310. {
  311. case JOINT_HINGE:
  312. dJointGetHingeAxis(mJoint, axis);
  313. return Vector3(axis[0], axis[1], axis[2]);
  314. }
  315. return Vector3::sZero;
  316. }