Joint.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "Context.h"
  25. #include "Joint.h"
  26. #include "Log.h"
  27. #include "PhysicsWorld.h"
  28. #include "RigidBody.h"
  29. #include "Scene.h"
  30. #include <ode/ode.h>
  31. #include "DebugNew.h"
  32. static const String typeNames[] =
  33. {
  34. "none",
  35. "ball",
  36. "hinge",
  37. ""
  38. };
  39. OBJECTTYPESTATIC(Joint);
  40. Joint::Joint(Context* context) :
  41. Component(context),
  42. type_(JOINT_NONE),
  43. joint_(0),
  44. position_(Vector3::ZERO),
  45. axis_(Vector3::ZERO),
  46. recreateJoint_(false)
  47. {
  48. }
  49. Joint::~Joint()
  50. {
  51. Clear();
  52. }
  53. void Joint::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Joint>();
  56. ENUM_ATTRIBUTE(Joint, "Joint Type", type_, typeNames, JOINT_NONE, AM_DEFAULT);
  57. ACCESSOR_ATTRIBUTE(Joint, VAR_INT, "Body A", GetBodyAAttr, SetBodyAAttr, int, 0, AM_DEFAULT);
  58. ACCESSOR_ATTRIBUTE(Joint, VAR_INT, "Body B", GetBodyBAttr, SetBodyBAttr, int, 0, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  60. ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Axis", GetAxis, SetAxis, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  61. }
  62. void Joint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  63. {
  64. Serializable::OnSetAttribute(attr, src);
  65. // Change of the joint type requires the joint to be recreated
  66. if (attr.offset_ == offsetof(Joint, type_))
  67. recreateJoint_ = true;
  68. }
  69. void Joint::FinishUpdate()
  70. {
  71. if (recreateJoint_)
  72. {
  73. switch (type_)
  74. {
  75. case JOINT_NONE:
  76. Clear();
  77. break;
  78. case JOINT_BALL:
  79. SetBall(position_, bodyA_, bodyB_);
  80. break;
  81. case JOINT_HINGE:
  82. SetHinge(position_, axis_, bodyA_, bodyB_);
  83. break;
  84. }
  85. recreateJoint_ = false;
  86. }
  87. }
  88. void Joint::GetDependencyNodes(PODVector<Node*>& dest)
  89. {
  90. if (bodyA_ && bodyA_->GetNode())
  91. dest.Push(bodyA_->GetNode());
  92. if (bodyB_ && bodyB_->GetNode())
  93. dest.Push(bodyB_->GetNode());
  94. }
  95. void Joint::Clear()
  96. {
  97. if (joint_)
  98. {
  99. dJointDestroy(joint_);
  100. joint_ = 0;
  101. }
  102. bodyA_.Reset();
  103. bodyB_.Reset();
  104. type_ = JOINT_NONE;
  105. }
  106. bool Joint::SetBall(const Vector3& position, RigidBody* bodyA, RigidBody* bodyB)
  107. {
  108. Clear();
  109. if (!physicsWorld_)
  110. {
  111. LOGERROR("Null physics world, can not set joint type");
  112. return false;
  113. }
  114. if (!bodyA && !bodyB)
  115. {
  116. LOGERROR("Both bodies null, can not create joint");
  117. return false;
  118. }
  119. joint_ = dJointCreateBall(physicsWorld_->GetWorld(), 0);
  120. dJointSetData(joint_, this);
  121. dJointSetBallAnchor(joint_, position.x_, position.y_, position.z_);
  122. dJointAttach(joint_, bodyA ? bodyA->GetBody() : 0, bodyB ? bodyB->GetBody() : 0);
  123. type_ = JOINT_BALL;
  124. bodyA_ = bodyA;
  125. bodyB_ = bodyB;
  126. return true;
  127. }
  128. bool Joint::SetHinge(const Vector3& position, const Vector3& axis, RigidBody* bodyA, RigidBody* bodyB)
  129. {
  130. Clear();
  131. if (!physicsWorld_)
  132. {
  133. LOGERROR("Null physics world, can not set joint type");
  134. return false;
  135. }
  136. if (!bodyA && !bodyB)
  137. {
  138. LOGERROR("Both bodies null, can not create joint");
  139. return false;
  140. }
  141. Vector3 NormalizedAxis = axis.Normalized();
  142. joint_ = dJointCreateHinge(physicsWorld_->GetWorld(), 0);
  143. dJointSetData(joint_, this);
  144. dJointSetHingeAnchor(joint_, position.x_, position.y_, position.z_);
  145. dJointSetHingeAxis(joint_, NormalizedAxis.x_, NormalizedAxis.y_, NormalizedAxis.z_);
  146. dJointAttach(joint_, bodyA ? bodyA->GetBody() : 0, bodyB ? bodyB->GetBody() : 0);
  147. type_ = JOINT_HINGE;
  148. bodyA_ = bodyA;
  149. bodyB_ = bodyB;
  150. return true;
  151. }
  152. void Joint::SetPosition(Vector3 position)
  153. {
  154. if (joint_)
  155. {
  156. dJointType type = dJointGetType(joint_);
  157. switch (type)
  158. {
  159. case dJointTypeBall:
  160. dJointSetBallAnchor(joint_, position.x_, position.y_, position.z_);
  161. break;
  162. case dJointTypeHinge:
  163. dJointSetHingeAnchor(joint_, position.x_, position.y_, position.z_);
  164. break;
  165. }
  166. }
  167. }
  168. void Joint::SetAxis(Vector3 axis)
  169. {
  170. if (joint_)
  171. {
  172. dJointType type = dJointGetType(joint_);
  173. switch (type)
  174. {
  175. case dJointTypeHinge:
  176. dJointSetHingeAxis(joint_, axis.x_, axis.y_, axis.z_);
  177. break;
  178. }
  179. }
  180. }
  181. Vector3 Joint::GetPosition() const
  182. {
  183. dVector3 pos;
  184. if (joint_)
  185. {
  186. dJointType type = dJointGetType(joint_);
  187. switch (type)
  188. {
  189. case dJointTypeBall:
  190. dJointGetBallAnchor(joint_, pos);
  191. return Vector3(pos[0], pos[1], pos[2]);
  192. case dJointTypeHinge:
  193. dJointGetHingeAnchor(joint_, pos);
  194. return Vector3(pos[0], pos[1], pos[2]);
  195. }
  196. }
  197. return Vector3::ZERO;
  198. }
  199. Vector3 Joint::GetAxis() const
  200. {
  201. dVector3 axis;
  202. if (joint_)
  203. {
  204. dJointType type = dJointGetType(joint_);
  205. switch (type)
  206. {
  207. case dJointTypeHinge:
  208. dJointGetHingeAxis(joint_, axis);
  209. return Vector3(axis[0], axis[1], axis[2]);
  210. }
  211. }
  212. return Vector3::ZERO;
  213. }
  214. void Joint::SetBodyAAttr(int value)
  215. {
  216. Scene* scene = node_ ? node_->GetScene() : 0;
  217. bodyA_ = scene ? dynamic_cast<RigidBody*>(scene->GetComponentByID(value)) : (RigidBody*)0;
  218. recreateJoint_ = true;
  219. }
  220. void Joint::SetBodyBAttr(int value)
  221. {
  222. Scene* scene = node_ ? node_->GetScene() : 0;
  223. bodyB_ = scene ? dynamic_cast<RigidBody*>(scene->GetComponentByID(value)) : (RigidBody*)0;
  224. recreateJoint_ = true;
  225. }
  226. int Joint::GetBodyAAttr() const
  227. {
  228. return bodyA_ ? bodyA_->GetID() : 0;
  229. }
  230. int Joint::GetBodyBAttr() const
  231. {
  232. return bodyB_ ? bodyB_->GetID() : 0;
  233. }
  234. void Joint::OnNodeSet(Node* node)
  235. {
  236. if (node)
  237. {
  238. Scene* scene = node->GetScene();
  239. if (scene)
  240. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  241. }
  242. }