Joint.cpp 6.3 KB

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