| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include "../Precompiled.h"
- #include "../Core/Context.h"
- #include "../Physics2D/ConstraintFriction2D.h"
- #include "../Physics2D/PhysicsUtils2D.h"
- #include "../Physics2D/RigidBody2D.h"
- #include "../DebugNew.h"
- namespace Urho3D
- {
- extern const char* PHYSICS2D_CATEGORY;
- ConstraintFriction2D::ConstraintFriction2D(Context* context) :
- Constraint2D(context),
- anchor_(Vector2::ZERO)
- {
- }
- ConstraintFriction2D::~ConstraintFriction2D() = default;
- void ConstraintFriction2D::RegisterObject(Context* context)
- {
- context->RegisterFactory<ConstraintFriction2D>(PHYSICS2D_CATEGORY);
- URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
- URHO3D_ACCESSOR_ATTRIBUTE("Anchor", GetAnchor, SetAnchor, Vector2::ZERO, AM_DEFAULT);
- URHO3D_ACCESSOR_ATTRIBUTE("Max Force", GetMaxForce, SetMaxForce, 0.0f, AM_DEFAULT);
- URHO3D_ACCESSOR_ATTRIBUTE("Max Torque", GetMaxTorque, SetMaxTorque, 0.0f, AM_DEFAULT);
- URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
- }
- void ConstraintFriction2D::SetAnchor(const Vector2& anchor)
- {
- if (anchor == anchor_)
- return;
- anchor_ = anchor;
- RecreateJoint();
- MarkNetworkUpdate();
- }
- void ConstraintFriction2D::SetMaxForce(float maxForce)
- {
- if (maxForce == jointDef_.maxForce)
- return;
- jointDef_.maxForce = maxForce;
- if (joint_)
- static_cast<b2FrictionJoint*>(joint_)->SetMaxForce(maxForce);
- else
- RecreateJoint();
- MarkNetworkUpdate();
- }
- void ConstraintFriction2D::SetMaxTorque(float maxTorque)
- {
- if (maxTorque == jointDef_.maxTorque)
- return;
- jointDef_.maxTorque = maxTorque;
- if (joint_)
- static_cast<b2FrictionJoint*>(joint_)->SetMaxTorque(maxTorque);
- else
- RecreateJoint();
- MarkNetworkUpdate();
- }
- b2JointDef* ConstraintFriction2D::GetJointDef()
- {
- if (!ownerBody_ || !otherBody_)
- return nullptr;
- b2Body* bodyA = ownerBody_->GetBody();
- b2Body* bodyB = otherBody_->GetBody();
- if (!bodyA || !bodyB)
- return nullptr;
- jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
- return &jointDef_;
- }
- }
|