Constraint.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. constraintType_(CONSTRAINT_POINT),
  52. position_(Vector3::ZERO),
  53. rotation_(Quaternion::IDENTITY),
  54. otherPosition_(Vector3::ZERO),
  55. otherRotation_(Quaternion::IDENTITY),
  56. highLimit_(Vector2::ZERO),
  57. lowLimit_(Vector2::ZERO),
  58. otherBodyNodeID_(0),
  59. disableCollision_(false),
  60. recreateConstraint_(false),
  61. framesDirty_(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", constraintType_, typeNames, CONSTRAINT_POINT, AM_DEFAULT);
  74. ATTRIBUTE(Constraint, VAR_VECTOR3, "Position", position_, Vector3::ZERO, AM_DEFAULT);
  75. ATTRIBUTE(Constraint, VAR_QUATERNION, "Rotation", rotation_, Quaternion::IDENTITY, AM_DEFAULT);
  76. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Position", otherPosition_, Vector3::ZERO, AM_DEFAULT);
  77. ATTRIBUTE(Constraint, VAR_QUATERNION, "Other Body Rotation", otherRotation_, Quaternion::IDENTITY, AM_DEFAULT);
  78. ATTRIBUTE(Constraint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  79. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR2, "High Limit", GetHighLimit, SetHighLimit, Vector2, Vector2::ZERO, AM_DEFAULT);
  80. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR2, "Low Limit", GetLowLimit, SetLowLimit, Vector2, Vector2::ZERO, AM_DEFAULT);
  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. if (!attr.accessor_)
  87. {
  88. // Convenience for editing static constraints: if not connected to another body, adjust world position to match local
  89. // (when deserializing, the proper other body position will be read after own position, so this calculation is safely
  90. // overridden and does not accumulate constraint error
  91. if (attr.offset_ == offsetof(Constraint, position_) && constraint_ && !otherBody_)
  92. {
  93. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  94. btVector3 worldPos = ownBody * ToBtVector3(position_ * cachedWorldScale_);
  95. otherPosition_ = ToVector3(worldPos);
  96. }
  97. // Certain attribute changes require recreation of the constraint
  98. if (attr.offset_ == offsetof(Constraint, constraintType_) || attr.offset_ == offsetof(Constraint, otherBodyNodeID_) ||
  99. attr.offset_ == offsetof(Constraint, disableCollision_))
  100. recreateConstraint_ = true;
  101. else
  102. framesDirty_ = true;
  103. }
  104. }
  105. void Constraint::ApplyAttributes()
  106. {
  107. if (recreateConstraint_)
  108. {
  109. if (otherBody_)
  110. otherBody_->RemoveConstraint(this);
  111. otherBody_.Reset();
  112. Scene* scene = GetScene();
  113. if (scene && otherBodyNodeID_)
  114. {
  115. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  116. if (otherNode)
  117. otherBody_ = otherNode->GetComponent<RigidBody>();
  118. }
  119. CreateConstraint();
  120. recreateConstraint_ = false;
  121. framesDirty_ = false;
  122. }
  123. else if (framesDirty_)
  124. {
  125. ApplyFrames();
  126. framesDirty_ = false;
  127. }
  128. }
  129. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  130. {
  131. if (otherBody_ && otherBody_->GetNode())
  132. dest.Push(otherBody_->GetNode());
  133. }
  134. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  135. {
  136. if (debug && physicsWorld_ && constraint_)
  137. {
  138. physicsWorld_->SetDebugRenderer(debug);
  139. physicsWorld_->SetDebugDepthTest(depthTest);
  140. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  141. physicsWorld_->SetDebugRenderer(0);
  142. }
  143. }
  144. void Constraint::SetConstraintType(ConstraintType type)
  145. {
  146. if (type != constraintType_)
  147. {
  148. constraintType_ = type;
  149. CreateConstraint();
  150. MarkNetworkUpdate();
  151. }
  152. }
  153. void Constraint::SetOtherBody(RigidBody* body)
  154. {
  155. if (otherBody_ != body)
  156. {
  157. if (otherBody_)
  158. otherBody_->RemoveConstraint(this);
  159. otherBody_ = body;
  160. // Update the connected body attribute
  161. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  162. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  163. CreateConstraint();
  164. MarkNetworkUpdate();
  165. }
  166. }
  167. void Constraint::SetPosition(const Vector3& position)
  168. {
  169. if (position != position_)
  170. {
  171. position_ = position;
  172. ApplyFrames();
  173. MarkNetworkUpdate();
  174. }
  175. }
  176. void Constraint::SetRotation(const Quaternion& rotation)
  177. {
  178. if (rotation != rotation_)
  179. {
  180. rotation_ = rotation;
  181. ApplyFrames();
  182. MarkNetworkUpdate();
  183. }
  184. }
  185. void Constraint::SetAxis(const Vector3& axis)
  186. {
  187. switch (constraintType_)
  188. {
  189. case CONSTRAINT_POINT:
  190. case CONSTRAINT_HINGE:
  191. rotation_ = Quaternion(Vector3::FORWARD, axis);
  192. break;
  193. case CONSTRAINT_SLIDER:
  194. case CONSTRAINT_CONETWIST:
  195. rotation_ = Quaternion(Vector3::RIGHT, axis);
  196. break;
  197. }
  198. ApplyFrames();
  199. MarkNetworkUpdate();
  200. }
  201. void Constraint::SetOtherPosition(const Vector3& position)
  202. {
  203. if (position != otherPosition_)
  204. {
  205. otherPosition_ = position;
  206. ApplyFrames();
  207. MarkNetworkUpdate();
  208. }
  209. }
  210. void Constraint::SetOtherRotation(const Quaternion& rotation)
  211. {
  212. if (rotation != otherRotation_)
  213. {
  214. otherRotation_ = rotation;
  215. ApplyFrames();
  216. MarkNetworkUpdate();
  217. }
  218. }
  219. void Constraint::SetOtherAxis(const Vector3& axis)
  220. {
  221. switch (constraintType_)
  222. {
  223. case CONSTRAINT_POINT:
  224. case CONSTRAINT_HINGE:
  225. otherRotation_ = Quaternion(Vector3::FORWARD, axis);
  226. break;
  227. case CONSTRAINT_SLIDER:
  228. case CONSTRAINT_CONETWIST:
  229. otherRotation_ = Quaternion(Vector3::RIGHT, axis);
  230. break;
  231. }
  232. ApplyFrames();
  233. MarkNetworkUpdate();
  234. }
  235. void Constraint::SetWorldPosition(const Vector3& position)
  236. {
  237. if (constraint_)
  238. {
  239. btTransform ownBodyInverse = constraint_->getRigidBodyA().getWorldTransform().inverse();
  240. btTransform otherBodyInverse = constraint_->getRigidBodyB().getWorldTransform().inverse();
  241. btVector3 worldPos = ToBtVector3(position);
  242. position_ = ToVector3(ownBodyInverse * worldPos) / cachedWorldScale_;
  243. otherPosition_ = ToVector3(otherBodyInverse * worldPos);
  244. if (otherBody_)
  245. otherPosition_ /= otherBody_->GetNode()->GetWorldScale();
  246. ApplyFrames();
  247. MarkNetworkUpdate();
  248. }
  249. else
  250. LOGWARNING("Constraint not created, world position could not be stored");
  251. }
  252. void Constraint::SetHighLimit(const Vector2& limit)
  253. {
  254. if (limit != highLimit_)
  255. {
  256. highLimit_ = limit;
  257. ApplyLimits();
  258. MarkNetworkUpdate();
  259. }
  260. }
  261. void Constraint::SetLowLimit(const Vector2& limit)
  262. {
  263. if (limit != lowLimit_)
  264. {
  265. lowLimit_ = limit;
  266. ApplyLimits();
  267. MarkNetworkUpdate();
  268. }
  269. }
  270. void Constraint::SetDisableCollision(bool disable)
  271. {
  272. if (disable != disableCollision_)
  273. {
  274. disableCollision_ = disable;
  275. CreateConstraint();
  276. MarkNetworkUpdate();
  277. }
  278. }
  279. Vector3 Constraint::GetWorldPosition() const
  280. {
  281. if (constraint_)
  282. {
  283. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  284. return ToVector3(ownBody * ToBtVector3(position_ * cachedWorldScale_));
  285. }
  286. else
  287. return Vector3::ZERO;
  288. }
  289. void Constraint::ReleaseConstraint()
  290. {
  291. if (constraint_)
  292. {
  293. if (ownBody_)
  294. ownBody_->RemoveConstraint(this);
  295. if (otherBody_)
  296. otherBody_->RemoveConstraint(this);
  297. if (physicsWorld_)
  298. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  299. delete constraint_;
  300. constraint_ = 0;
  301. }
  302. }
  303. void Constraint::OnNodeSet(Node* node)
  304. {
  305. if (node)
  306. {
  307. Scene* scene = GetScene();
  308. if (scene)
  309. {
  310. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  311. if (physicsWorld_)
  312. physicsWorld_->AddConstraint(this);
  313. else
  314. LOGERROR("No physics world component in scene, can not create constraint");
  315. }
  316. node->AddListener(this);
  317. // Try to create constraint immediately, may fail if the rigid body component does not exist yet
  318. CreateConstraint();
  319. }
  320. }
  321. void Constraint::OnMarkedDirty(Node* node)
  322. {
  323. /// \todo This does not catch the connected body node's scale changing
  324. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  325. ApplyFrames();
  326. }
  327. void Constraint::CreateConstraint()
  328. {
  329. PROFILE(CreateConstraint);
  330. cachedWorldScale_ = node_->GetWorldScale();
  331. ReleaseConstraint();
  332. ownBody_ = GetComponent<RigidBody>();
  333. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  334. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  335. if (!physicsWorld_ || !ownBody)
  336. return;
  337. if (!otherBody)
  338. otherBody = &btTypedConstraint::getFixedBody();
  339. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  340. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  341. otherPosition_;
  342. switch (constraintType_)
  343. {
  344. case CONSTRAINT_POINT:
  345. {
  346. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(ownBodyScaledPosition),
  347. ToBtVector3(otherBodyScaledPosition));
  348. }
  349. break;
  350. case CONSTRAINT_HINGE:
  351. {
  352. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  353. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  354. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  355. }
  356. break;
  357. case CONSTRAINT_SLIDER:
  358. {
  359. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  360. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  361. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  362. }
  363. break;
  364. case CONSTRAINT_CONETWIST:
  365. {
  366. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  367. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  368. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  369. }
  370. break;
  371. }
  372. constraint_->setUserConstraintPtr(this);
  373. ownBody_->AddConstraint(this);
  374. if (otherBody_)
  375. otherBody_->AddConstraint(this);
  376. ApplyLimits();
  377. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  378. }
  379. void Constraint::ApplyFrames()
  380. {
  381. if (!constraint_)
  382. return;
  383. if (node_)
  384. cachedWorldScale_ = node_->GetWorldScale();
  385. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  386. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  387. otherPosition_;
  388. switch (constraint_->getConstraintType())
  389. {
  390. case POINT2POINT_CONSTRAINT_TYPE:
  391. {
  392. btPoint2PointConstraint* pointConstraint = static_cast<btPoint2PointConstraint*>(constraint_);
  393. pointConstraint->setPivotA(ToBtVector3(ownBodyScaledPosition));
  394. pointConstraint->setPivotB(ToBtVector3(otherBodyScaledPosition));
  395. }
  396. break;
  397. case HINGE_CONSTRAINT_TYPE:
  398. {
  399. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  400. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  401. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  402. hingeConstraint->setFrames(ownFrame, otherFrame);
  403. }
  404. break;
  405. case SLIDER_CONSTRAINT_TYPE:
  406. {
  407. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  408. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  409. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  410. sliderConstraint->setFrames(ownFrame, otherFrame);
  411. }
  412. break;
  413. case CONETWIST_CONSTRAINT_TYPE:
  414. {
  415. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  416. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  417. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  418. coneTwistConstraint->setFrames(ownFrame, otherFrame);
  419. }
  420. break;
  421. }
  422. }
  423. void Constraint::ApplyLimits()
  424. {
  425. if (!constraint_)
  426. return;
  427. switch (constraint_->getConstraintType())
  428. {
  429. case HINGE_CONSTRAINT_TYPE:
  430. {
  431. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  432. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  433. }
  434. break;
  435. case SLIDER_CONSTRAINT_TYPE:
  436. {
  437. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  438. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  439. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  440. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  441. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  442. }
  443. break;
  444. case CONETWIST_CONSTRAINT_TYPE:
  445. {
  446. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  447. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.y_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  448. }
  449. break;
  450. }
  451. }