ConstraintFriction2D.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintFriction2D.h"
  6. #include "../Physics2D/PhysicsUtils2D.h"
  7. #include "../Physics2D/RigidBody2D.h"
  8. #include "../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. extern const char* PHYSICS2D_CATEGORY;
  12. ConstraintFriction2D::ConstraintFriction2D(Context* context) :
  13. Constraint2D(context),
  14. anchor_(Vector2::ZERO)
  15. {
  16. }
  17. ConstraintFriction2D::~ConstraintFriction2D() = default;
  18. void ConstraintFriction2D::RegisterObject(Context* context)
  19. {
  20. context->RegisterFactory<ConstraintFriction2D>(PHYSICS2D_CATEGORY);
  21. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  22. URHO3D_ACCESSOR_ATTRIBUTE("Anchor", GetAnchor, SetAnchor, Vector2::ZERO, AM_DEFAULT);
  23. URHO3D_ACCESSOR_ATTRIBUTE("Max Force", GetMaxForce, SetMaxForce, 0.0f, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Max Torque", GetMaxTorque, SetMaxTorque, 0.0f, AM_DEFAULT);
  25. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  26. }
  27. void ConstraintFriction2D::SetAnchor(const Vector2& anchor)
  28. {
  29. if (anchor == anchor_)
  30. return;
  31. anchor_ = anchor;
  32. RecreateJoint();
  33. MarkNetworkUpdate();
  34. }
  35. void ConstraintFriction2D::SetMaxForce(float maxForce)
  36. {
  37. if (maxForce == jointDef_.maxForce)
  38. return;
  39. jointDef_.maxForce = maxForce;
  40. if (joint_)
  41. static_cast<b2FrictionJoint*>(joint_)->SetMaxForce(maxForce);
  42. else
  43. RecreateJoint();
  44. MarkNetworkUpdate();
  45. }
  46. void ConstraintFriction2D::SetMaxTorque(float maxTorque)
  47. {
  48. if (maxTorque == jointDef_.maxTorque)
  49. return;
  50. jointDef_.maxTorque = maxTorque;
  51. if (joint_)
  52. static_cast<b2FrictionJoint*>(joint_)->SetMaxTorque(maxTorque);
  53. else
  54. RecreateJoint();
  55. MarkNetworkUpdate();
  56. }
  57. b2JointDef* ConstraintFriction2D::GetJointDef()
  58. {
  59. if (!ownerBody_ || !otherBody_)
  60. return nullptr;
  61. b2Body* bodyA = ownerBody_->GetBody();
  62. b2Body* bodyB = otherBody_->GetBody();
  63. if (!bodyA || !bodyB)
  64. return nullptr;
  65. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
  66. return &jointDef_;
  67. }
  68. }