Constraint.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "Constraint.h"
  27. #include "Log.h"
  28. #include "PhysicsUtils.h"
  29. #include "PhysicsWorld.h"
  30. #include "Profiler.h"
  31. #include "RigidBody.h"
  32. #include "Scene.h"
  33. #include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
  34. #include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
  35. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  36. #include "DebugNew.h"
  37. static const String typeNames[] =
  38. {
  39. "Point",
  40. "Hinge",
  41. ""
  42. };
  43. static const float DEFAULT_LOW_LIMIT = -180.0f;
  44. static const float DEFAULT_HIGH_LIMIT = 180.0f;
  45. OBJECTTYPESTATIC(Constraint);
  46. Constraint::Constraint(Context* context) :
  47. Component(context),
  48. constraint_(0),
  49. type_(CONSTRAINT_POINT),
  50. position_(Vector3::ZERO),
  51. otherPosition_(Vector3::ZERO),
  52. axis_(Vector3::ZERO),
  53. otherAxis_(Vector3::ZERO),
  54. lowLimit_(DEFAULT_LOW_LIMIT),
  55. highLimit_(DEFAULT_HIGH_LIMIT),
  56. otherBodyNodeID_(0),
  57. disableCollision_(false),
  58. recreateConstraint_(false)
  59. {
  60. }
  61. Constraint::~Constraint()
  62. {
  63. ReleaseConstraint();
  64. if (physicsWorld_)
  65. physicsWorld_->RemoveConstraint(this);
  66. }
  67. void Constraint::RegisterObject(Context* context)
  68. {
  69. context->RegisterFactory<Constraint>();
  70. ENUM_ATTRIBUTE(Constraint, "Constraint Type", type_, typeNames, CONSTRAINT_POINT, AM_DEFAULT);
  71. ATTRIBUTE(Constraint, VAR_VECTOR3, "Position", position_, Vector3::ZERO, AM_DEFAULT);
  72. ATTRIBUTE(Constraint, VAR_VECTOR3, "Axis", axis_, Vector3::ZERO, AM_DEFAULT);
  73. ATTRIBUTE(Constraint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  74. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Position", otherPosition_, Vector3::ZERO, AM_DEFAULT);
  75. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Axis", otherAxis_, Vector3::ZERO, AM_DEFAULT);
  76. ATTRIBUTE(Constraint, VAR_FLOAT, "Low Limit", lowLimit_, DEFAULT_LOW_LIMIT, AM_DEFAULT);
  77. ATTRIBUTE(Constraint, VAR_FLOAT, "High Limit", highLimit_, DEFAULT_HIGH_LIMIT, AM_DEFAULT);
  78. ATTRIBUTE(Constraint, VAR_BOOL, "Disable Collision", disableCollision_, false, AM_DEFAULT);
  79. }
  80. void Constraint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  81. {
  82. Component::OnSetAttribute(attr, src);
  83. // Change of any attribute requires recreation of the constraint
  84. recreateConstraint_ = true;
  85. }
  86. void Constraint::ApplyAttributes()
  87. {
  88. if (recreateConstraint_)
  89. {
  90. otherBody_.Reset();
  91. Scene* scene = GetScene();
  92. if (scene && otherBodyNodeID_)
  93. {
  94. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  95. if (otherNode)
  96. otherBody_ = otherNode->GetComponent<RigidBody>();
  97. }
  98. CreateConstraint();
  99. recreateConstraint_ = false;
  100. }
  101. }
  102. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  103. {
  104. if (otherBody_ && otherBody_->GetNode())
  105. dest.Push(otherBody_->GetNode());
  106. }
  107. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  108. {
  109. if (debug && physicsWorld_ && constraint_)
  110. {
  111. physicsWorld_->SetDebugRenderer(debug);
  112. physicsWorld_->SetDebugDepthTest(depthTest);
  113. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  114. physicsWorld_->SetDebugRenderer(0);
  115. }
  116. }
  117. void Constraint::SetConstraintType(ConstraintType type)
  118. {
  119. type_ = type;
  120. CreateConstraint();
  121. }
  122. void Constraint::SetOtherBody(RigidBody* body)
  123. {
  124. if (otherBody_ != body)
  125. {
  126. otherBody_ = body;
  127. // Update the connected body attribute
  128. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  129. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  130. CreateConstraint();
  131. }
  132. }
  133. void Constraint::SetPosition(const Vector3& position)
  134. {
  135. if (position != position_)
  136. {
  137. position_ = position;
  138. CreateConstraint();
  139. }
  140. }
  141. void Constraint::SetOtherPosition(const Vector3& position)
  142. {
  143. if (position != otherPosition_)
  144. {
  145. otherPosition_ = position;
  146. CreateConstraint();
  147. }
  148. }
  149. void Constraint::SetAxis(const Vector3& axis)
  150. {
  151. if (axis != axis_)
  152. {
  153. axis_ = axis;
  154. if (type_ == CONSTRAINT_HINGE)
  155. CreateConstraint();
  156. }
  157. }
  158. void Constraint::SetOtherAxis(const Vector3& axis)
  159. {
  160. if (axis != otherAxis_)
  161. {
  162. otherAxis_ = axis;
  163. if (type_ == CONSTRAINT_HINGE)
  164. CreateConstraint();
  165. }
  166. }
  167. void Constraint::SetLowLimit(float limit)
  168. {
  169. lowLimit_ = limit;
  170. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  171. {
  172. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  173. hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
  174. }
  175. }
  176. void Constraint::SetHighLimit(float limit)
  177. {
  178. highLimit_ = limit;
  179. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  180. {
  181. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  182. hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
  183. }
  184. }
  185. void Constraint::ReleaseConstraint()
  186. {
  187. if (constraint_)
  188. {
  189. if (physicsWorld_)
  190. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  191. delete constraint_;
  192. constraint_ = 0;
  193. }
  194. }
  195. void Constraint::OnNodeSet(Node* node)
  196. {
  197. if (node)
  198. {
  199. Scene* scene = GetScene();
  200. if (scene)
  201. {
  202. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  203. if (physicsWorld_)
  204. physicsWorld_->AddConstraint(this);
  205. else
  206. LOGERROR("No physics world component in scene, can not create constraint");
  207. }
  208. node->AddListener(this);
  209. }
  210. }
  211. void Constraint::CreateConstraint()
  212. {
  213. PROFILE(CreateConstraint);
  214. ReleaseConstraint();
  215. ownBody_ = GetComponent<RigidBody>();
  216. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  217. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  218. if (!physicsWorld_ || !ownBody)
  219. return;
  220. switch (type_)
  221. {
  222. case CONSTRAINT_POINT:
  223. if (otherBody)
  224. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(position_), ToBtVector3(otherPosition_));
  225. else
  226. constraint_ = new btPoint2PointConstraint(*ownBody, ToBtVector3(position_));
  227. break;
  228. case CONSTRAINT_HINGE:
  229. {
  230. btHingeConstraint* hingeConstraint;
  231. if (otherBody)
  232. {
  233. constraint_ = hingeConstraint = new btHingeConstraint(*ownBody, *otherBody, ToBtVector3(position_),
  234. ToBtVector3(otherPosition_), ToBtVector3(axis_.Normalized()), ToBtVector3(otherAxis_.Normalized()));
  235. }
  236. else
  237. {
  238. constraint_ = hingeConstraint = new btHingeConstraint(*ownBody, ToBtVector3(position_),
  239. ToBtVector3(axis_.Normalized()));
  240. }
  241. hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
  242. }
  243. break;
  244. }
  245. constraint_->setUserConstraintPtr(this);
  246. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  247. }