Joint.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #pragma once
  24. #include "Component.h"
  25. #include "Vector3.h"
  26. /// Supported joint types.
  27. enum JointType
  28. {
  29. JOINT_NONE = 0,
  30. JOINT_BALL,
  31. JOINT_HINGE
  32. };
  33. class PhysicsWorld;
  34. class RigidBody;
  35. /// Physics joint component. Connects two rigid bodies together, or one rigid body to a static point.
  36. class Joint : public Component
  37. {
  38. OBJECT(Joint);
  39. public:
  40. /// Construct.
  41. Joint(Context* context);
  42. /// Destruct.
  43. ~Joint();
  44. /// Register object factory.
  45. static void RegisterObject(Context* context);
  46. /// Handle attribute write access.
  47. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  48. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  49. virtual void ApplyAttributes();
  50. /// Return the depended on nodes to order network updates.
  51. virtual void GetDependencyNodes(PODVector<Node*>& dest);
  52. /// Remove the joint.
  53. void Clear();
  54. /// %Set joint type and recreate the joint. Return true if successful.
  55. bool SetJointType(JointType type);
  56. /// %Set other body to connect to.
  57. void SetOtherBody(RigidBody* body);
  58. /// %Set joint world-space position.
  59. void SetPosition(const Vector3& position);
  60. /// %Set joint world-space axis.
  61. void SetAxis(const Vector3& axis);
  62. /// Return physics world.
  63. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  64. /// Return joint type.
  65. JointType GetJointType() const { return type_; }
  66. /// Return rigid body in own scene node.
  67. RigidBody* GetOwnBody() const { return ownBody_; }
  68. /// Return the other rigid body. May be null if connected to the static world.
  69. RigidBody* GetOtherBody() const { return otherBody_; }
  70. /// Return joint world-space position.
  71. const Vector3& GetPosition() const;
  72. /// Return joint world-space axis.
  73. const Vector3& GetAxis() const;
  74. /// Add debug geometry to the debug renderer.
  75. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  76. protected:
  77. /// Handle node being assigned.
  78. virtual void OnNodeSet(Node* node);
  79. private:
  80. /// Physics world.
  81. WeakPtr<PhysicsWorld> physicsWorld_;
  82. /// Own rigid body.
  83. WeakPtr<RigidBody> ownBody_;
  84. /// Other rigid body.
  85. WeakPtr<RigidBody> otherBody_;
  86. /// Joint type.
  87. JointType type_;
  88. /// Joint world-space position.
  89. mutable Vector3 position_;
  90. /// Joint world-space axis.
  91. mutable Vector3 axis_;
  92. /// Other body node ID for pending joint recreation.
  93. int otherBodyNodeID_;
  94. /// Recreate joint flag.
  95. bool recreateJoint_;
  96. };