Constraint.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. if (otherBody_)
  99. otherBody_->RemoveConstraint(this);
  100. otherBody_.Reset();
  101. Scene* scene = GetScene();
  102. if (scene && otherBodyNodeID_)
  103. {
  104. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  105. if (otherNode)
  106. otherBody_ = otherNode->GetComponent<RigidBody>();
  107. }
  108. CreateConstraint();
  109. recreateConstraint_ = false;
  110. }
  111. }
  112. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  113. {
  114. if (otherBody_ && otherBody_->GetNode())
  115. dest.Push(otherBody_->GetNode());
  116. }
  117. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  118. {
  119. if (debug && physicsWorld_ && constraint_)
  120. {
  121. physicsWorld_->SetDebugRenderer(debug);
  122. physicsWorld_->SetDebugDepthTest(depthTest);
  123. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  124. physicsWorld_->SetDebugRenderer(0);
  125. }
  126. }
  127. void Constraint::SetConstraintType(ConstraintType type)
  128. {
  129. if (type != type_)
  130. {
  131. type_ = type;
  132. CreateConstraint();
  133. MarkNetworkUpdate();
  134. }
  135. }
  136. void Constraint::SetOtherBody(RigidBody* body)
  137. {
  138. if (otherBody_ != body)
  139. {
  140. if (otherBody_)
  141. otherBody_->RemoveConstraint(this);
  142. otherBody_ = body;
  143. // Update the connected body attribute
  144. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  145. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  146. CreateConstraint();
  147. MarkNetworkUpdate();
  148. }
  149. }
  150. void Constraint::SetPosition(const Vector3& position)
  151. {
  152. if (position != position_)
  153. {
  154. position_ = position;
  155. /// \todo Optimize and do not recreate the constraint
  156. if (constraint_)
  157. CreateConstraint();
  158. MarkNetworkUpdate();
  159. }
  160. }
  161. void Constraint::SetAxis(const Vector3& axis)
  162. {
  163. if (axis != axis_)
  164. {
  165. axis_ = axis;
  166. /// \todo Optimize and do not recreate the constraint
  167. if (constraint_ && constraint_->getConstraintType() == HINGE_CONSTRAINT_TYPE)
  168. CreateConstraint();
  169. MarkNetworkUpdate();
  170. }
  171. }
  172. void Constraint::SetHighLimit(const Vector3& limit)
  173. {
  174. if (limit != highLimit_)
  175. {
  176. highLimit_ = limit;
  177. ApplyLimits();
  178. MarkNetworkUpdate();
  179. }
  180. }
  181. void Constraint::SetLowLimit(const Vector3& limit)
  182. {
  183. if (limit != lowLimit_)
  184. {
  185. lowLimit_ = limit;
  186. ApplyLimits();
  187. MarkNetworkUpdate();
  188. }
  189. }
  190. void Constraint::ReleaseConstraint()
  191. {
  192. if (constraint_)
  193. {
  194. if (ownBody_)
  195. ownBody_->RemoveConstraint(this);
  196. if (otherBody_)
  197. otherBody_->RemoveConstraint(this);
  198. if (physicsWorld_)
  199. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  200. delete constraint_;
  201. constraint_ = 0;
  202. }
  203. }
  204. void Constraint::OnNodeSet(Node* node)
  205. {
  206. if (node)
  207. {
  208. Scene* scene = GetScene();
  209. if (scene)
  210. {
  211. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  212. if (physicsWorld_)
  213. physicsWorld_->AddConstraint(this);
  214. else
  215. LOGERROR("No physics world component in scene, can not create constraint");
  216. }
  217. node->AddListener(this);
  218. // Try to create constraint immediately, may fail if the rigid body component does not exist yet
  219. CreateConstraint();
  220. }
  221. }
  222. void Constraint::OnMarkedDirty(Node* node)
  223. {
  224. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  225. CreateConstraint();
  226. }
  227. void Constraint::CreateConstraint()
  228. {
  229. PROFILE(CreateConstraint);
  230. cachedWorldScale_ = node_->GetWorldScale();
  231. ReleaseConstraint();
  232. ownBody_ = GetComponent<RigidBody>();
  233. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  234. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  235. if (!physicsWorld_ || !ownBody)
  236. return;
  237. if (!otherBody)
  238. otherBody = &btTypedConstraint::getFixedBody();
  239. btTransform ownInverse = ownBody->getWorldTransform().inverse();
  240. btTransform otherInverse = otherBody->getWorldTransform().inverse();
  241. // If the deserialized constraint other body position is valid, use it, but only this time
  242. if (otherBodyPositionValid_)
  243. otherBodyPositionValid_ = false;
  244. else
  245. {
  246. // Otherwise calculate it from own body's position
  247. otherBodyPosition_ = ToVector3(otherInverse * (ownBody->getWorldTransform() * ToBtVector3(position_ *
  248. cachedWorldScale_)));
  249. otherBodyAxis_ = ToVector3(otherInverse.getBasis() * (ownBody->getWorldTransform().getBasis() *
  250. ToBtVector3(axis_)));
  251. }
  252. switch (type_)
  253. {
  254. case CONSTRAINT_POINT:
  255. {
  256. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(position_ * cachedWorldScale_),
  257. ToBtVector3(otherBodyPosition_));
  258. }
  259. break;
  260. case CONSTRAINT_HINGE:
  261. {
  262. Quaternion ownRotation(Vector3::FORWARD, axis_);
  263. Quaternion otherRotation(Vector3::FORWARD, otherBodyAxis_);
  264. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  265. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  266. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  267. }
  268. break;
  269. case CONSTRAINT_SLIDER:
  270. {
  271. Quaternion ownRotation(Vector3::RIGHT, axis_);
  272. Quaternion otherRotation(Vector3::RIGHT, otherBodyAxis_);
  273. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  274. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  275. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  276. }
  277. break;
  278. case CONSTRAINT_CONETWIST:
  279. {
  280. Quaternion ownRotation(Vector3::RIGHT, axis_);
  281. Quaternion otherRotation(Vector3::RIGHT, otherBodyAxis_);
  282. btTransform ownFrame(ToBtQuaternion(ownRotation), ToBtVector3(position_ * cachedWorldScale_));
  283. btTransform otherFrame(ToBtQuaternion(otherRotation), ToBtVector3(otherBodyPosition_));
  284. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  285. }
  286. break;
  287. }
  288. constraint_->setUserConstraintPtr(this);
  289. ownBody_->AddConstraint(this);
  290. if (otherBody_)
  291. otherBody_->AddConstraint(this);
  292. ApplyLimits();
  293. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  294. }
  295. void Constraint::ApplyLimits()
  296. {
  297. if (!constraint_)
  298. return;
  299. switch (constraint_->getConstraintType())
  300. {
  301. case HINGE_CONSTRAINT_TYPE:
  302. {
  303. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  304. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  305. }
  306. break;
  307. case SLIDER_CONSTRAINT_TYPE:
  308. {
  309. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  310. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  311. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  312. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  313. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  314. }
  315. break;
  316. case CONETWIST_CONSTRAINT_TYPE:
  317. {
  318. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  319. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.z_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  320. }
  321. break;
  322. }
  323. }