Joint.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "DebugRenderer.h"
  26. #include "Joint.h"
  27. #include "Log.h"
  28. #include "PhysicsWorld.h"
  29. #include "RigidBody.h"
  30. #include "Scene.h"
  31. #include <ode/ode.h>
  32. #include "DebugNew.h"
  33. static const String typeNames[] =
  34. {
  35. "None",
  36. "Ball",
  37. "Hinge",
  38. ""
  39. };
  40. OBJECTTYPESTATIC(Joint);
  41. Joint::Joint(Context* context) :
  42. Component(context),
  43. type_(JOINT_NONE),
  44. joint_(0),
  45. position_(Vector3::ZERO),
  46. axis_(Vector3::ZERO),
  47. otherBodyNodeID_(0),
  48. recreateJoint_(false)
  49. {
  50. }
  51. Joint::~Joint()
  52. {
  53. Clear();
  54. if (physicsWorld_)
  55. physicsWorld_->RemoveJoint(this);
  56. }
  57. void Joint::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<Joint>();
  60. ENUM_ATTRIBUTE(Joint, "Joint Type", type_, typeNames, JOINT_NONE, AM_DEFAULT);
  61. REF_ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  62. REF_ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Axis", GetAxis, SetAxis, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  63. ATTRIBUTE(Joint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  64. }
  65. void Joint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  66. {
  67. Serializable::OnSetAttribute(attr, src);
  68. // Change of the joint type or connected body requires the joint to be recreated
  69. if (attr.offset_ == offsetof(Joint, type_) || attr.offset_ == offsetof(Joint, otherBodyNodeID_))
  70. recreateJoint_ = true;
  71. }
  72. void Joint::ApplyAttributes()
  73. {
  74. if (recreateJoint_)
  75. {
  76. otherBody_.Reset();
  77. Scene* scene = node_ ? node_->GetScene() : 0;
  78. if (scene && otherBodyNodeID_)
  79. {
  80. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  81. if (otherNode)
  82. otherBody_ = otherNode->GetComponent<RigidBody>();
  83. }
  84. SetJointType(type_);
  85. recreateJoint_ = false;
  86. }
  87. }
  88. void Joint::GetDependencyNodes(PODVector<Node*>& dest)
  89. {
  90. if (otherBody_ && otherBody_->GetNode())
  91. dest.Push(otherBody_->GetNode());
  92. }
  93. void Joint::Clear()
  94. {
  95. if (joint_ && physicsWorld_)
  96. {
  97. dJointDestroy(joint_);
  98. joint_ = 0;
  99. type_ = JOINT_NONE;
  100. }
  101. }
  102. bool Joint::SetJointType(JointType type)
  103. {
  104. Clear();
  105. if (type == JOINT_NONE)
  106. return true;
  107. if (!physicsWorld_)
  108. {
  109. LOGERROR("Null physics world, can not set joint type");
  110. return false;
  111. }
  112. ownBody_ = static_cast<RigidBody*>(GetComponent(RigidBody::GetTypeStatic()));
  113. if (!ownBody_)
  114. {
  115. LOGERROR("No rigid body in node, can not set joint type");
  116. return false;
  117. }
  118. type_ = type;
  119. switch (type)
  120. {
  121. case JOINT_NONE:
  122. break;
  123. case JOINT_BALL:
  124. joint_ = dJointCreateBall(physicsWorld_->GetWorld(), 0);
  125. dJointSetData(joint_, this);
  126. dJointAttach(joint_, ownBody_->GetBody(), otherBody_ ? otherBody_->GetBody() : 0);
  127. SetPosition(position_);
  128. break;
  129. case JOINT_HINGE:
  130. joint_ = dJointCreateHinge(physicsWorld_->GetWorld(), 0);
  131. dJointSetData(joint_, this);
  132. dJointAttach(joint_, ownBody_->GetBody(), otherBody_ ? otherBody_->GetBody() : 0);
  133. SetPosition(position_);
  134. SetAxis(axis_);
  135. break;
  136. }
  137. return true;
  138. }
  139. void Joint::SetOtherBody(RigidBody* body)
  140. {
  141. if (otherBody_ != body)
  142. {
  143. otherBody_ = body;
  144. // Update the connected body attribute
  145. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  146. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  147. if (joint_)
  148. dJointAttach(joint_, ownBody_->GetBody(), otherBody_ ? otherBody_->GetBody() : 0);
  149. }
  150. }
  151. void Joint::SetPosition(const Vector3& position)
  152. {
  153. position_ = position;
  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(const Vector3& axis)
  169. {
  170. axis_ = axis.Normalized();
  171. if (joint_)
  172. {
  173. dJointType type = dJointGetType(joint_);
  174. switch (type)
  175. {
  176. case dJointTypeHinge:
  177. dJointSetHingeAxis(joint_, axis_.x_, axis_.y_, axis_.z_);
  178. break;
  179. }
  180. }
  181. }
  182. const Vector3& Joint::GetPosition() const
  183. {
  184. dVector3 pos;
  185. if (joint_)
  186. {
  187. dJointType type = dJointGetType(joint_);
  188. switch (type)
  189. {
  190. case dJointTypeBall:
  191. dJointGetBallAnchor(joint_, pos);
  192. position_ = Vector3(pos[0], pos[1], pos[2]);
  193. break;
  194. case dJointTypeHinge:
  195. dJointGetHingeAnchor(joint_, pos);
  196. position_ = Vector3(pos[0], pos[1], pos[2]);
  197. break;
  198. }
  199. }
  200. return position_;
  201. }
  202. const Vector3& Joint::GetAxis() const
  203. {
  204. dVector3 axis;
  205. if (joint_)
  206. {
  207. dJointType type = dJointGetType(joint_);
  208. switch (type)
  209. {
  210. case dJointTypeHinge:
  211. dJointGetHingeAxis(joint_, axis);
  212. axis_ = Vector3(axis[0], axis[1], axis[2]);
  213. break;
  214. }
  215. }
  216. return axis_;
  217. }
  218. void Joint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  219. {
  220. if (debug && joint_ && ownBody_)
  221. {
  222. const Vector3& jointPos = GetPosition();
  223. const Vector3& ownBodyPos = ownBody_->GetPosition();
  224. const Vector3& otherBodyPos = otherBody_ ? otherBody_->GetPosition() : Vector3::ZERO;
  225. BoundingBox jointBox;
  226. jointBox.Merge(jointPos);
  227. jointBox.Merge(ownBodyPos);
  228. if (otherBody_)
  229. jointBox.Merge(otherBodyPos);
  230. if (debug->IsInside(jointBox))
  231. {
  232. debug->AddLine(jointPos, ownBodyPos, Color::YELLOW, depthTest);
  233. if (otherBody_)
  234. debug->AddLine(jointPos, otherBodyPos, Color::YELLOW, depthTest);
  235. if (type_ == JOINT_HINGE)
  236. {
  237. const Vector3& axis = GetAxis();
  238. debug->AddLine(jointPos + 0.1f * axis, jointPos - 0.1f * axis, Color::WHITE, depthTest);
  239. }
  240. }
  241. }
  242. }
  243. void Joint::OnNodeSet(Node* node)
  244. {
  245. if (node)
  246. {
  247. Scene* scene = node->GetScene();
  248. if (scene)
  249. {
  250. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  251. if (physicsWorld_)
  252. physicsWorld_->AddJoint(this);
  253. }
  254. node->AddListener(this);
  255. // Set default position at the node
  256. position_ = node->GetWorldPosition();
  257. }
  258. }