Constraint.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. }
  204. ApplyFrames();
  205. MarkNetworkUpdate();
  206. }
  207. void Constraint::SetOtherPosition(const Vector3& position)
  208. {
  209. if (position != otherPosition_)
  210. {
  211. otherPosition_ = position;
  212. ApplyFrames();
  213. MarkNetworkUpdate();
  214. }
  215. }
  216. void Constraint::SetOtherRotation(const Quaternion& rotation)
  217. {
  218. if (rotation != otherRotation_)
  219. {
  220. otherRotation_ = rotation;
  221. ApplyFrames();
  222. MarkNetworkUpdate();
  223. }
  224. }
  225. void Constraint::SetOtherAxis(const Vector3& axis)
  226. {
  227. switch (constraintType_)
  228. {
  229. case CONSTRAINT_POINT:
  230. case CONSTRAINT_HINGE:
  231. otherRotation_ = Quaternion(Vector3::FORWARD, axis);
  232. break;
  233. case CONSTRAINT_SLIDER:
  234. case CONSTRAINT_CONETWIST:
  235. otherRotation_ = Quaternion(Vector3::RIGHT, axis);
  236. break;
  237. }
  238. ApplyFrames();
  239. MarkNetworkUpdate();
  240. }
  241. void Constraint::SetWorldPosition(const Vector3& position)
  242. {
  243. if (constraint_)
  244. {
  245. btTransform ownBodyInverse = constraint_->getRigidBodyA().getWorldTransform().inverse();
  246. btTransform otherBodyInverse = constraint_->getRigidBodyB().getWorldTransform().inverse();
  247. btVector3 worldPos = ToBtVector3(position);
  248. position_ = ToVector3(ownBodyInverse * worldPos) / cachedWorldScale_;
  249. otherPosition_ = ToVector3(otherBodyInverse * worldPos);
  250. if (otherBody_)
  251. otherPosition_ /= otherBody_->GetNode()->GetWorldScale();
  252. ApplyFrames();
  253. MarkNetworkUpdate();
  254. }
  255. else
  256. LOGWARNING("Constraint not created, world position could not be stored");
  257. }
  258. void Constraint::SetHighLimit(const Vector2& limit)
  259. {
  260. if (limit != highLimit_)
  261. {
  262. highLimit_ = limit;
  263. ApplyLimits();
  264. MarkNetworkUpdate();
  265. }
  266. }
  267. void Constraint::SetLowLimit(const Vector2& limit)
  268. {
  269. if (limit != lowLimit_)
  270. {
  271. lowLimit_ = limit;
  272. ApplyLimits();
  273. MarkNetworkUpdate();
  274. }
  275. }
  276. void Constraint::SetDisableCollision(bool disable)
  277. {
  278. if (disable != disableCollision_)
  279. {
  280. disableCollision_ = disable;
  281. CreateConstraint();
  282. MarkNetworkUpdate();
  283. }
  284. }
  285. Vector3 Constraint::GetWorldPosition() const
  286. {
  287. if (constraint_)
  288. {
  289. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  290. return ToVector3(ownBody * ToBtVector3(position_ * cachedWorldScale_));
  291. }
  292. else
  293. return Vector3::ZERO;
  294. }
  295. void Constraint::ReleaseConstraint()
  296. {
  297. if (constraint_)
  298. {
  299. if (ownBody_)
  300. ownBody_->RemoveConstraint(this);
  301. if (otherBody_)
  302. otherBody_->RemoveConstraint(this);
  303. if (physicsWorld_)
  304. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  305. delete constraint_;
  306. constraint_ = 0;
  307. }
  308. }
  309. void Constraint::OnNodeSet(Node* node)
  310. {
  311. if (node)
  312. {
  313. Scene* scene = GetScene();
  314. if (scene)
  315. {
  316. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  317. if (physicsWorld_)
  318. physicsWorld_->AddConstraint(this);
  319. else
  320. LOGERROR("No physics world component in scene, can not create constraint");
  321. }
  322. node->AddListener(this);
  323. cachedWorldScale_ = node->GetWorldScale();
  324. }
  325. }
  326. void Constraint::OnMarkedDirty(Node* node)
  327. {
  328. /// \todo This does not catch the connected body node's scale changing
  329. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  330. ApplyFrames();
  331. }
  332. void Constraint::CreateConstraint()
  333. {
  334. PROFILE(CreateConstraint);
  335. cachedWorldScale_ = node_->GetWorldScale();
  336. ReleaseConstraint();
  337. ownBody_ = GetComponent<RigidBody>();
  338. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  339. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  340. if (!physicsWorld_ || !ownBody)
  341. return;
  342. if (!otherBody)
  343. otherBody = &btTypedConstraint::getFixedBody();
  344. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  345. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  346. otherPosition_;
  347. switch (constraintType_)
  348. {
  349. case CONSTRAINT_POINT:
  350. {
  351. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(ownBodyScaledPosition),
  352. ToBtVector3(otherBodyScaledPosition));
  353. }
  354. break;
  355. case CONSTRAINT_HINGE:
  356. {
  357. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  358. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  359. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  360. }
  361. break;
  362. case CONSTRAINT_SLIDER:
  363. {
  364. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  365. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  366. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  367. }
  368. break;
  369. case CONSTRAINT_CONETWIST:
  370. {
  371. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  372. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  373. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  374. }
  375. break;
  376. default:
  377. break;
  378. }
  379. if (constraint_)
  380. {
  381. constraint_->setUserConstraintPtr(this);
  382. ownBody_->AddConstraint(this);
  383. if (otherBody_)
  384. otherBody_->AddConstraint(this);
  385. ApplyLimits();
  386. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  387. }
  388. }
  389. void Constraint::ApplyFrames()
  390. {
  391. if (!constraint_)
  392. return;
  393. if (node_)
  394. cachedWorldScale_ = node_->GetWorldScale();
  395. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  396. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  397. otherPosition_;
  398. switch (constraint_->getConstraintType())
  399. {
  400. case POINT2POINT_CONSTRAINT_TYPE:
  401. {
  402. btPoint2PointConstraint* pointConstraint = static_cast<btPoint2PointConstraint*>(constraint_);
  403. pointConstraint->setPivotA(ToBtVector3(ownBodyScaledPosition));
  404. pointConstraint->setPivotB(ToBtVector3(otherBodyScaledPosition));
  405. }
  406. break;
  407. case HINGE_CONSTRAINT_TYPE:
  408. {
  409. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  410. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  411. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  412. hingeConstraint->setFrames(ownFrame, otherFrame);
  413. }
  414. break;
  415. case SLIDER_CONSTRAINT_TYPE:
  416. {
  417. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  418. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  419. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  420. sliderConstraint->setFrames(ownFrame, otherFrame);
  421. }
  422. break;
  423. case CONETWIST_CONSTRAINT_TYPE:
  424. {
  425. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  426. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  427. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  428. coneTwistConstraint->setFrames(ownFrame, otherFrame);
  429. }
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. void Constraint::ApplyLimits()
  436. {
  437. if (!constraint_)
  438. return;
  439. switch (constraint_->getConstraintType())
  440. {
  441. case HINGE_CONSTRAINT_TYPE:
  442. {
  443. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  444. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  445. }
  446. break;
  447. case SLIDER_CONSTRAINT_TYPE:
  448. {
  449. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  450. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  451. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  452. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  453. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  454. }
  455. break;
  456. case CONETWIST_CONSTRAINT_TYPE:
  457. {
  458. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  459. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.y_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. }