Joint.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #pragma once
  24. #include "Component.h"
  25. #include "PhysicsDefs.h"
  26. #include "Vector3.h"
  27. /// Supported joint types.
  28. enum JointType
  29. {
  30. JOINT_NONE = 0,
  31. JOINT_BALL,
  32. JOINT_HINGE
  33. };
  34. class PhysicsWorld;
  35. class RigidBody;
  36. /// Physics joint component. Connects two rigid bodies together, or one rigid body to a static point.
  37. class Joint : public Component
  38. {
  39. OBJECT(Joint);
  40. public:
  41. /// Construct.
  42. Joint(Context* context);
  43. /// Destruct.
  44. ~Joint();
  45. /// Register object factory.
  46. static void RegisterObject(Context* context);
  47. /// Handle attribute write access.
  48. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  49. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  50. virtual void ApplyAttributes();
  51. /// Return the depended on nodes to order network updates.
  52. virtual void GetDependencyNodes(PODVector<Node*>& dest);
  53. /// Remove the joint.
  54. void Clear();
  55. /// %Set a ball joint.
  56. bool SetBall(const Vector3& position, RigidBody* bodyA, RigidBody* bodyB = 0);
  57. /// %Set a hinge joint.
  58. bool SetHinge(const Vector3& position, const Vector3& axis, RigidBody* bodyA, RigidBody* bodyB = 0);
  59. /// %Set joint world position.
  60. void SetPosition(Vector3 position);
  61. /// %Set joint world axis if applicable.
  62. void SetAxis(Vector3 axis);
  63. /// Return physics world.
  64. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  65. /// Return the first rigid body.
  66. RigidBody* GetBodyA() const { return bodyA_; }
  67. /// Return the second rigid body.
  68. RigidBody* GetBodyB() const { return bodyB_; }
  69. /// Return joint type.
  70. JointType GetJointType() const { return type_; }
  71. /// Return the ODE joint ID.
  72. dJointID GetJoint() const { return joint_; }
  73. /// Return joint world position.
  74. Vector3 GetPosition() const;
  75. /// Return joint world axis.
  76. Vector3 GetAxis() const;
  77. /// %Set body A attribute.
  78. void SetBodyAAttr(int value);
  79. /// %Set body B attribute.
  80. void SetBodyBAttr(int value);
  81. /// Return body A attribute.
  82. int GetBodyAAttr() const;
  83. /// Return body B attribute.
  84. int GetBodyBAttr() const;
  85. protected:
  86. /// Handle node being assigned.
  87. virtual void OnNodeSet(Node* node);
  88. private:
  89. /// Physics world.
  90. WeakPtr<PhysicsWorld> physicsWorld_;
  91. /// First rigid body.
  92. WeakPtr<RigidBody> bodyA_;
  93. /// Second rigid body.
  94. WeakPtr<RigidBody> bodyB_;
  95. /// Joint type.
  96. JointType type_;
  97. /// ODE joint ID.
  98. dJointID joint_;
  99. /// Joint position for creation during post-load.
  100. Vector3 position_;
  101. /// Joint axis for creation during post-load.
  102. Vector3 axis_;
  103. /// Recreate joint flag.
  104. bool recreateJoint_;
  105. };