Constraint.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. axis_(Vector3::RIGHT),
  52. otherBodyPosition_(Vector3::ZERO),
  53. otherBodyAxis_(Vector3::RIGHT),
  54. lowLimit_(DEFAULT_LOW_LIMIT),
  55. highLimit_(DEFAULT_HIGH_LIMIT),
  56. otherBodyNodeID_(0),
  57. disableCollision_(false),
  58. recreateConstraint_(false),
  59. otherBodyPositionValid_(false)
  60. {
  61. }
  62. Constraint::~Constraint()
  63. {
  64. ReleaseConstraint();
  65. if (physicsWorld_)
  66. physicsWorld_->RemoveConstraint(this);
  67. }
  68. void Constraint::RegisterObject(Context* context)
  69. {
  70. context->RegisterFactory<Constraint>();
  71. ENUM_ATTRIBUTE(Constraint, "Constraint Type", type_, typeNames, CONSTRAINT_POINT, AM_DEFAULT);
  72. ATTRIBUTE(Constraint, VAR_VECTOR3, "Position", position_, Vector3::ZERO, AM_DEFAULT);
  73. ATTRIBUTE(Constraint, VAR_VECTOR3, "Axis", axis_, Vector3::RIGHT, AM_DEFAULT);
  74. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Position", otherBodyPosition_, Vector3::ZERO, AM_DEFAULT | AM_NOEDIT);
  75. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Axis", otherBodyAxis_, Vector3::RIGHT, AM_DEFAULT | AM_NOEDIT);
  76. ACCESSOR_ATTRIBUTE(Constraint, VAR_FLOAT, "Low Limit", GetLowLimit, SetLowLimit, float, DEFAULT_LOW_LIMIT, AM_DEFAULT);
  77. ACCESSOR_ATTRIBUTE(Constraint, VAR_FLOAT, "High Limit", GetHighLimit, SetHighLimit, float, DEFAULT_HIGH_LIMIT, AM_DEFAULT);
  78. ATTRIBUTE(Constraint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  79. ATTRIBUTE(Constraint, VAR_BOOL, "Disable Collision", disableCollision_, false, AM_DEFAULT);
  80. }
  81. void Constraint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  82. {
  83. Component::OnSetAttribute(attr, src);
  84. // Change of any non-accessor attribute requires recreation of the constraint
  85. if (!attr.accessor_)
  86. {
  87. recreateConstraint_ = true;
  88. if (attr.offset_ == offsetof(Constraint, otherBodyPosition_) || attr.offset_ == offsetof(Constraint, otherBodyAxis_))
  89. otherBodyPositionValid_ = true;
  90. }
  91. }
  92. void Constraint::ApplyAttributes()
  93. {
  94. if (recreateConstraint_)
  95. {
  96. otherBody_.Reset();
  97. Scene* scene = GetScene();
  98. if (scene && otherBodyNodeID_)
  99. {
  100. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  101. if (otherNode)
  102. otherBody_ = otherNode->GetComponent<RigidBody>();
  103. }
  104. CreateConstraint();
  105. recreateConstraint_ = false;
  106. }
  107. }
  108. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  109. {
  110. if (otherBody_ && otherBody_->GetNode())
  111. dest.Push(otherBody_->GetNode());
  112. }
  113. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  114. {
  115. if (debug && physicsWorld_ && constraint_)
  116. {
  117. physicsWorld_->SetDebugRenderer(debug);
  118. physicsWorld_->SetDebugDepthTest(depthTest);
  119. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  120. physicsWorld_->SetDebugRenderer(0);
  121. }
  122. }
  123. void Constraint::SetConstraintType(ConstraintType type)
  124. {
  125. if (type != type_)
  126. {
  127. type_ = type;
  128. CreateConstraint();
  129. }
  130. }
  131. void Constraint::SetOtherBody(RigidBody* body)
  132. {
  133. if (otherBody_ != body)
  134. {
  135. otherBody_ = body;
  136. // Update the connected body attribute
  137. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  138. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  139. CreateConstraint();
  140. }
  141. }
  142. void Constraint::SetPosition(const Vector3& position)
  143. {
  144. if (position != position_)
  145. {
  146. position_ = position;
  147. /// \todo Optimize and do not recreate the constraint
  148. if (constraint_)
  149. CreateConstraint();
  150. }
  151. }
  152. void Constraint::SetAxis(const Vector3& axis)
  153. {
  154. if (axis != axis_)
  155. {
  156. axis_ = axis;
  157. /// \todo Optimize and do not recreate the constraint
  158. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  159. CreateConstraint();
  160. }
  161. }
  162. void Constraint::SetLowLimit(float limit)
  163. {
  164. if (limit != lowLimit_)
  165. {
  166. lowLimit_ = limit;
  167. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  168. {
  169. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  170. hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
  171. }
  172. }
  173. }
  174. void Constraint::SetHighLimit(float limit)
  175. {
  176. if (limit != highLimit_)
  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. }
  186. void Constraint::ReleaseConstraint()
  187. {
  188. if (constraint_)
  189. {
  190. if (physicsWorld_)
  191. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  192. delete constraint_;
  193. constraint_ = 0;
  194. }
  195. }
  196. void Constraint::OnNodeSet(Node* node)
  197. {
  198. if (node)
  199. {
  200. Scene* scene = GetScene();
  201. if (scene)
  202. {
  203. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  204. if (physicsWorld_)
  205. physicsWorld_->AddConstraint(this);
  206. else
  207. LOGERROR("No physics world component in scene, can not create constraint");
  208. }
  209. node->AddListener(this);
  210. // Try to create constraint immediately, may fail if the rigid body component does not exist yet
  211. CreateConstraint();
  212. }
  213. }
  214. void Constraint::OnMarkedDirty(Node* node)
  215. {
  216. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  217. CreateConstraint();
  218. }
  219. void Constraint::CreateConstraint()
  220. {
  221. PROFILE(CreateConstraint);
  222. cachedWorldScale_ = node_->GetWorldScale();
  223. ReleaseConstraint();
  224. ownBody_ = GetComponent<RigidBody>();
  225. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  226. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  227. if (!physicsWorld_ || !ownBody)
  228. return;
  229. if (!otherBody)
  230. otherBody = &btTypedConstraint::getFixedBody();
  231. btTransform ownInverse = ownBody->getWorldTransform().inverse();
  232. btTransform otherInverse = otherBody->getWorldTransform().inverse();
  233. // If the deserialized constraint other body position is valid, use it, but only this time
  234. if (otherBodyPositionValid_)
  235. otherBodyPositionValid_ = false;
  236. else
  237. {
  238. // Otherwise calculate it from own body's position
  239. otherBodyPosition_ = ToVector3(otherInverse * (ownBody->getWorldTransform() * ToBtVector3(position_ *
  240. cachedWorldScale_)));
  241. otherBodyAxis_ = ToVector3(otherInverse.getBasis() * (ownBody->getWorldTransform().getBasis() *
  242. ToBtVector3(axis_)));
  243. }
  244. switch (type_)
  245. {
  246. case CONSTRAINT_POINT:
  247. {
  248. btPoint2PointConstraint* pointConstraint;
  249. constraint_ = pointConstraint = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(position_ *
  250. cachedWorldScale_), ToBtVector3(otherBodyPosition_));
  251. }
  252. break;
  253. case CONSTRAINT_HINGE:
  254. {
  255. btHingeConstraint* hingeConstraint;
  256. Quaternion ownRotation(Vector3::FORWARD, axis_);
  257. Quaternion otherRotation(Vector3::FORWARD, otherBodyAxis_);
  258. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  259. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  260. constraint_ = hingeConstraint = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  261. hingeConstraint->setLimit(lowLimit_ * M_DEGTORAD, highLimit_ * M_DEGTORAD);
  262. }
  263. break;
  264. }
  265. constraint_->setUserConstraintPtr(this);
  266. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  267. }