Constraint.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "Constraint.h"
  26. #include "Log.h"
  27. #include "PhysicsUtils.h"
  28. #include "PhysicsWorld.h"
  29. #include "Profiler.h"
  30. #include "RigidBody.h"
  31. #include "Scene.h"
  32. #include <BulletDynamics/ConstraintSolver/btConeTwistConstraint.h>
  33. #include <BulletDynamics/ConstraintSolver/btHingeConstraint.h>
  34. #include <BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h>
  35. #include <BulletDynamics/ConstraintSolver/btSliderConstraint.h>
  36. #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
  37. #include "DebugNew.h"
  38. namespace Urho3D
  39. {
  40. static const char* typeNames[] =
  41. {
  42. "None",
  43. "Point",
  44. "Hinge",
  45. "Slider",
  46. "ConeTwist",
  47. 0
  48. };
  49. OBJECTTYPESTATIC(Constraint);
  50. Constraint::Constraint(Context* context) :
  51. Component(context),
  52. constraint_(0),
  53. constraintType_(CONSTRAINT_NONE),
  54. position_(Vector3::ZERO),
  55. rotation_(Quaternion::IDENTITY),
  56. otherPosition_(Vector3::ZERO),
  57. otherRotation_(Quaternion::IDENTITY),
  58. highLimit_(Vector2::ZERO),
  59. lowLimit_(Vector2::ZERO),
  60. otherBodyNodeID_(0),
  61. disableCollision_(false),
  62. recreateConstraint_(false),
  63. framesDirty_(false)
  64. {
  65. }
  66. Constraint::~Constraint()
  67. {
  68. ReleaseConstraint();
  69. if (physicsWorld_)
  70. physicsWorld_->RemoveConstraint(this);
  71. }
  72. void Constraint::RegisterObject(Context* context)
  73. {
  74. context->RegisterFactory<Constraint>();
  75. ENUM_ATTRIBUTE(Constraint, "Constraint Type", constraintType_, typeNames, CONSTRAINT_NONE, AM_DEFAULT);
  76. ATTRIBUTE(Constraint, VAR_VECTOR3, "Position", position_, Vector3::ZERO, AM_DEFAULT);
  77. ATTRIBUTE(Constraint, VAR_QUATERNION, "Rotation", rotation_, Quaternion::IDENTITY, AM_DEFAULT);
  78. ATTRIBUTE(Constraint, VAR_VECTOR3, "Other Body Position", otherPosition_, Vector3::ZERO, AM_DEFAULT);
  79. ATTRIBUTE(Constraint, VAR_QUATERNION, "Other Body Rotation", otherRotation_, Quaternion::IDENTITY, AM_DEFAULT);
  80. ATTRIBUTE(Constraint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  81. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR2, "High Limit", GetHighLimit, SetHighLimit, Vector2, Vector2::ZERO, AM_DEFAULT);
  82. REF_ACCESSOR_ATTRIBUTE(Constraint, VAR_VECTOR2, "Low Limit", GetLowLimit, SetLowLimit, Vector2, Vector2::ZERO, AM_DEFAULT);
  83. ATTRIBUTE(Constraint, VAR_BOOL, "Disable Collision", disableCollision_, false, AM_DEFAULT);
  84. }
  85. void Constraint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  86. {
  87. Component::OnSetAttribute(attr, src);
  88. if (!attr.accessor_)
  89. {
  90. // Convenience for editing static constraints: if not connected to another body, adjust world position to match local
  91. // (when deserializing, the proper other body position will be read after own position, so this calculation is safely
  92. // overridden and does not accumulate constraint error
  93. if (attr.offset_ == offsetof(Constraint, position_) && constraint_ && !otherBody_)
  94. {
  95. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  96. btVector3 worldPos = ownBody * ToBtVector3(position_ * cachedWorldScale_);
  97. otherPosition_ = ToVector3(worldPos);
  98. }
  99. // Certain attribute changes require recreation of the constraint
  100. if (attr.offset_ == offsetof(Constraint, constraintType_) || attr.offset_ == offsetof(Constraint, otherBodyNodeID_) ||
  101. attr.offset_ == offsetof(Constraint, disableCollision_))
  102. recreateConstraint_ = true;
  103. else
  104. framesDirty_ = true;
  105. }
  106. }
  107. void Constraint::ApplyAttributes()
  108. {
  109. if (recreateConstraint_)
  110. {
  111. if (otherBody_)
  112. otherBody_->RemoveConstraint(this);
  113. otherBody_.Reset();
  114. Scene* scene = GetScene();
  115. if (scene && otherBodyNodeID_)
  116. {
  117. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  118. if (otherNode)
  119. otherBody_ = otherNode->GetComponent<RigidBody>();
  120. }
  121. CreateConstraint();
  122. recreateConstraint_ = false;
  123. framesDirty_ = false;
  124. }
  125. else if (framesDirty_)
  126. {
  127. ApplyFrames();
  128. framesDirty_ = false;
  129. }
  130. }
  131. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  132. {
  133. if (otherBody_ && otherBody_->GetNode())
  134. dest.Push(otherBody_->GetNode());
  135. }
  136. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  137. {
  138. if (debug && physicsWorld_ && constraint_)
  139. {
  140. physicsWorld_->SetDebugRenderer(debug);
  141. physicsWorld_->SetDebugDepthTest(depthTest);
  142. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  143. physicsWorld_->SetDebugRenderer(0);
  144. }
  145. }
  146. void Constraint::Clear()
  147. {
  148. SetConstraintType(CONSTRAINT_NONE);
  149. }
  150. void Constraint::SetConstraintType(ConstraintType type)
  151. {
  152. if (type != constraintType_)
  153. {
  154. constraintType_ = type;
  155. CreateConstraint();
  156. MarkNetworkUpdate();
  157. }
  158. }
  159. void Constraint::SetOtherBody(RigidBody* body)
  160. {
  161. if (otherBody_ != body)
  162. {
  163. if (otherBody_)
  164. otherBody_->RemoveConstraint(this);
  165. otherBody_ = body;
  166. // Update the connected body attribute
  167. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  168. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  169. CreateConstraint();
  170. MarkNetworkUpdate();
  171. }
  172. }
  173. void Constraint::SetPosition(const Vector3& position)
  174. {
  175. if (position != position_)
  176. {
  177. position_ = position;
  178. ApplyFrames();
  179. MarkNetworkUpdate();
  180. }
  181. }
  182. void Constraint::SetRotation(const Quaternion& rotation)
  183. {
  184. if (rotation != rotation_)
  185. {
  186. rotation_ = rotation;
  187. ApplyFrames();
  188. MarkNetworkUpdate();
  189. }
  190. }
  191. void Constraint::SetAxis(const Vector3& axis)
  192. {
  193. switch (constraintType_)
  194. {
  195. case CONSTRAINT_POINT:
  196. case CONSTRAINT_HINGE:
  197. rotation_ = Quaternion(Vector3::FORWARD, axis);
  198. break;
  199. case CONSTRAINT_SLIDER:
  200. case CONSTRAINT_CONETWIST:
  201. rotation_ = Quaternion(Vector3::RIGHT, axis);
  202. break;
  203. default:
  204. break;
  205. }
  206. ApplyFrames();
  207. MarkNetworkUpdate();
  208. }
  209. void Constraint::SetOtherPosition(const Vector3& position)
  210. {
  211. if (position != otherPosition_)
  212. {
  213. otherPosition_ = position;
  214. ApplyFrames();
  215. MarkNetworkUpdate();
  216. }
  217. }
  218. void Constraint::SetOtherRotation(const Quaternion& rotation)
  219. {
  220. if (rotation != otherRotation_)
  221. {
  222. otherRotation_ = rotation;
  223. ApplyFrames();
  224. MarkNetworkUpdate();
  225. }
  226. }
  227. void Constraint::SetOtherAxis(const Vector3& axis)
  228. {
  229. switch (constraintType_)
  230. {
  231. case CONSTRAINT_POINT:
  232. case CONSTRAINT_HINGE:
  233. otherRotation_ = Quaternion(Vector3::FORWARD, axis);
  234. break;
  235. case CONSTRAINT_SLIDER:
  236. case CONSTRAINT_CONETWIST:
  237. otherRotation_ = Quaternion(Vector3::RIGHT, axis);
  238. break;
  239. default:
  240. break;
  241. }
  242. ApplyFrames();
  243. MarkNetworkUpdate();
  244. }
  245. void Constraint::SetWorldPosition(const Vector3& position)
  246. {
  247. if (constraint_)
  248. {
  249. btTransform ownBodyInverse = constraint_->getRigidBodyA().getWorldTransform().inverse();
  250. btTransform otherBodyInverse = constraint_->getRigidBodyB().getWorldTransform().inverse();
  251. btVector3 worldPos = ToBtVector3(position);
  252. position_ = ToVector3(ownBodyInverse * worldPos) / cachedWorldScale_;
  253. otherPosition_ = ToVector3(otherBodyInverse * worldPos);
  254. if (otherBody_)
  255. otherPosition_ /= otherBody_->GetNode()->GetWorldScale();
  256. ApplyFrames();
  257. MarkNetworkUpdate();
  258. }
  259. else
  260. LOGWARNING("Constraint not created, world position could not be stored");
  261. }
  262. void Constraint::SetHighLimit(const Vector2& limit)
  263. {
  264. if (limit != highLimit_)
  265. {
  266. highLimit_ = limit;
  267. ApplyLimits();
  268. MarkNetworkUpdate();
  269. }
  270. }
  271. void Constraint::SetLowLimit(const Vector2& limit)
  272. {
  273. if (limit != lowLimit_)
  274. {
  275. lowLimit_ = limit;
  276. ApplyLimits();
  277. MarkNetworkUpdate();
  278. }
  279. }
  280. void Constraint::SetDisableCollision(bool disable)
  281. {
  282. if (disable != disableCollision_)
  283. {
  284. disableCollision_ = disable;
  285. CreateConstraint();
  286. MarkNetworkUpdate();
  287. }
  288. }
  289. Vector3 Constraint::GetWorldPosition() const
  290. {
  291. if (constraint_)
  292. {
  293. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  294. return ToVector3(ownBody * ToBtVector3(position_ * cachedWorldScale_));
  295. }
  296. else
  297. return Vector3::ZERO;
  298. }
  299. void Constraint::ReleaseConstraint()
  300. {
  301. if (constraint_)
  302. {
  303. if (ownBody_)
  304. ownBody_->RemoveConstraint(this);
  305. if (otherBody_)
  306. otherBody_->RemoveConstraint(this);
  307. if (physicsWorld_)
  308. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  309. delete constraint_;
  310. constraint_ = 0;
  311. }
  312. }
  313. void Constraint::OnNodeSet(Node* node)
  314. {
  315. if (node)
  316. {
  317. Scene* scene = GetScene();
  318. if (scene)
  319. {
  320. if (scene == node)
  321. LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  322. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  323. if (physicsWorld_)
  324. physicsWorld_->AddConstraint(this);
  325. else
  326. LOGERROR("No physics world component in scene, can not create constraint");
  327. }
  328. node->AddListener(this);
  329. cachedWorldScale_ = node->GetWorldScale();
  330. }
  331. }
  332. void Constraint::OnMarkedDirty(Node* node)
  333. {
  334. /// \todo This does not catch the connected body node's scale changing
  335. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  336. ApplyFrames();
  337. }
  338. void Constraint::CreateConstraint()
  339. {
  340. PROFILE(CreateConstraint);
  341. cachedWorldScale_ = node_->GetWorldScale();
  342. ReleaseConstraint();
  343. ownBody_ = GetComponent<RigidBody>();
  344. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  345. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  346. if (!physicsWorld_ || !ownBody)
  347. return;
  348. if (!otherBody)
  349. otherBody = &btTypedConstraint::getFixedBody();
  350. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  351. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  352. otherPosition_;
  353. switch (constraintType_)
  354. {
  355. case CONSTRAINT_POINT:
  356. {
  357. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(ownBodyScaledPosition),
  358. ToBtVector3(otherBodyScaledPosition));
  359. }
  360. break;
  361. case CONSTRAINT_HINGE:
  362. {
  363. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  364. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  365. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  366. }
  367. break;
  368. case CONSTRAINT_SLIDER:
  369. {
  370. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  371. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  372. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  373. }
  374. break;
  375. case CONSTRAINT_CONETWIST:
  376. {
  377. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  378. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  379. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  380. }
  381. break;
  382. default:
  383. break;
  384. }
  385. if (constraint_)
  386. {
  387. constraint_->setUserConstraintPtr(this);
  388. ownBody_->AddConstraint(this);
  389. if (otherBody_)
  390. otherBody_->AddConstraint(this);
  391. ApplyLimits();
  392. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  393. }
  394. }
  395. void Constraint::ApplyFrames()
  396. {
  397. if (!constraint_)
  398. return;
  399. if (node_)
  400. cachedWorldScale_ = node_->GetWorldScale();
  401. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  402. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  403. otherPosition_;
  404. switch (constraint_->getConstraintType())
  405. {
  406. case POINT2POINT_CONSTRAINT_TYPE:
  407. {
  408. btPoint2PointConstraint* pointConstraint = static_cast<btPoint2PointConstraint*>(constraint_);
  409. pointConstraint->setPivotA(ToBtVector3(ownBodyScaledPosition));
  410. pointConstraint->setPivotB(ToBtVector3(otherBodyScaledPosition));
  411. }
  412. break;
  413. case HINGE_CONSTRAINT_TYPE:
  414. {
  415. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  416. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  417. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  418. hingeConstraint->setFrames(ownFrame, otherFrame);
  419. }
  420. break;
  421. case SLIDER_CONSTRAINT_TYPE:
  422. {
  423. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  424. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  425. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  426. sliderConstraint->setFrames(ownFrame, otherFrame);
  427. }
  428. break;
  429. case CONETWIST_CONSTRAINT_TYPE:
  430. {
  431. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  432. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  433. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  434. coneTwistConstraint->setFrames(ownFrame, otherFrame);
  435. }
  436. break;
  437. default:
  438. break;
  439. }
  440. }
  441. void Constraint::ApplyLimits()
  442. {
  443. if (!constraint_)
  444. return;
  445. switch (constraint_->getConstraintType())
  446. {
  447. case HINGE_CONSTRAINT_TYPE:
  448. {
  449. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  450. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  451. }
  452. break;
  453. case SLIDER_CONSTRAINT_TYPE:
  454. {
  455. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  456. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  457. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  458. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  459. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  460. }
  461. break;
  462. case CONETWIST_CONSTRAINT_TYPE:
  463. {
  464. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  465. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.y_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  466. }
  467. break;
  468. default:
  469. break;
  470. }
  471. }
  472. }