Constraint.cpp 17 KB

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