Constraint.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  40. {
  41. static const char* typeNames[] =
  42. {
  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_POINT),
  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_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. 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::SetConstraintType(ConstraintType type)
  147. {
  148. if (type != constraintType_)
  149. {
  150. constraintType_ = type;
  151. CreateConstraint();
  152. MarkNetworkUpdate();
  153. }
  154. }
  155. void Constraint::SetOtherBody(RigidBody* body)
  156. {
  157. if (otherBody_ != body)
  158. {
  159. if (otherBody_)
  160. otherBody_->RemoveConstraint(this);
  161. otherBody_ = body;
  162. // Update the connected body attribute
  163. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  164. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  165. CreateConstraint();
  166. MarkNetworkUpdate();
  167. }
  168. }
  169. void Constraint::SetPosition(const Vector3& position)
  170. {
  171. if (position != position_)
  172. {
  173. position_ = position;
  174. ApplyFrames();
  175. MarkNetworkUpdate();
  176. }
  177. }
  178. void Constraint::SetRotation(const Quaternion& rotation)
  179. {
  180. if (rotation != rotation_)
  181. {
  182. rotation_ = rotation;
  183. ApplyFrames();
  184. MarkNetworkUpdate();
  185. }
  186. }
  187. void Constraint::SetAxis(const Vector3& axis)
  188. {
  189. switch (constraintType_)
  190. {
  191. case CONSTRAINT_POINT:
  192. case CONSTRAINT_HINGE:
  193. rotation_ = Quaternion(Vector3::FORWARD, axis);
  194. break;
  195. case CONSTRAINT_SLIDER:
  196. case CONSTRAINT_CONETWIST:
  197. rotation_ = Quaternion(Vector3::RIGHT, axis);
  198. break;
  199. }
  200. ApplyFrames();
  201. MarkNetworkUpdate();
  202. }
  203. void Constraint::SetOtherPosition(const Vector3& position)
  204. {
  205. if (position != otherPosition_)
  206. {
  207. otherPosition_ = position;
  208. ApplyFrames();
  209. MarkNetworkUpdate();
  210. }
  211. }
  212. void Constraint::SetOtherRotation(const Quaternion& rotation)
  213. {
  214. if (rotation != otherRotation_)
  215. {
  216. otherRotation_ = rotation;
  217. ApplyFrames();
  218. MarkNetworkUpdate();
  219. }
  220. }
  221. void Constraint::SetOtherAxis(const Vector3& axis)
  222. {
  223. switch (constraintType_)
  224. {
  225. case CONSTRAINT_POINT:
  226. case CONSTRAINT_HINGE:
  227. otherRotation_ = Quaternion(Vector3::FORWARD, axis);
  228. break;
  229. case CONSTRAINT_SLIDER:
  230. case CONSTRAINT_CONETWIST:
  231. otherRotation_ = Quaternion(Vector3::RIGHT, axis);
  232. break;
  233. }
  234. ApplyFrames();
  235. MarkNetworkUpdate();
  236. }
  237. void Constraint::SetWorldPosition(const Vector3& position)
  238. {
  239. if (constraint_)
  240. {
  241. btTransform ownBodyInverse = constraint_->getRigidBodyA().getWorldTransform().inverse();
  242. btTransform otherBodyInverse = constraint_->getRigidBodyB().getWorldTransform().inverse();
  243. btVector3 worldPos = ToBtVector3(position);
  244. position_ = ToVector3(ownBodyInverse * worldPos) / cachedWorldScale_;
  245. otherPosition_ = ToVector3(otherBodyInverse * worldPos);
  246. if (otherBody_)
  247. otherPosition_ /= otherBody_->GetNode()->GetWorldScale();
  248. ApplyFrames();
  249. MarkNetworkUpdate();
  250. }
  251. else
  252. LOGWARNING("Constraint not created, world position could not be stored");
  253. }
  254. void Constraint::SetHighLimit(const Vector2& limit)
  255. {
  256. if (limit != highLimit_)
  257. {
  258. highLimit_ = limit;
  259. ApplyLimits();
  260. MarkNetworkUpdate();
  261. }
  262. }
  263. void Constraint::SetLowLimit(const Vector2& limit)
  264. {
  265. if (limit != lowLimit_)
  266. {
  267. lowLimit_ = limit;
  268. ApplyLimits();
  269. MarkNetworkUpdate();
  270. }
  271. }
  272. void Constraint::SetDisableCollision(bool disable)
  273. {
  274. if (disable != disableCollision_)
  275. {
  276. disableCollision_ = disable;
  277. CreateConstraint();
  278. MarkNetworkUpdate();
  279. }
  280. }
  281. Vector3 Constraint::GetWorldPosition() const
  282. {
  283. if (constraint_)
  284. {
  285. btTransform ownBody = constraint_->getRigidBodyA().getWorldTransform();
  286. return ToVector3(ownBody * ToBtVector3(position_ * cachedWorldScale_));
  287. }
  288. else
  289. return Vector3::ZERO;
  290. }
  291. void Constraint::ReleaseConstraint()
  292. {
  293. if (constraint_)
  294. {
  295. if (ownBody_)
  296. ownBody_->RemoveConstraint(this);
  297. if (otherBody_)
  298. otherBody_->RemoveConstraint(this);
  299. if (physicsWorld_)
  300. physicsWorld_->GetWorld()->removeConstraint(constraint_);
  301. delete constraint_;
  302. constraint_ = 0;
  303. }
  304. }
  305. void Constraint::OnNodeSet(Node* node)
  306. {
  307. if (node)
  308. {
  309. Scene* scene = GetScene();
  310. if (scene)
  311. {
  312. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  313. if (physicsWorld_)
  314. physicsWorld_->AddConstraint(this);
  315. else
  316. LOGERROR("No physics world component in scene, can not create constraint");
  317. }
  318. node->AddListener(this);
  319. // Try to create constraint immediately, may fail if the rigid body component does not exist yet
  320. CreateConstraint();
  321. }
  322. }
  323. void Constraint::OnMarkedDirty(Node* node)
  324. {
  325. /// \todo This does not catch the connected body node's scale changing
  326. if (!node->GetWorldScale().Equals(cachedWorldScale_))
  327. ApplyFrames();
  328. }
  329. void Constraint::CreateConstraint()
  330. {
  331. PROFILE(CreateConstraint);
  332. cachedWorldScale_ = node_->GetWorldScale();
  333. ReleaseConstraint();
  334. ownBody_ = GetComponent<RigidBody>();
  335. btRigidBody* ownBody = ownBody_ ? ownBody_->GetBody() : 0;
  336. btRigidBody* otherBody = otherBody_ ? otherBody_->GetBody() : 0;
  337. if (!physicsWorld_ || !ownBody)
  338. return;
  339. if (!otherBody)
  340. otherBody = &btTypedConstraint::getFixedBody();
  341. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  342. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  343. otherPosition_;
  344. switch (constraintType_)
  345. {
  346. case CONSTRAINT_POINT:
  347. {
  348. constraint_ = new btPoint2PointConstraint(*ownBody, *otherBody, ToBtVector3(ownBodyScaledPosition),
  349. ToBtVector3(otherBodyScaledPosition));
  350. }
  351. break;
  352. case CONSTRAINT_HINGE:
  353. {
  354. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  355. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  356. constraint_ = new btHingeConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  357. }
  358. break;
  359. case CONSTRAINT_SLIDER:
  360. {
  361. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  362. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  363. constraint_ = new btSliderConstraint(*ownBody, *otherBody, ownFrame, otherFrame, false);
  364. }
  365. break;
  366. case CONSTRAINT_CONETWIST:
  367. {
  368. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  369. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  370. constraint_ = new btConeTwistConstraint(*ownBody, *otherBody, ownFrame, otherFrame);
  371. }
  372. break;
  373. }
  374. constraint_->setUserConstraintPtr(this);
  375. ownBody_->AddConstraint(this);
  376. if (otherBody_)
  377. otherBody_->AddConstraint(this);
  378. ApplyLimits();
  379. physicsWorld_->GetWorld()->addConstraint(constraint_, disableCollision_);
  380. }
  381. void Constraint::ApplyFrames()
  382. {
  383. if (!constraint_)
  384. return;
  385. if (node_)
  386. cachedWorldScale_ = node_->GetWorldScale();
  387. Vector3 ownBodyScaledPosition = position_ * cachedWorldScale_;
  388. Vector3 otherBodyScaledPosition = otherBody_ ? otherPosition_ * otherBody_->GetNode()->GetWorldScale() :
  389. otherPosition_;
  390. switch (constraint_->getConstraintType())
  391. {
  392. case POINT2POINT_CONSTRAINT_TYPE:
  393. {
  394. btPoint2PointConstraint* pointConstraint = static_cast<btPoint2PointConstraint*>(constraint_);
  395. pointConstraint->setPivotA(ToBtVector3(ownBodyScaledPosition));
  396. pointConstraint->setPivotB(ToBtVector3(otherBodyScaledPosition));
  397. }
  398. break;
  399. case HINGE_CONSTRAINT_TYPE:
  400. {
  401. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  402. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  403. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  404. hingeConstraint->setFrames(ownFrame, otherFrame);
  405. }
  406. break;
  407. case SLIDER_CONSTRAINT_TYPE:
  408. {
  409. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  410. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  411. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  412. sliderConstraint->setFrames(ownFrame, otherFrame);
  413. }
  414. break;
  415. case CONETWIST_CONSTRAINT_TYPE:
  416. {
  417. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  418. btTransform ownFrame(ToBtQuaternion(rotation_), ToBtVector3(ownBodyScaledPosition));
  419. btTransform otherFrame(ToBtQuaternion(otherRotation_), ToBtVector3(otherBodyScaledPosition));
  420. coneTwistConstraint->setFrames(ownFrame, otherFrame);
  421. }
  422. break;
  423. }
  424. }
  425. void Constraint::ApplyLimits()
  426. {
  427. if (!constraint_)
  428. return;
  429. switch (constraint_->getConstraintType())
  430. {
  431. case HINGE_CONSTRAINT_TYPE:
  432. {
  433. btHingeConstraint* hingeConstraint = static_cast<btHingeConstraint*>(constraint_);
  434. hingeConstraint->setLimit(lowLimit_.x_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  435. }
  436. break;
  437. case SLIDER_CONSTRAINT_TYPE:
  438. {
  439. btSliderConstraint* sliderConstraint = static_cast<btSliderConstraint*>(constraint_);
  440. sliderConstraint->setUpperLinLimit(highLimit_.x_);
  441. sliderConstraint->setUpperAngLimit(highLimit_.y_ * M_DEGTORAD);
  442. sliderConstraint->setLowerLinLimit(lowLimit_.x_);
  443. sliderConstraint->setLowerAngLimit(lowLimit_.y_ * M_DEGTORAD);
  444. }
  445. break;
  446. case CONETWIST_CONSTRAINT_TYPE:
  447. {
  448. btConeTwistConstraint* coneTwistConstraint = static_cast<btConeTwistConstraint*>(constraint_);
  449. coneTwistConstraint->setLimit(highLimit_.y_ * M_DEGTORAD, highLimit_.y_ * M_DEGTORAD, highLimit_.x_ * M_DEGTORAD);
  450. }
  451. break;
  452. }
  453. }
  454. }