Constraint2D.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Scene/Component.h"
  24. #include <Box2D/Box2D.h>
  25. namespace Atomic
  26. {
  27. class RigidBody2D;
  28. class PhysicsWorld2D;
  29. /// 2D physics constraint component.
  30. class ATOMIC_API Constraint2D : public Component
  31. {
  32. ATOMIC_OBJECT(Constraint2D, Component);
  33. public:
  34. /// Construct.
  35. Constraint2D(Context* context);
  36. /// Destruct.
  37. virtual ~Constraint2D();
  38. /// Register object factory.
  39. static void RegisterObject(Context* context);
  40. /// Handle attribute write access.
  41. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  42. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  43. virtual void ApplyAttributes();
  44. /// Handle enabled/disabled state change.
  45. virtual void OnSetEnabled();
  46. /// Create joint.
  47. void CreateJoint();
  48. /// Release joint.
  49. void ReleaseJoint();
  50. /// Set other rigid body.
  51. void SetOtherBody(RigidBody2D* body);
  52. /// Set collide connected.
  53. void SetCollideConnected(bool collideConnected);
  54. /// Set attached constriant (for gear).
  55. void SetAttachedConstraint(Constraint2D* constraint);
  56. /// Return owner body.
  57. RigidBody2D* GetOwnerBody() const { return ownerBody_; }
  58. /// Return other body.
  59. RigidBody2D* GetOtherBody() const { return otherBody_; }
  60. /// Return collide connected.
  61. bool GetCollideConnected() const { return collideConnected_; }
  62. /// Return attached constraint (for gear).
  63. Constraint2D* GetAttachedConstraint() const { return attachedConstraint_; }
  64. /// Return Box2D joint.
  65. b2Joint* GetJoint() const { return joint_; }
  66. protected:
  67. /// Handle node being assigned.
  68. virtual void OnNodeSet(Node* node);
  69. /// Handle scene being assigned.
  70. virtual void OnSceneSet(Scene* scene);
  71. /// Return joint def.
  72. virtual b2JointDef* GetJointDef() { return 0; };
  73. /// Recreate joint.
  74. void RecreateJoint();
  75. /// Initialize joint def.
  76. void InitializeJointDef(b2JointDef* jointDef);
  77. /// Physics world.
  78. WeakPtr<PhysicsWorld2D> physicsWorld_;
  79. /// Box2D joint.
  80. b2Joint* joint_;
  81. /// Owner body.
  82. WeakPtr<RigidBody2D> ownerBody_;
  83. /// Other body.
  84. WeakPtr<RigidBody2D> otherBody_;
  85. /// Other body node ID for serialization.
  86. unsigned otherBodyNodeID_;
  87. /// Collide connected flag.
  88. bool collideConnected_;
  89. /// Other body node ID dirty flag.
  90. bool otherBodyNodeIDDirty_;
  91. /// Attached constraint.
  92. WeakPtr<Constraint2D> attachedConstraint_;
  93. };
  94. }