Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Fix keeping track of RigidBody2D constraints. Closes #1569.
Browse files Browse the repository at this point in the history
  • Loading branch information
cadaver committed Sep 7, 2016
1 parent 4bf27d1 commit 6be0d71
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Source/Urho3D/Graphics/GraphicsDefs.h
Expand Up @@ -356,7 +356,7 @@ enum FaceCameraMode
FC_DIRECTION,
};

/// Shadow type
/// Shadow type.
enum ShadowQuality
{
SHADOWQUALITY_SIMPLE_16BIT = 0,
Expand Down
18 changes: 12 additions & 6 deletions Source/Urho3D/Urho2D/Constraint2D.cpp
Expand Up @@ -48,12 +48,6 @@ Constraint2D::Constraint2D(Context* context) :

Constraint2D::~Constraint2D()
{
if (ownerBody_)
ownerBody_->RemoveConstraint2D(this);

if (otherBody_)
otherBody_->RemoveConstraint2D(this);

ReleaseJoint();
}

Expand All @@ -80,6 +74,12 @@ void Constraint2D::CreateJoint()
{
joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
joint_->SetUserData(this);

if (ownerBody_)
ownerBody_->AddConstraint2D(this);

if (otherBody_)
otherBody_->AddConstraint2D(this);
}
}

Expand All @@ -88,6 +88,12 @@ void Constraint2D::ReleaseJoint()
if (!joint_)
return;

if (ownerBody_)
ownerBody_->RemoveConstraint2D(this);

if (otherBody_)
otherBody_->RemoveConstraint2D(this);

if (physicsWorld_)
physicsWorld_->GetWorld()->DestroyJoint(joint_);

Expand Down
4 changes: 2 additions & 2 deletions Source/Urho3D/Urho2D/Constraint2D.h
Expand Up @@ -47,9 +47,9 @@ class URHO3D_API Constraint2D : public Component

/// Handle enabled/disabled state change.
virtual void OnSetEnabled();
/// Create Joint.
/// Create joint.
void CreateJoint();
/// Release Joint.
/// Release joint.
void ReleaseJoint();

/// Set other rigid body.
Expand Down
8 changes: 5 additions & 3 deletions Source/Urho3D/Urho2D/RigidBody2D.cpp
Expand Up @@ -395,10 +395,12 @@ void RigidBody2D::ReleaseBody()
if (!physicsWorld_ || !physicsWorld_->GetWorld())
return;

for (unsigned i = 0; i < constraints_.Size(); ++i)
// Make a copy for iteration
Vector<WeakPtr<Constraint2D> > constraints = constraints_;
for (unsigned i = 0; i < constraints.Size(); ++i)
{
if (constraints_[i])
constraints_[i]->ReleaseJoint();
if (constraints[i])
constraints[i]->ReleaseJoint();
}

for (unsigned i = 0; i < collisionShapes_.Size(); ++i)
Expand Down

0 comments on commit 6be0d71

Please sign in to comment.