Joint.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Joint.h"
  27. #include "Log.h"
  28. #include "PhysicsWorld.h"
  29. #include "RigidBody.h"
  30. #include "Scene.h"
  31. #include "DebugNew.h"
  32. static const String typeNames[] =
  33. {
  34. "None",
  35. "Ball",
  36. "Hinge",
  37. ""
  38. };
  39. OBJECTTYPESTATIC(Joint);
  40. Joint::Joint(Context* context) :
  41. Component(context),
  42. type_(JOINT_NONE),
  43. position_(Vector3::ZERO),
  44. axis_(Vector3::ZERO),
  45. otherBodyNodeID_(0),
  46. recreateJoint_(false)
  47. {
  48. }
  49. Joint::~Joint()
  50. {
  51. Clear();
  52. if (physicsWorld_)
  53. physicsWorld_->RemoveJoint(this);
  54. }
  55. void Joint::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<Joint>();
  58. ENUM_ATTRIBUTE(Joint, "Joint Type", type_, typeNames, JOINT_NONE, AM_DEFAULT);
  59. REF_ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  60. REF_ACCESSOR_ATTRIBUTE(Joint, VAR_VECTOR3, "Axis", GetAxis, SetAxis, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  61. ATTRIBUTE(Joint, VAR_INT, "Other Body NodeID", otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  62. }
  63. void Joint::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  64. {
  65. Serializable::OnSetAttribute(attr, src);
  66. // Change of the joint type or connected body requires the joint to be recreated
  67. if (attr.offset_ == offsetof(Joint, type_) || attr.offset_ == offsetof(Joint, otherBodyNodeID_))
  68. recreateJoint_ = true;
  69. }
  70. void Joint::ApplyAttributes()
  71. {
  72. if (recreateJoint_)
  73. {
  74. otherBody_.Reset();
  75. Scene* scene = node_ ? node_->GetScene() : 0;
  76. if (scene && otherBodyNodeID_)
  77. {
  78. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  79. if (otherNode)
  80. otherBody_ = otherNode->GetComponent<RigidBody>();
  81. }
  82. SetJointType(type_);
  83. recreateJoint_ = false;
  84. }
  85. }
  86. void Joint::GetDependencyNodes(PODVector<Node*>& dest)
  87. {
  88. if (otherBody_ && otherBody_->GetNode())
  89. dest.Push(otherBody_->GetNode());
  90. }
  91. void Joint::Clear()
  92. {
  93. type_ = JOINT_NONE;
  94. }
  95. bool Joint::SetJointType(JointType type)
  96. {
  97. Clear();
  98. return true;
  99. }
  100. void Joint::SetOtherBody(RigidBody* body)
  101. {
  102. if (otherBody_ != body)
  103. {
  104. otherBody_ = body;
  105. // Update the connected body attribute
  106. Node* otherNode = otherBody_ ? otherBody_->GetNode() : 0;
  107. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  108. }
  109. }
  110. void Joint::SetPosition(const Vector3& position)
  111. {
  112. }
  113. void Joint::SetAxis(const Vector3& axis)
  114. {
  115. }
  116. const Vector3& Joint::GetPosition() const
  117. {
  118. return position_;
  119. }
  120. const Vector3& Joint::GetAxis() const
  121. {
  122. return axis_;
  123. }
  124. void Joint::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  125. {
  126. }
  127. void Joint::OnNodeSet(Node* node)
  128. {
  129. if (node)
  130. {
  131. Scene* scene = node->GetScene();
  132. if (scene)
  133. {
  134. physicsWorld_ = scene->GetComponent<PhysicsWorld>();
  135. if (physicsWorld_)
  136. physicsWorld_->AddJoint(this);
  137. }
  138. node->AddListener(this);
  139. // Set default position at the node
  140. position_ = node->GetWorldPosition();
  141. }
  142. }