Joint.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "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. };
  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. {
  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);
  57. ATTRIBUTE(Joint, VAR_INT, "Body A", bodyA_, 0);
  58. ATTRIBUTE(Joint, VAR_INT, "Body B", bodyB_, 0);
  59. ATTRIBUTE(Joint, VAR_VECTOR3, "Position", position_, Vector3::ZERO);
  60. ATTRIBUTE(Joint, VAR_VECTOR3, "Axis", axis_, Vector3::ZERO);
  61. }
  62. void Joint::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  63. {
  64. Scene* scene = node_ ? node_->GetScene() : 0;
  65. switch (attr.offset_)
  66. {
  67. case offsetof(Joint, bodyA_):
  68. bodyA_ = scene ? dynamic_cast<RigidBody*>(scene->GetComponentByID(value.GetInt())) : (RigidBody*)0;
  69. break;
  70. case offsetof(Joint, bodyB_):
  71. bodyB_ = scene ? dynamic_cast<RigidBody*>(scene->GetComponentByID(value.GetInt())) : (RigidBody*)0;
  72. break;
  73. default:
  74. Serializable::OnSetAttribute(attr, value);
  75. break;
  76. }
  77. }
  78. Variant Joint::OnGetAttribute(const AttributeInfo& attr)
  79. {
  80. switch (attr.offset_)
  81. {
  82. case offsetof(Joint, bodyA_):
  83. return bodyA_ ? bodyA_->GetID() : 0;
  84. case offsetof(Joint, bodyB_):
  85. return bodyB_ ? bodyB_->GetID() : 0;
  86. case offsetof(Joint, position_):
  87. return GetPosition();
  88. case offsetof(Joint, axis_):
  89. return GetAxis();
  90. default:
  91. return Serializable::OnGetAttribute(attr);
  92. }
  93. }
  94. void Joint::PostLoad()
  95. {
  96. switch (type_)
  97. {
  98. case JOINT_NONE:
  99. Clear();
  100. break;
  101. case JOINT_BALL:
  102. SetBall(position_, bodyA_, bodyB_);
  103. break;
  104. case JOINT_HINGE:
  105. SetHinge(position_, axis_, bodyA_, bodyB_);
  106. break;
  107. }
  108. }
  109. void Joint::Clear()
  110. {
  111. if (joint_)
  112. {
  113. dJointDestroy(joint_);
  114. joint_ = 0;
  115. }
  116. bodyA_.Reset();
  117. bodyB_.Reset();
  118. type_ = JOINT_NONE;
  119. }
  120. bool Joint::SetBall(const Vector3& position, RigidBody* bodyA, RigidBody* bodyB)
  121. {
  122. Clear();
  123. if (!physicsWorld_)
  124. {
  125. LOGERROR("Null physics world, can not set joint type");
  126. return false;
  127. }
  128. if ((!bodyA) && (!bodyB))
  129. {
  130. LOGERROR("Both bodies null, can not create joint");
  131. return false;
  132. }
  133. joint_ = dJointCreateBall(physicsWorld_->GetWorld(), 0);
  134. dJointSetData(joint_, this);
  135. dJointSetBallAnchor(joint_, position.x_, position.y_, position.z_);
  136. dJointAttach(joint_, bodyA ? bodyA->GetBody() : 0, bodyB ? bodyB->GetBody() : 0);
  137. type_ = JOINT_BALL;
  138. bodyA_ = bodyA;
  139. bodyB_ = bodyB;
  140. return true;
  141. }
  142. bool Joint::SetHinge(const Vector3& position, const Vector3& axis, RigidBody* bodyA, RigidBody* bodyB)
  143. {
  144. Clear();
  145. if (!physicsWorld_)
  146. {
  147. LOGERROR("Null physics world, can not set joint type");
  148. return false;
  149. }
  150. if ((!bodyA) && (!bodyB))
  151. {
  152. LOGERROR("Both bodies null, can not create joint");
  153. return false;
  154. }
  155. Vector3 NormalizedAxis = axis.GetNormalized();
  156. joint_ = dJointCreateHinge(physicsWorld_->GetWorld(), 0);
  157. dJointSetData(joint_, this);
  158. dJointSetHingeAnchor(joint_, position.x_, position.y_, position.z_);
  159. dJointSetHingeAxis(joint_, NormalizedAxis.x_, NormalizedAxis.y_, NormalizedAxis.z_);
  160. dJointAttach(joint_, bodyA ? bodyA->GetBody() : 0, bodyB ? bodyB->GetBody() : 0);
  161. type_ = JOINT_HINGE;
  162. bodyA_ = bodyA;
  163. bodyB_ = bodyB;
  164. return true;
  165. }
  166. void Joint::SetPosition(const Vector3& position)
  167. {
  168. switch (type_)
  169. {
  170. case JOINT_BALL:
  171. dJointSetBallAnchor(joint_, position.x_, position.y_, position.z_);
  172. break;
  173. case JOINT_HINGE:
  174. dJointSetHingeAnchor(joint_, position.x_, position.y_, position.z_);
  175. break;
  176. }
  177. }
  178. void Joint::SetAxis(const Vector3& axis)
  179. {
  180. switch (type_)
  181. {
  182. case JOINT_HINGE:
  183. dJointSetHingeAxis(joint_, axis.x_, axis.y_, axis.z_);
  184. break;
  185. }
  186. }
  187. Vector3 Joint::GetPosition() const
  188. {
  189. dVector3 pos;
  190. switch (type_)
  191. {
  192. case JOINT_BALL:
  193. dJointGetBallAnchor(joint_, pos);
  194. return Vector3(pos[0], pos[1], pos[2]);
  195. case JOINT_HINGE:
  196. dJointGetHingeAnchor(joint_, pos);
  197. return Vector3(pos[0], pos[1], pos[2]);
  198. }
  199. return Vector3::ZERO;
  200. }
  201. Vector3 Joint::GetAxis() const
  202. {
  203. dVector3 axis;
  204. switch (type_)
  205. {
  206. case JOINT_HINGE:
  207. dJointGetHingeAxis(joint_, axis);
  208. return Vector3(axis[0], axis[1], axis[2]);
  209. }
  210. return Vector3::ZERO;
  211. }
  212. void Joint::OnNodeSet(Node* node)
  213. {
  214. if (node)
  215. {
  216. Scene* scene = node->GetScene();
  217. if (scene)
  218. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  219. }
  220. }