Constraint.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/btConeTwistConstraint.h>
  34. #include <BulletDynamics/ConstraintSolver/btHingeConstraint.h>
  35. #include <BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h>
  36. #include <BulletDynamics/ConstraintSolver/btSliderConstraint.h>
  37. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  38. #include "DebugNew.h"
  39. static const String typeNames[] =
  40. {
  41. "Point",
  42. "Hinge",
  43. "Slider",
  44. "ConeTwist",
  45. ""
  46. };
  47. OBJECTTYPESTATIC(Constraint);
  48. Constraint::Constraint(Context* context) :
  49. Component(context),
  50. constraint_(0),
  51. type_(CONSTRAINT_POINT),
  52. position_(Vector3::ZERO),
  53. axis_(Vector3::RIGHT),
  54. otherBodyPosition_(Vector3::ZERO),
  55. otherBodyAxis_(Vector3::RIGHT),
  56. lowLimit_(Vector3::ZERO),
  57. highLimit_(Vector3::ZERO),
  58. otherBodyNodeID_(0),
  59. disableCollision_(false),
  60. recreateConstraint_(false),
  61. otherBodyPositionValid_(false)
  62. {
  63. }
  64. Constraint::~Constraint()
  65. {
  66. ReleaseConstraint();
  67. if (physicsWorld_)
  68. physicsWorld_->RemoveConstraint(this);
  69. }
  70. void Constraint::RegisterObject(Context* context)
  71. {
  72. context->RegisterFactory<Constraint>();
  73. ENUM_ATTRIBUTE(Constraint, "Constraint Type", type_, typeNames, CONSTRAINT_POINT, AM_DEFAULT);
  74. ATTRIBUTE(Constraint, VAR_VECTOR3, "Position", position_, Vector3::ZERO, AM_DEFAULT);
  75. ATTRIBUTE(Constraint, VAR_VECTOR3, "Axis", axis_, Vector3::RIGHT, AM_DEFAULT);
  76. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Position", otherBodyPosition_, Vector3::ZERO, AM_DEFAULT | AM_NOEDIT);
  77. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Axis", otherBodyAxis_, Vector3::RIGHT, AM_DEFAULT | AM_NOEDIT);
  78. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR3, "High Limit", GetHighLimit, SetHighLimit, Vector3, Vector3::ZERO, AM_DEFAULT);
  79. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR3, "Low Limit", GetLowLimit, SetLowLimit, Vector3, Vector3::ZERO, AM_DEFAULT);
  80. ATTRIBUTE(Constraint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  81. ATTRIBUTE(Constraint, VAR_BOOL, "Disable Collision", disableCollision_, false, AM_DEFAULT);
  82. }
  83. void Constraint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  84. {
  85. Component::OnSetAttribute(attr, src);
  86. // Change of any non-accessor attribute requires recreation of the constraint
  87. if (!attr.accessor_)
  88. {
  89. recreateConstraint_ = true;
  90. if (attr.offset_ == offsetof(Constraint, otherBodyPosition_) || attr.offset_ == offsetof(Constraint, otherBodyAxis_))
  91. otherBodyPositionValid_ = true;
  92. }
  93. }
  94. void Constraint::ApplyAttributes()
  95. {
  96. if (recreateConstraint_)
  97. {
  98. otherBody_.Reset();
  99. Scene* scene = GetScene();
  100. if (scene && otherBodyNodeID_)
  101. {
  102. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  103. if (otherNode)
  104. otherBody_ = otherNode->GetComponent<RigidBody>();
  105. }
  106. CreateConstraint();
  107. recreateConstraint_ = false;
  108. }
  109. }
  110. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  111. {
  112. if (otherBody_ && otherBody_->GetNode())
  113. dest.Push(otherBody_->GetNode());
  114. }
  115. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  116. {
  117. if (debug && physicsWorld_ && constraint_)
  118. {
  119. physicsWorld_->SetDebugRenderer(debug);
  120. physicsWorld_->SetDebugDepthTest(depthTest);
  121. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  122. physicsWorld_->SetDebugRenderer(0);
  123. }
  124. }
  125. void Constraint::SetConstraintType(ConstraintType type)
  126. {
  127. if (type != type_)
  128. {
  129. type_ = type;
  130. CreateConstraint();
  131. MarkNetworkUpdate();
  132. }
  133. }
  134. void Constraint::SetOtherBody(RigidBody* body)
  135. {
  136. if (otherBody_ != body)
  137. {
  138. otherBody_ = body;
  139. // Update the connected body attribute
  140. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  141. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  142. CreateConstraint();
  143. MarkNetworkUpdate();
  144. }
  145. }
  146. void Constraint::SetPosition(const Vector3& position)
  147. {
  148. if (position != position_)
  149. {
  150. position_ = position;
  151. /// \todo Optimize and do not recreate the constraint
  152. if (constraint_)
  153. CreateConstraint();
  154. MarkNetworkUpdate();
  155. }
  156. }
  157. void Constraint::SetAxis(const Vector3& axis)
  158. {
  159. if (axis != axis_)
  160. {
  161. axis_ = axis;
  162. /// \todo Optimize and do not recreate the constraint
  163. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  164. CreateConstraint();
  165. MarkNetworkUpdate();
  166. }
  167. }
  168. void Constraint::SetHighLimit(const Vector3& limit)
  169. {
  170. if (limit != highLimit_)
  171. {
  172. highLimit_ = limit;
  173. ApplyLimits();
  174. MarkNetworkUpdate();
  175. }
  176. }
  177. void Constraint::SetLowLimit(const Vector3& limit)
  178. {
  179. if (limit != lowLimit_)
  180. {
  181. lowLimit_ = limit;
  182. ApplyLimits();
  183. MarkNetworkUpdate();
  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. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(position_ * cachedWorldScale_),
  249. ToBtVector3(otherBodyPosition_));
  250. }
  251. break;
  252. case CONSTRAINT_HINGE:
  253. {
  254. Quaternion ownRotation(Vector3::FORWARD, axis_);
  255. Quaternion otherRotation(Vector3::FORWARD, otherBodyAxis_);
  256. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  257. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  258. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  259. }
  260. break;
  261. case CONSTRAINT_SLIDER:
  262. {
  263. Quaternion ownRotation(Vector3::RIGHT, axis_);
  264. Quaternion otherRotation(Vector3::RIGHT, otherBodyAxis_);
  265. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  266. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  267. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  268. }
  269. break;
  270. case CONSTRAINT_CONETWIST:
  271. {
  272. Quaternion ownRotation(Vector3::RIGHT, axis_);
  273. Quaternion otherRotation(Vector3::RIGHT, otherBodyAxis_);
  274. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  275. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  276. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  277. }
  278. break;
  279. }
  280. constraint_->setUserConstraintPtr(this);
  281. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  282. ApplyLimits();
  283. }
  284. void Constraint::ApplyLimits()
  285. {
  286. if (!constraint_)
  287. return;
  288. switch (constraint_->getConstraintType())
  289. {
  290. case HINGE_CONSTRAINT_TYPE:
  291. {
  292. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  293. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  294. }
  295. break;
  296. case SLIDER_CONSTRAINT_TYPE:
  297. {
  298. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  299. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  300. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  301. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  302. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  303. }
  304. break;
  305. case CONETWIST_CONSTRAINT_TYPE:
  306. {
  307. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  308. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.z_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  309. }
  310. break;
  311. }
  312. }