2
0

Constraint.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. "Point",
  43. "Hinge",
  44. "Slider",
  45. "ConeTwist",
  46. 0
  47. };
  48. OBJECTTYPESTATIC(Constraint);
  49. Constraint::Constraint(Context* context) :
  50. Component(context),
  51. constraint_(0),
  52. constraintType_(CONSTRAINT_POINT),
  53. position_(Vector3::ZERO),
  54. rotation_(Quaternion::IDENTITY),
  55. otherPosition_(Vector3::ZERO),
  56. otherRotation_(Quaternion::IDENTITY),
  57. highLimit_(Vector2::ZERO),
  58. lowLimit_(Vector2::ZERO),
  59. otherBodyNodeID_(0),
  60. disableCollision_(false),
  61. recreateConstraint_(true),
  62. framesDirty_(false)
  63. {
  64. }
  65. Constraint::~Constraint()
  66. {
  67. ReleaseConstraint();
  68. if (physicsWorld_)
  69. physicsWorld_->RemoveConstraint(this);
  70. }
  71. void Constraint::RegisterObject(Context* context)
  72. {
  73. context->RegisterComponentFactory<Constraint>(PHYSICS_CATEGORY);
  74. ACCESSOR_ATTRIBUTE(Constraint, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  75. ENUM_ATTRIBUTE(Constraint, "Constraint Type", constraintType_, typeNames, CONSTRAINT_POINT, 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. }
  123. else if (framesDirty_)
  124. {
  125. ApplyFrames();
  126. framesDirty_ = false;
  127. }
  128. }
  129. void Constraint::OnSetEnabled()
  130. {
  131. if (constraint_)
  132. constraint_->setEnabled(IsEnabledEffective());
  133. }
  134. void Constraint::GetDependencyNodes(PODVector<Node*>& dest)
  135. {
  136. if (otherBody_ && otherBody_->GetNode())
  137. dest.Push(otherBody_->GetNode());
  138. }
  139. void Constraint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  140. {
  141. if (debug && physicsWorld_ && constraint_ && IsEnabledEffective())
  142. {
  143. physicsWorld_->SetDebugRenderer(debug);
  144. physicsWorld_->SetDebugDepthTest(depthTest);
  145. physicsWorld_->GetWorld()->debugDrawConstraint(constraint_);
  146. physicsWorld_->SetDebugRenderer(0);
  147. }
  148. }
  149. void Constraint::SetConstraintType(ConstraintType type)
  150. {
  151. if (type != constraintType_)
  152. {
  153. constraintType_ = type;
  154. CreateConstraint();
  155. MarkNetworkUpdate();
  156. }
  157. }
  158. void Constraint::SetOtherBody(RigidBody* body)
  159. {
  160. if (otherBody_ != body)
  161. {
  162. if (otherBody_)
  163. otherBody_->RemoveConstraint(this);
  164. otherBody_ = body;
  165. // Update the connected body attribute
  166. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  167. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  168. CreateConstraint();
  169. MarkNetworkUpdate();
  170. }
  171. }
  172. void Constraint::SetPosition(const Vector3& position)
  173. {
  174. if (position != position_)
  175. {
  176. position_ = position;
  177. ApplyFrames();
  178. MarkNetworkUpdate();
  179. }
  180. }
  181. void Constraint::SetRotation(const Quaternion& rotation)
  182. {
  183. if (rotation != rotation_)
  184. {
  185. rotation_ = rotation;
  186. ApplyFrames();
  187. MarkNetworkUpdate();
  188. }
  189. }
  190. void Constraint::SetAxis(const Vector3& axis)
  191. {
  192. switch (constraintType_)
  193. {
  194. case CONSTRAINT_POINT:
  195. case CONSTRAINT_HINGE:
  196. rotation_ = Quaternion(Vector3::FORWARD, axis);
  197. break;
  198. case CONSTRAINT_SLIDER:
  199. case CONSTRAINT_CONETWIST:
  200. rotation_ = Quaternion(Vector3::RIGHT, axis);
  201. break;
  202. default:
  203. break;
  204. }
  205. ApplyFrames();
  206. MarkNetworkUpdate();
  207. }
  208. void Constraint::SetOtherPosition(const Vector3& position)
  209. {
  210. if (position != otherPosition_)
  211. {
  212. otherPosition_ = position;
  213. ApplyFrames();
  214. MarkNetworkUpdate();
  215. }
  216. }
  217. void Constraint::SetOtherRotation(const Quaternion& rotation)
  218. {
  219. if (rotation != otherRotation_)
  220. {
  221. otherRotation_ = rotation;
  222. ApplyFrames();
  223. MarkNetworkUpdate();
  224. }
  225. }
  226. void Constraint::SetOtherAxis(const Vector3& axis)
  227. {
  228. switch (constraintType_)
  229. {
  230. case CONSTRAINT_POINT:
  231. case CONSTRAINT_HINGE:
  232. otherRotation_ = Quaternion(Vector3::FORWARD, axis);
  233. break;
  234. case CONSTRAINT_SLIDER:
  235. case CONSTRAINT_CONETWIST:
  236. otherRotation_ = Quaternion(Vector3::RIGHT, axis);
  237. break;
  238. default:
  239. break;
  240. }
  241. ApplyFrames();
  242. MarkNetworkUpdate();
  243. }
  244. void Constraint::SetWorldPosition(const Vector3& position)
  245. {
  246. if (constraint_)
  247. {
  248. btTransform ownBodyInverse = constraint_->getRigidBodyA().getWorldTransform().inverse();
  249. btTransform otherBodyInverse = constraint_->getRigidBodyB().getWorldTransform().inverse();
  250. btVector3 worldPos = ToBtVector3(position);
  251. position_ = ToVector3(ownBodyInverse * worldPos) / cachedWorldScale_;
  252. otherPosition_ = ToVector3(otherBodyInverse * worldPos);
  253. if (otherBody_)
  254. otherPosition_ /= otherBody_->GetNode()->GetWorldScale();
  255. ApplyFrames();
  256. MarkNetworkUpdate();
  257. }
  258. else
  259. LOGWARNING("Constraint not created, world position could not be stored");
  260. }
  261. void Constraint::SetHighLimit(const Vector2& limit)
  262. {
  263. if (limit != highLimit_)
  264. {
  265. highLimit_ = limit;
  266. ApplyLimits();
  267. MarkNetworkUpdate();
  268. }
  269. }
  270. void Constraint::SetLowLimit(const Vector2& limit)
  271. {
  272. if (limit != lowLimit_)
  273. {
  274. lowLimit_ = limit;
  275. ApplyLimits();
  276. MarkNetworkUpdate();
  277. }
  278. }
  279. void Constraint::SetDisableCollision(bool disable)
  280. {
  281. if (disable != disableCollision_)
  282. {
  283. disableCollision_ = disable;
  284. CreateConstraint();
  285. MarkNetworkUpdate();
  286. }
  287. }
  288. Vector3 Constraint::GetWorldPosition() const
  289. {
  290. if (constraint_)
  291. {
  292. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  293. return ToVector3(ownBody * ToBtVector3(position_ * cachedWorldScale_));
  294. }
  295. else
  296. return Vector3::ZERO;
  297. }
  298. void Constraint::ReleaseConstraint()
  299. {
  300. if (constraint_)
  301. {
  302. if (ownBody_)
  303. ownBody_->RemoveConstraint(this);
  304. if (otherBody_)
  305. otherBody_->RemoveConstraint(this);
  306. if (physicsWorld_)
  307. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  308. delete constraint_;
  309. constraint_ = 0;
  310. }
  311. }
  312. void Constraint::OnNodeSet(Node* node)
  313. {
  314. if (node)
  315. {
  316. Scene* scene = GetScene();
  317. if (scene)
  318. {
  319. if (scene == node)
  320. LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  321. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  322. if (physicsWorld_)
  323. physicsWorld_->AddConstraint(this);
  324. else
  325. LOGERROR("No physics world component in scene, can not create constraint");
  326. }
  327. node->AddListener(this);
  328. cachedWorldScale_ = node->GetWorldScale();
  329. }
  330. }
  331. void Constraint::OnMarkedDirty(Node* node)
  332. {
  333. /// \todo This does not catch the connected body node's scale changing
  334. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  335. ApplyFrames();
  336. }
  337. void Constraint::CreateConstraint()
  338. {
  339. PROFILE(CreateConstraint);
  340. cachedWorldScale_ = node_->GetWorldScale();
  341. ReleaseConstraint();
  342. ownBody_ = GetComponent<RigidBody>();
  343. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  344. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  345. if (!physicsWorld_ || !ownBody)
  346. return;
  347. if (!otherBody)
  348. otherBody = &btTypedConstraint::getFixedBody();
  349. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  350. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  351. otherPosition_;
  352. switch (constraintType_)
  353. {
  354. case CONSTRAINT_POINT:
  355. {
  356. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(ownBodyScaledPosition),
  357. ToBtVector3(otherBodyScaledPosition));
  358. }
  359. break;
  360. case CONSTRAINT_HINGE:
  361. {
  362. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  363. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  364. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  365. }
  366. break;
  367. case CONSTRAINT_SLIDER:
  368. {
  369. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  370. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  371. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  372. }
  373. break;
  374. case CONSTRAINT_CONETWIST:
  375. {
  376. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  377. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  378. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  379. }
  380. break;
  381. default:
  382. break;
  383. }
  384. if (constraint_)
  385. {
  386. constraint_->setUserConstraintPtr(this);
  387. constraint_->setEnabled(IsEnabledEffective());
  388. ownBody_->AddConstraint(this);
  389. if (otherBody_)
  390. otherBody_->AddConstraint(this);
  391. ApplyLimits();
  392. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  393. }
  394. recreateConstraint_ = false;
  395. framesDirty_ = false;
  396. }
  397. void Constraint::ApplyFrames()
  398. {
  399. if (!constraint_)
  400. return;
  401. if (node_)
  402. cachedWorldScale_ = node_->GetWorldScale();
  403. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  404. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  405. otherPosition_;
  406. switch (constraint_->getConstraintType())
  407. {
  408. case POINT2POINT_CONSTRAINT_TYPE:
  409. {
  410. btPoint2PointConstraint* pointConstraint = static_cast<btPoint2PointConstraint*>(constraint_);
  411. pointConstraint->setPivotA(ToBtVector3(ownBodyScaledPosition));
  412. pointConstraint->setPivotB(ToBtVector3(otherBodyScaledPosition));
  413. }
  414. break;
  415. case HINGE_CONSTRAINT_TYPE:
  416. {
  417. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  418. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  419. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  420. hingeConstraint->setFrames(ownFrame, otherFrame);
  421. }
  422. break;
  423. case SLIDER_CONSTRAINT_TYPE:
  424. {
  425. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  426. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  427. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  428. sliderConstraint->setFrames(ownFrame, otherFrame);
  429. }
  430. break;
  431. case CONETWIST_CONSTRAINT_TYPE:
  432. {
  433. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  434. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  435. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  436. coneTwistConstraint->setFrames(ownFrame, otherFrame);
  437. }
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. void Constraint::ApplyLimits()
  444. {
  445. if (!constraint_)
  446. return;
  447. switch (constraint_->getConstraintType())
  448. {
  449. case HINGE_CONSTRAINT_TYPE:
  450. {
  451. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  452. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  453. }
  454. break;
  455. case SLIDER_CONSTRAINT_TYPE:
  456. {
  457. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  458. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  459. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  460. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  461. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  462. }
  463. break;
  464. case CONETWIST_CONSTRAINT_TYPE:
  465. {
  466. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  467. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.y_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  468. }
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. }