瀏覽代碼

Fix ConstraintMotor2D, refactor all Constraint2D classes.

aster2013 11 年之前
父節點
當前提交
5b47ba268d

+ 15 - 9
Source/Engine/LuaScript/pkgs/Urho2D/ConstraintMotor2D.pkg

@@ -2,15 +2,21 @@ $#include "ConstraintMotor2D.h"
 
 
 class ConstraintMotor2D : Constraint2D
 class ConstraintMotor2D : Constraint2D
 {
 {
-    void SetOwnerBodyAnchor(const Vector2& anchor);
-    void SetOtherBodyAnchor(const Vector2& anchor);
-    void SetMaxLength(float maxLength);
+    void SetLinearOffset(const Vector2& linearOffset);
+    void SetAngularOffset(float angularOffset);
+    void SetMaxForce(float maxForce);
+    void SetMaxTorque(float maxTorque);
+    void SetCorrectionFactor(float correctionFactor);
 
 
-    const Vector2& GetOwnerBodyAnchor() const;
-    const Vector2& GetOtherBodyAnchor() const;
-    float GetMaxLength() const;
+    const Vector2& GetLinearOffset() const;
+    float GetAngularOffset() const;
+    float GetMaxForce() const;
+    float GetMaxTorque() const;
+    float GetCorrectionFactor() const;
 
 
-    tolua_property__get_set Vector2& ownerBodyAnchor;
-    tolua_property__get_set Vector2& otherBodyAnchor;
-    tolua_property__get_set float maxLength;
+    tolua_property__get_set Vector2& linearOffset;
+    tolua_property__get_set float angularOffset;
+    tolua_property__get_set float maxForce;
+    tolua_property__get_set float maxTorque;
+    tolua_property__get_set float correctionFactor;
 };
 };

+ 10 - 6
Source/Engine/Script/Urho2DAPI.cpp

@@ -464,12 +464,16 @@ static void RegisterConstraintGear2D(asIScriptEngine* engine)
 static void RegisterConstraintMotor2D(asIScriptEngine* engine)
 static void RegisterConstraintMotor2D(asIScriptEngine* engine)
 {
 {
     RegisterConstraint2D<ConstraintMotor2D>(engine, "ConstraintMotor2D");
     RegisterConstraint2D<ConstraintMotor2D>(engine, "ConstraintMotor2D");
-    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_ownerBodyAnchor(const Vector2&)", asMETHOD(ConstraintMotor2D, SetOwnerBodyAnchor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ConstraintMotor2D", "const Vector2& get_ownerBodyAnchor() const", asMETHOD(ConstraintMotor2D, GetOwnerBodyAnchor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_otherBodyAnchor(const Vector2&)", asMETHOD(ConstraintMotor2D, SetOtherBodyAnchor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ConstraintMotor2D", "const Vector2& get_otherBodyAnchor() const", asMETHOD(ConstraintMotor2D, GetOtherBodyAnchor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_maxLength(float)", asMETHOD(ConstraintMotor2D, SetMaxLength), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ConstraintMotor2D", "float get_maxLength() const", asMETHOD(ConstraintMotor2D, GetMaxLength), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_linearOffset(const Vector2&)", asMETHOD(ConstraintMotor2D, SetLinearOffset), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "const Vector2& get_linearOffset() const", asMETHOD(ConstraintMotor2D, GetLinearOffset), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_angularOffset(float)", asMETHOD(ConstraintMotor2D, SetAngularOffset), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "float get_angularOffset() const", asMETHOD(ConstraintMotor2D, GetAngularOffset), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_maxForce(float)", asMETHOD(ConstraintMotor2D, SetMaxForce), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "float get_maxForce() const", asMETHOD(ConstraintMotor2D, GetMaxForce), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_maxTorque(float)", asMETHOD(ConstraintMotor2D, SetMaxTorque), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "float get_maxTorque() const", asMETHOD(ConstraintMotor2D, GetMaxTorque), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "void set_correctionFactor(float)", asMETHOD(ConstraintMotor2D, SetCorrectionFactor), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ConstraintMotor2D", "float get_correctionFactor() const", asMETHOD(ConstraintMotor2D, GetCorrectionFactor), asCALL_THISCALL);
 }
 }
 
 
 static void RegisterConstraintMouse2D(asIScriptEngine* engine)
 static void RegisterConstraintMouse2D(asIScriptEngine* engine)

+ 1 - 3
Source/Engine/Urho2D/Constraint2D.cpp

@@ -75,13 +75,11 @@ void Constraint2D::CreateJoint()
     if (joint_)
     if (joint_)
         return;
         return;
 
 
-    b2JointDef* jointDef = CreateJointDef();
+    b2JointDef* jointDef = GetJointDef();
     if (jointDef)
     if (jointDef)
     {
     {
         joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
         joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
         joint_->SetUserData(this);
         joint_->SetUserData(this);
-
-        delete jointDef;
     }
     }
 }
 }
 
 

+ 2 - 2
Source/Engine/Urho2D/Constraint2D.h

@@ -73,8 +73,8 @@ public:
 protected:
 protected:
     /// Handle node being assigned.
     /// Handle node being assigned.
     virtual void OnNodeSet(Node* node);
     virtual void OnNodeSet(Node* node);
-    /// Create joint def.
-    virtual b2JointDef* CreateJointDef() { return 0; };
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef() { return 0; };
     /// Recreate joint.
     /// Recreate joint.
     void RecreateJoint();
     void RecreateJoint();
     /// Initialize joint def.
     /// Initialize joint def.

+ 8 - 13
Source/Engine/Urho2D/ConstraintDistance2D.cpp

@@ -34,9 +34,7 @@ namespace Urho3D
 ConstraintDistance2D::ConstraintDistance2D(Context* context) :
 ConstraintDistance2D::ConstraintDistance2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
     ownerBodyAnchor_(Vector2::ZERO),
     ownerBodyAnchor_(Vector2::ZERO),
-    otherBodyAnchor_(Vector2::ZERO),
-    frequencyHz_(0.0f),
-    dampingRatio_(0.0f)
+    otherBodyAnchor_(Vector2::ZERO)
 {
 {
 
 
 }
 }
@@ -80,10 +78,10 @@ void ConstraintDistance2D::SetOtherBodyAnchor(const Vector2& anchor)
 
 
 void ConstraintDistance2D::SetFrequencyHz(float frequencyHz)
 void ConstraintDistance2D::SetFrequencyHz(float frequencyHz)
 {
 {
-    if (frequencyHz == frequencyHz_)
+    if (frequencyHz == jointDef_.frequencyHz)
         return;
         return;
 
 
-    frequencyHz_ = frequencyHz;
+    jointDef_.frequencyHz = frequencyHz;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -91,16 +89,16 @@ void ConstraintDistance2D::SetFrequencyHz(float frequencyHz)
 
 
 void ConstraintDistance2D::SetDampingRatio(float dampingRatio)
 void ConstraintDistance2D::SetDampingRatio(float dampingRatio)
 {
 {
-    if (dampingRatio == dampingRatio_)
+    if (dampingRatio == jointDef_.dampingRatio)
         return;
         return;
 
 
-    dampingRatio_ = dampingRatio;
+    jointDef_.dampingRatio = dampingRatio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintDistance2D::CreateJointDef()
+b2JointDef* ConstraintDistance2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -110,12 +108,9 @@ b2JointDef* ConstraintDistance2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2DistanceJointDef* jointDef = new b2DistanceJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_));
-    jointDef->frequencyHz = frequencyHz_;
-    jointDef->dampingRatio = dampingRatio_;
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_));
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 5 - 8
Source/Engine/Urho2D/ConstraintDistance2D.h

@@ -54,22 +54,19 @@ public:
     /// Return other body anchor.
     /// Return other body anchor.
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     /// Return frequency Hz.
     /// Return frequency Hz.
-    float GetFrequencyHz() const { return frequencyHz_; }
+    float GetFrequencyHz() const { return jointDef_.frequencyHz; }
     /// Return damping ratio.
     /// Return damping ratio.
-    float GetDampingRatio() const { return dampingRatio_; }
+    float GetDampingRatio() const { return jointDef_.dampingRatio; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    b2DistanceJointDef jointDef_;
     /// Owner body anchor.
     /// Owner body anchor.
     Vector2 ownerBodyAnchor_;
     Vector2 ownerBodyAnchor_;
     /// Other body anchor.
     /// Other body anchor.
     Vector2 otherBodyAnchor_;
     Vector2 otherBodyAnchor_;
-    /// Frequency Hz.
-    float frequencyHz_;
-    /// Damping ratio.
-    float dampingRatio_;
 };
 };
 
 
 }
 }

+ 8 - 13
Source/Engine/Urho2D/ConstraintFriction2D.cpp

@@ -33,9 +33,7 @@ namespace Urho3D
 
 
 ConstraintFriction2D::ConstraintFriction2D(Context* context) :
 ConstraintFriction2D::ConstraintFriction2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
-    anchor_(Vector2::ZERO),
-    maxForce_(0.0f),
-    maxTorque_(0.0f)
+    anchor_(Vector2::ZERO)
 {
 {
 
 
 }
 }
@@ -67,10 +65,10 @@ void ConstraintFriction2D::SetAnchor(const Vector2& anchor)
 
 
 void ConstraintFriction2D::SetMaxForce(float maxForce)
 void ConstraintFriction2D::SetMaxForce(float maxForce)
 {
 {
-    if (maxForce == maxForce_)
+    if (maxForce == jointDef_.maxForce)
         return;
         return;
 
 
-    maxForce_ = maxForce;
+    jointDef_.maxForce = maxForce;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -79,16 +77,16 @@ void ConstraintFriction2D::SetMaxForce(float maxForce)
 
 
 void ConstraintFriction2D::SetMaxTorque(float maxTorque)
 void ConstraintFriction2D::SetMaxTorque(float maxTorque)
 {
 {
-    if (maxTorque == maxTorque_)
+    if (maxTorque == jointDef_.maxTorque)
         return;
         return;
 
 
-    maxTorque_ = maxTorque;
+    jointDef_.maxTorque = maxTorque;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintFriction2D::CreateJointDef()
+b2JointDef* ConstraintFriction2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -98,12 +96,9 @@ b2JointDef* ConstraintFriction2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2FrictionJointDef* jointDef = new b2FrictionJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
-    jointDef->maxForce = maxForce_;
-    jointDef->maxTorque = maxTorque_;
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));    
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 6 - 8
Source/Engine/Urho2D/ConstraintFriction2D.h

@@ -50,20 +50,18 @@ public:
     /// Return anchor.
     /// Return anchor.
     const Vector2& GetAnchor() const { return anchor_; }
     const Vector2& GetAnchor() const { return anchor_; }
     /// Set max force.
     /// Set max force.
-    float GetMaxForce() const { return maxForce_; }
+    float GetMaxForce() const { return jointDef_.maxForce; }
     /// Set max torque.
     /// Set max torque.
-    float GetMaxTorque() const { return maxTorque_; }
+    float GetMaxTorque() const { return jointDef_.maxTorque; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2FrictionJointDef jointDef_;
     /// Anchor.
     /// Anchor.
     Vector2 anchor_;
     Vector2 anchor_;
-    /// Max force.
-    float maxForce_;
-    /// Max torque.
-    float maxTorque_;
 };
 };
 
 
 }
 }

+ 9 - 13
Source/Engine/Urho2D/ConstraintGear2D.cpp

@@ -32,8 +32,7 @@ namespace Urho3D
 {
 {
 
 
 ConstraintGear2D::ConstraintGear2D(Context* context) :
 ConstraintGear2D::ConstraintGear2D(Context* context) :
-    Constraint2D(context),
-    ratio_(1.0f)
+    Constraint2D(context)
 {
 {
 }
 }
 
 
@@ -86,16 +85,16 @@ void ConstraintGear2D::SetOtherConstraint(Constraint2D* constraint)
 
 
 void ConstraintGear2D::SetRatio(float ratio)
 void ConstraintGear2D::SetRatio(float ratio)
 {
 {
-    if (ratio == ratio_)
+    if (ratio == jointDef_.ratio)
         return;
         return;
 
 
-    ratio_ = ratio;
+    jointDef_.ratio = ratio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintGear2D::CreateJointDef()
+b2JointDef* ConstraintGear2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -113,14 +112,11 @@ b2JointDef* ConstraintGear2D::CreateJointDef()
     if (!jointA || !jointB)
     if (!jointA || !jointB)
         return 0;
         return 0;
 
 
-    b2GearJointDef* jointDef = new b2GearJointDef;
-    InitializeJointDef(jointDef);
-
-    jointDef->joint1 = jointA;
-    jointDef->joint2 = jointB;
-    jointDef->ratio = ratio_;
-
-    return jointDef;
+    InitializeJointDef(&jointDef_);
+    jointDef_.joint1 = jointA;
+    jointDef_.joint2 = jointB;
+    
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 5 - 5
Source/Engine/Urho2D/ConstraintGear2D.h

@@ -52,18 +52,18 @@ public:
     /// Return other constraint.
     /// Return other constraint.
     Constraint2D* GetOtherConstraint() const { return otherConstraint_; }
     Constraint2D* GetOtherConstraint() const { return otherConstraint_; }
     /// Return ratio.
     /// Return ratio.
-    float GetRatio() const { return ratio_; }
+    float GetRatio() const { return jointDef_.ratio; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2GearJointDef jointDef_;
     /// Owner body constraint.
     /// Owner body constraint.
     WeakPtr<Constraint2D> ownerConstraint_;
     WeakPtr<Constraint2D> ownerConstraint_;
     /// Other body constraint.
     /// Other body constraint.
     WeakPtr<Constraint2D> otherConstraint_;
     WeakPtr<Constraint2D> otherConstraint_;
-    /// Ratio.
-    float ratio_;
 };
 };
 
 
 }
 }

+ 41 - 24
Source/Engine/Urho2D/ConstraintMotor2D.cpp

@@ -33,9 +33,7 @@ namespace Urho3D
 
 
 ConstraintMotor2D::ConstraintMotor2D(Context* context) :
 ConstraintMotor2D::ConstraintMotor2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
-    ownerBodyAnchor_(Vector2::ZERO),
-    otherBodyAnchor_(Vector2::ZERO),
-    maxLength_(0.0f)
+    linearOffset_(Vector2::ZERO)
 {
 {
 
 
 }
 }
@@ -48,47 +46,70 @@ void ConstraintMotor2D::RegisterObject(Context* context)
 {
 {
     context->RegisterFactory<ConstraintMotor2D>();
     context->RegisterFactory<ConstraintMotor2D>();
     
     
-    REF_ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_VECTOR2, "Owner Body Anchor", GetOwnerBodyAnchor, SetOwnerBodyAnchor, Vector2, Vector2::ZERO, AM_DEFAULT);
-    REF_ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_VECTOR2, "Other Body Anchor", GetOtherBodyAnchor, SetOtherBodyAnchor, Vector2, Vector2::ZERO, AM_DEFAULT);
-    ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_FLOAT, "Max Length", GetMaxLength, SetMaxLength, float, 0.0f, AM_DEFAULT);
+    REF_ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_VECTOR2, "Linear Offset", GetLinearOffset, SetLinearOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_FLOAT, "Angular Offset", GetAngularOffset, SetAngularOffset, float, 0.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_FLOAT, "Max Force", GetMaxForce, SetMaxForce, float, 1.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_FLOAT, "Max Torque", GetMaxTorque, SetMaxTorque, float, 1.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMotor2D, VAR_FLOAT, "Correction Factor", GetCorrectionFactor, SetCorrectionFactor, float, 0.3f, AM_DEFAULT);
     COPY_BASE_ATTRIBUTES(ConstraintMotor2D, Constraint2D);
     COPY_BASE_ATTRIBUTES(ConstraintMotor2D, Constraint2D);
 }
 }
 
 
-void ConstraintMotor2D::SetOwnerBodyAnchor(const Vector2& anchor)
+void ConstraintMotor2D::SetLinearOffset(const Vector2& linearOffset)
 {
 {
-    if (anchor == ownerBodyAnchor_)
+    if (linearOffset == linearOffset_)
         return;
         return;
 
 
-    ownerBodyAnchor_ = anchor;
+    linearOffset_ = linearOffset;
+    
+    RecreateJoint();
+    MarkNetworkUpdate();
+}
+
+void ConstraintMotor2D::SetAngularOffset(float angularOffset)
+{
+    if (angularOffset == jointDef_.angularOffset)
+        return;
+
+    jointDef_.angularOffset = angularOffset;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-void ConstraintMotor2D::SetOtherBodyAnchor(const Vector2& anchor)
+void ConstraintMotor2D::SetMaxForce(float maxForce)
 {
 {
-    if (anchor == otherBodyAnchor_)
+    if (maxForce == jointDef_.maxForce)
         return;
         return;
 
 
-    otherBodyAnchor_ = anchor;
+    jointDef_.maxForce = maxForce;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-void ConstraintMotor2D::SetMaxLength(float maxLength)
+void ConstraintMotor2D::SetMaxTorque(float maxTorque)
 {
 {
-    maxLength = Max(0.0f, maxLength);
-    if (maxLength == maxLength_)
+    if (maxTorque == jointDef_.maxTorque)
         return;
         return;
 
 
-    maxLength_ = maxLength;
+    jointDef_.maxTorque = maxTorque;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintMotor2D::CreateJointDef()
+void ConstraintMotor2D::SetCorrectionFactor(float correctionFactor)
+{
+    if (correctionFactor == jointDef_.correctionFactor)
+        return;
+
+    jointDef_.correctionFactor = correctionFactor;
+
+    RecreateJoint();
+    MarkNetworkUpdate();
+}
+
+b2JointDef* ConstraintMotor2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -98,14 +119,10 @@ b2JointDef* ConstraintMotor2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2RopeJointDef* jointDef = new b2RopeJointDef;
-    InitializeJointDef(jointDef);
-
-    jointDef->localAnchorA = ToB2Vec2(ownerBodyAnchor_);
-    jointDef->localAnchorB = ToB2Vec2(otherBodyAnchor_);
-    jointDef->maxLength = maxLength_;
+    jointDef_.Initialize(bodyA, bodyB);
+    jointDef_.linearOffset = ToB2Vec2(linearOffset_);
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 26 - 20
Source/Engine/Urho2D/ConstraintMotor2D.h

@@ -40,30 +40,36 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
 
 
-    /// Set owner body anchor.
-    void SetOwnerBodyAnchor(const Vector2& anchor);
-    /// Set other body anchor.
-    void SetOtherBodyAnchor(const Vector2& anchor);
-    /// Set max length.
-    void SetMaxLength(float maxLength);
+    /// Set linear offset.
+    void SetLinearOffset(const Vector2& linearOffset);
+    /// Set angular offset.
+    void SetAngularOffset(float angularOffset);
+    /// Set max force.
+    void SetMaxForce(float maxForce);
+    /// Set max torque.
+    void SetMaxTorque(float maxTorque);
+    /// Set correction factor.
+    void SetCorrectionFactor(float correctionFactor);
 
 
-    /// Return owner body anchor.
-    const Vector2& GetOwnerBodyAnchor() const { return ownerBodyAnchor_; }
-    /// Return other body anchor.
-    const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
-    /// Return max length.
-    float GetMaxLength() const { return maxLength_; }
+    /// Return linear offset.
+    const Vector2& GetLinearOffset() const { return linearOffset_; }
+    /// Return angular offset.
+    float GetAngularOffset() const { return jointDef_.angularOffset; }
+    /// Return max force.
+    float GetMaxForce() const { return jointDef_.maxForce; }
+    /// Return max torque.
+    float GetMaxTorque() const { return jointDef_.maxTorque; }
+    /// Return correction factor.
+    float GetCorrectionFactor() const { return jointDef_.correctionFactor; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
-    /// Owner body anchor.
-    Vector2 ownerBodyAnchor_;
-    /// Other body anchor.
-    Vector2 otherBodyAnchor_;
-    /// Max length.
-    float maxLength_;
+    /// Box2D joint def.
+    b2MotorJointDef jointDef_;
+    /// Linear offset.
+    Vector2 linearOffset_;
 };
 };
 
 
 }
 }

+ 13 - 21
Source/Engine/Urho2D/ConstraintMouse2D.cpp

@@ -33,10 +33,7 @@ namespace Urho3D
 
 
 ConstraintMouse2D::ConstraintMouse2D(Context* context) :
 ConstraintMouse2D::ConstraintMouse2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
-    target_(Vector2::ZERO),
-    maxForce_(0.0f),
-    frequencyHz_(5.0f),
-    dampingRatio_(0.7f)
+    target_(Vector2::ZERO)
 {
 {
 }
 }
 
 
@@ -50,8 +47,8 @@ void ConstraintMouse2D::RegisterObject(Context* context)
 
 
     REF_ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_VECTOR2, "Target", GetTarget, SetTarget, Vector2, Vector2::ZERO, AM_DEFAULT);
     REF_ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_VECTOR2, "Target", GetTarget, SetTarget, Vector2, Vector2::ZERO, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Max Force", GetMaxForce, SetMaxForce, float, 0.0f, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Max Force", GetMaxForce, SetMaxForce, float, 0.0f, AM_DEFAULT);
-    ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Frequency Hz", GetFrequencyHz, SetFrequencyHz, float, 0.0f, AM_DEFAULT);
-    ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Damping Ratio", GetDampingRatio, SetDampingRatio, float, 0.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Frequency Hz", GetFrequencyHz, SetFrequencyHz, float, 5.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(ConstraintMouse2D, VAR_FLOAT, "Damping Ratio", GetDampingRatio, SetDampingRatio, float, 0.7f, AM_DEFAULT);
     COPY_BASE_ATTRIBUTES(ConstraintMouse2D, Constraint2D);
     COPY_BASE_ATTRIBUTES(ConstraintMouse2D, Constraint2D);
 }
 }
 
 
@@ -68,10 +65,10 @@ void ConstraintMouse2D::SetTarget(const Vector2& target)
 
 
 void ConstraintMouse2D::SetMaxForce(float maxForce)
 void ConstraintMouse2D::SetMaxForce(float maxForce)
 {
 {
-    if (maxForce == maxForce_)
+    if (maxForce == jointDef_.maxForce)
         return;
         return;
 
 
-    maxForce_ = maxForce;
+    jointDef_.maxForce = maxForce;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -79,10 +76,10 @@ void ConstraintMouse2D::SetMaxForce(float maxForce)
 
 
 void ConstraintMouse2D::SetFrequencyHz(float frequencyHz)
 void ConstraintMouse2D::SetFrequencyHz(float frequencyHz)
 {
 {
-    if (frequencyHz == frequencyHz_)
+    if (frequencyHz == jointDef_.frequencyHz)
         return;
         return;
 
 
-    frequencyHz_ = frequencyHz;
+    jointDef_.frequencyHz = frequencyHz;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -90,16 +87,16 @@ void ConstraintMouse2D::SetFrequencyHz(float frequencyHz)
 
 
 void ConstraintMouse2D::SetDampingRatio(float dampingRatio)
 void ConstraintMouse2D::SetDampingRatio(float dampingRatio)
 {
 {
-    if (dampingRatio == dampingRatio_)
+    if (dampingRatio == jointDef_.dampingRatio)
         return;
         return;
 
 
-    dampingRatio_ = dampingRatio;
+    jointDef_.dampingRatio = dampingRatio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintMouse2D::CreateJointDef()
+b2JointDef* ConstraintMouse2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -109,15 +106,10 @@ b2JointDef* ConstraintMouse2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2MouseJointDef* jointDef = new b2MouseJointDef;
-    InitializeJointDef(jointDef);
+    InitializeJointDef(&jointDef_);
+    jointDef_.target = ToB2Vec2(target_);
 
 
-    jointDef->target = ToB2Vec2(target_);
-    jointDef->maxForce = maxForce_;
-    jointDef->frequencyHz = frequencyHz_;
-    jointDef->dampingRatio = dampingRatio_;
-
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 8 - 11
Source/Engine/Urho2D/ConstraintMouse2D.h

@@ -52,23 +52,20 @@ public:
     /// Return target.
     /// Return target.
     const Vector2& GetTarget() const { return target_; }
     const Vector2& GetTarget() const { return target_; }
     /// Return max force.
     /// Return max force.
-    float GetMaxForce() const { return maxForce_; }
+    float GetMaxForce() const { return jointDef_.maxForce; }
     /// Return frequency Hz.
     /// Return frequency Hz.
-    float GetFrequencyHz() const { return frequencyHz_; }
+    float GetFrequencyHz() const { return jointDef_.frequencyHz; }
     /// Return damping ratio.
     /// Return damping ratio.
-    float GetDampingRatio() const { return dampingRatio_; }
+    float GetDampingRatio() const { return jointDef_.dampingRatio; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
+
+    /// Box2D joint def.
+    b2MouseJointDef jointDef_; 
     /// Target.
     /// Target.
     Vector2 target_;
     Vector2 target_;
-    /// Max foce.
-    float maxForce_;
-    /// Frequency Hz.
-    float frequencyHz_;
-    /// Damping ratio.
-    float dampingRatio_;
 };
 };
 
 
 }
 }

+ 19 - 31
Source/Engine/Urho2D/ConstraintPrismatic2D.cpp

@@ -34,13 +34,7 @@ namespace Urho3D
 ConstraintPrismatic2D::ConstraintPrismatic2D(Context* context) :
 ConstraintPrismatic2D::ConstraintPrismatic2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
     anchor_(Vector2::ZERO),
     anchor_(Vector2::ZERO),
-    axis_(Vector2::RIGHT),
-    enableLimit_(false),
-    lowerTranslation_(0),
-    upperTranslation_(0),
-    enableMotor_(false),
-    maxMotorForce_(0),
-    motorSpeed_(0)
+    axis_(Vector2::RIGHT)
 {
 {
 }
 }
 
 
@@ -83,12 +77,14 @@ void ConstraintPrismatic2D::SetAxis(const Vector2& axis)
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
-}void ConstraintPrismatic2D::SetEnableLimit(bool enableLimit)
+}
+
+void ConstraintPrismatic2D::SetEnableLimit(bool enableLimit)
 {
 {
-    if (enableLimit == enableLimit_)
+    if (enableLimit == jointDef_.enableLimit)
         return;
         return;
 
 
-    enableLimit_ = enableLimit;
+    jointDef_.enableLimit = enableLimit;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -96,10 +92,10 @@ void ConstraintPrismatic2D::SetAxis(const Vector2& axis)
 
 
 void ConstraintPrismatic2D::SetLowerTranslation(float lowerTranslation)
 void ConstraintPrismatic2D::SetLowerTranslation(float lowerTranslation)
 {
 {
-    if (lowerTranslation == lowerTranslation_)
+    if (lowerTranslation == jointDef_.lowerTranslation)
         return;
         return;
 
 
-    lowerTranslation_ = lowerTranslation;
+    jointDef_.lowerTranslation = lowerTranslation;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -107,10 +103,10 @@ void ConstraintPrismatic2D::SetLowerTranslation(float lowerTranslation)
 
 
 void ConstraintPrismatic2D::SetUpperTranslation(float upperTranslation)
 void ConstraintPrismatic2D::SetUpperTranslation(float upperTranslation)
 {
 {
-    if (upperTranslation == upperTranslation_)
+    if (upperTranslation == jointDef_.upperTranslation)
         return;
         return;
 
 
-    upperTranslation_ = upperTranslation;
+    jointDef_.upperTranslation = upperTranslation;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -118,10 +114,10 @@ void ConstraintPrismatic2D::SetUpperTranslation(float upperTranslation)
 
 
 void ConstraintPrismatic2D::SetEnableMotor(bool enableMotor)
 void ConstraintPrismatic2D::SetEnableMotor(bool enableMotor)
 {
 {
-    if (enableMotor == enableMotor_)
+    if (enableMotor == jointDef_.enableMotor)
         return;
         return;
 
 
-    enableMotor_ = enableMotor;
+    jointDef_.enableMotor = enableMotor;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -129,10 +125,10 @@ void ConstraintPrismatic2D::SetEnableMotor(bool enableMotor)
 
 
 void ConstraintPrismatic2D::SetMaxMotorForce(float maxMotorForce)
 void ConstraintPrismatic2D::SetMaxMotorForce(float maxMotorForce)
 {
 {
-    if (maxMotorForce == maxMotorForce_)
+    if (maxMotorForce == jointDef_.maxMotorForce)
         return;
         return;
 
 
-    maxMotorForce_ = maxMotorForce;
+    jointDef_.maxMotorForce = maxMotorForce;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -140,17 +136,16 @@ void ConstraintPrismatic2D::SetMaxMotorForce(float maxMotorForce)
 
 
 void ConstraintPrismatic2D::SetMotorSpeed(float motorSpeed)
 void ConstraintPrismatic2D::SetMotorSpeed(float motorSpeed)
 {
 {
-    if (motorSpeed == motorSpeed_)
+    if (motorSpeed == jointDef_.motorSpeed)
         return;
         return;
 
 
-    motorSpeed_ = motorSpeed;
+    jointDef_.motorSpeed = motorSpeed;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-
-b2JointDef* ConstraintPrismatic2D::CreateJointDef()
+b2JointDef* ConstraintPrismatic2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -160,16 +155,9 @@ b2JointDef* ConstraintPrismatic2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2PrismaticJointDef* jointDef = new b2PrismaticJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(anchor_), ToB2Vec2(axis_));
-    jointDef->enableLimit = enableLimit_;
-    jointDef->lowerTranslation = lowerTranslation_;
-    jointDef->upperTranslation = upperTranslation_;
-    jointDef->enableMotor = enableMotor_;
-    jointDef->maxMotorForce = maxMotorForce_;
-    jointDef->motorSpeed = motorSpeed_;    
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_), ToB2Vec2(axis_));
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 10 - 20
Source/Engine/Urho2D/ConstraintPrismatic2D.h

@@ -62,38 +62,28 @@ public:
     /// Return axis.
     /// Return axis.
     const Vector2& GetAxis() const { return axis_; }
     const Vector2& GetAxis() const { return axis_; }
     /// Return enable limit.
     /// Return enable limit.
-    bool GetEnableLimit() const { return enableLimit_; }
+    bool GetEnableLimit() const { return jointDef_.enableLimit; }
     /// Return lower translation.
     /// Return lower translation.
-    float GetLowerTranslation() const { return lowerTranslation_; }
+    float GetLowerTranslation() const { return jointDef_.lowerTranslation; }
     /// Return upper translation.
     /// Return upper translation.
-    float GetUpperTranslation() const { return upperTranslation_; }
+    float GetUpperTranslation() const { return jointDef_.upperTranslation; }
     /// Return enable motor.
     /// Return enable motor.
-    bool GetEnableMotor() const { return enableMotor_; }
+    bool GetEnableMotor() const { return jointDef_.enableMotor; }
     /// Return maxmotor force.
     /// Return maxmotor force.
-    float GetMaxMotorForce() const { return maxMotorForce_; }
+    float GetMaxMotorForce() const { return jointDef_.maxMotorForce; }
     /// Return motor speed.
     /// Return motor speed.
-    float GetMotorSpeed() const { return motorSpeed_; }
+    float GetMotorSpeed() const { return jointDef_.motorSpeed; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2PrismaticJointDef jointDef_;
     /// Anchor.
     /// Anchor.
     Vector2 anchor_;
     Vector2 anchor_;
     /// Axis.
     /// Axis.
     Vector2 axis_;
     Vector2 axis_;
-    /// Enable limit.
-    bool enableLimit_;
-    /// Lower translation.
-    float lowerTranslation_;
-    /// Upper translation.
-    float upperTranslation_;
-    /// Enable motor.
-    bool enableMotor_;
-    /// Maxmotor force.
-    float maxMotorForce_;
-    /// Motor speed.
-    float motorSpeed_;
 };
 };
 
 
 }
 }

+ 6 - 8
Source/Engine/Urho2D/ConstraintPulley2D.cpp

@@ -36,8 +36,7 @@ ConstraintPulley2D::ConstraintPulley2D(Context* context) :
     ownerBodyGroundAnchor_(-1.0f, 1.0f),
     ownerBodyGroundAnchor_(-1.0f, 1.0f),
     otherBodyGroundAnchor_(1.0f, 1.0f),
     otherBodyGroundAnchor_(1.0f, 1.0f),
     ownerBodyAnchor_(-1.0f, 0.0f),
     ownerBodyAnchor_(-1.0f, 0.0f),
-    otherBodyAnchor_(1.0f, 0.0f),
-    ratio_(1.0f)
+    otherBodyAnchor_(1.0f, 0.0f)
 {
 {
 
 
 }
 }
@@ -104,16 +103,16 @@ void ConstraintPulley2D::SetOtherBodyAnchor(const Vector2& anchor)
 
 
 void ConstraintPulley2D::SetRatio(float ratio)
 void ConstraintPulley2D::SetRatio(float ratio)
 {
 {
-    if (ratio == ratio)
+    if (ratio == jointDef_.ratio)
         return;
         return;
 
 
-    ratio_ = ratio;
+    jointDef_.ratio = ratio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintPulley2D::CreateJointDef()
+b2JointDef* ConstraintPulley2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -123,10 +122,9 @@ b2JointDef* ConstraintPulley2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2PulleyJointDef* jointDef = new b2PulleyJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyGroundAnchor_), ToB2Vec2(otherBodyGroundAnchor_), ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_), ratio_);
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyGroundAnchor_), ToB2Vec2(otherBodyGroundAnchor_), ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_), jointDef_.ratio);
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 5 - 5
Source/Engine/Urho2D/ConstraintPulley2D.h

@@ -60,13 +60,15 @@ public:
     /// Return other body anchor.
     /// Return other body anchor.
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     /// Return ratio.
     /// Return ratio.
-    float GetRatio() const { return ratio_; }
+    float GetRatio() const { return jointDef_.ratio; }
 
 
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return Joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2PulleyJointDef jointDef_;
     /// Owner body ground anchor.
     /// Owner body ground anchor.
     Vector2 ownerBodyGroundAnchor_;
     Vector2 ownerBodyGroundAnchor_;
     /// Other body ground anchor.
     /// Other body ground anchor.
@@ -75,8 +77,6 @@ private:
     Vector2 ownerBodyAnchor_;
     Vector2 ownerBodyAnchor_;
     /// Other body anchor.
     /// Other body anchor.
     Vector2 otherBodyAnchor_;
     Vector2 otherBodyAnchor_;
-    /// Ratio.
-    float ratio_;
 };
 };
 
 
 }
 }

+ 16 - 29
Source/Engine/Urho2D/ConstraintRevolute2D.cpp

@@ -33,13 +33,7 @@ namespace Urho3D
 
 
 ConstraintRevolute2D::ConstraintRevolute2D(Context* context) :
 ConstraintRevolute2D::ConstraintRevolute2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
-    anchor_(Vector2::ZERO),
-    lowerAngle_(0.0f),
-    upperAngle_(0.0f),
-    maxMotorTorque_(0.0f),
-    motorSpeed_(0.0f),
-    enableLimit_(false),
-    enableMotor_(false)
+    anchor_(Vector2::ZERO)
 {
 {
 }
 }
 
 
@@ -74,10 +68,10 @@ void ConstraintRevolute2D::SetAnchor(const Vector2& anchor)
 
 
 void ConstraintRevolute2D::SetEnableLimit(bool enableLimit)
 void ConstraintRevolute2D::SetEnableLimit(bool enableLimit)
 {
 {
-    if (enableLimit == enableLimit_)
+    if (enableLimit == jointDef_.enableLimit)
         return;
         return;
 
 
-    enableLimit_ = enableLimit;
+    jointDef_.enableLimit = enableLimit;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -85,10 +79,10 @@ void ConstraintRevolute2D::SetEnableLimit(bool enableLimit)
 
 
 void ConstraintRevolute2D::SetLowerAngle(float lowerAngle)
 void ConstraintRevolute2D::SetLowerAngle(float lowerAngle)
 {
 {
-    if (lowerAngle == lowerAngle_)
+    if (lowerAngle == jointDef_.lowerAngle)
         return;
         return;
 
 
-    lowerAngle_ = lowerAngle;
+    jointDef_.lowerAngle = lowerAngle;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -96,10 +90,10 @@ void ConstraintRevolute2D::SetLowerAngle(float lowerAngle)
 
 
 void ConstraintRevolute2D::SetUpperAngle(float upperAngle)
 void ConstraintRevolute2D::SetUpperAngle(float upperAngle)
 {
 {
-    if (upperAngle == upperAngle_)
+    if (upperAngle == jointDef_.upperAngle)
         return;
         return;
 
 
-    upperAngle_ = upperAngle;
+    jointDef_.upperAngle = upperAngle;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -107,10 +101,10 @@ void ConstraintRevolute2D::SetUpperAngle(float upperAngle)
 
 
 void ConstraintRevolute2D::SetEnableMotor(bool enableMotor)
 void ConstraintRevolute2D::SetEnableMotor(bool enableMotor)
 {
 {
-    if (enableMotor == enableMotor_)
+    if (enableMotor == jointDef_.enableMotor)
         return;
         return;
 
 
-    enableMotor_ = enableMotor;
+    jointDef_.enableMotor = enableMotor;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -118,10 +112,10 @@ void ConstraintRevolute2D::SetEnableMotor(bool enableMotor)
 
 
 void ConstraintRevolute2D::SetMotorSpeed(float motorSpeed)
 void ConstraintRevolute2D::SetMotorSpeed(float motorSpeed)
 {
 {
-    if (motorSpeed == motorSpeed_)
+    if (motorSpeed == jointDef_.motorSpeed)
         return;
         return;
 
 
-    motorSpeed_ = motorSpeed;
+    jointDef_.motorSpeed = motorSpeed;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -129,16 +123,16 @@ void ConstraintRevolute2D::SetMotorSpeed(float motorSpeed)
 
 
 void ConstraintRevolute2D::SetMaxMotorTorque(float maxMotorTorque)
 void ConstraintRevolute2D::SetMaxMotorTorque(float maxMotorTorque)
 {
 {
-    if (maxMotorTorque == maxMotorTorque_)
+    if (maxMotorTorque == jointDef_.maxMotorTorque)
         return;
         return;
 
 
-    maxMotorTorque_ = maxMotorTorque;
+    jointDef_.maxMotorTorque = maxMotorTorque;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintRevolute2D::CreateJointDef()
+b2JointDef* ConstraintRevolute2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -148,16 +142,9 @@ b2JointDef* ConstraintRevolute2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2RevoluteJointDef* jointDef = new b2RevoluteJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
-    jointDef->enableLimit = enableLimit_;
-    jointDef->lowerAngle = lowerAngle_;
-    jointDef->upperAngle = upperAngle_;
-    jointDef->enableMotor = enableMotor_;
-    jointDef->motorSpeed = motorSpeed_;
-    jointDef->maxMotorTorque = maxMotorTorque_;
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 10 - 20
Source/Engine/Urho2D/ConstraintRevolute2D.h

@@ -58,36 +58,26 @@ public:
     /// Return anchor.
     /// Return anchor.
     const Vector2& GetAnchor() const { return anchor_; }
     const Vector2& GetAnchor() const { return anchor_; }
     /// Return enable limit.
     /// Return enable limit.
-    bool GetEnableLimit() const { return enableLimit_; }
+    bool GetEnableLimit() const { return jointDef_.enableLimit; }
     /// Return lower angle.
     /// Return lower angle.
-    float GetLowerAngle() const { return lowerAngle_; }
+    float GetLowerAngle() const { return jointDef_.lowerAngle; }
     /// Return upper angle.
     /// Return upper angle.
-    float GetUpperAngle() const { return upperAngle_; }
+    float GetUpperAngle() const { return jointDef_.upperAngle; }
     /// Return enable motor.
     /// Return enable motor.
-    bool GetEnableMotor() const { return enableMotor_; }
+    bool GetEnableMotor() const { return jointDef_.enableMotor; }
     /// Return motor speed.
     /// Return motor speed.
-    float GetMotorSpeed() const { return motorSpeed_; }
+    float GetMotorSpeed() const { return jointDef_.motorSpeed; }
     /// Return max motor torque.
     /// Return max motor torque.
-    float GetMaxMotorTorque() const { return maxMotorTorque_; }
+    float GetMaxMotorTorque() const { return jointDef_.maxMotorTorque; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2RevoluteJointDef jointDef_;
     /// Anchor.
     /// Anchor.
     Vector2 anchor_;
     Vector2 anchor_;
-    /// Enable limit.
-    bool enableLimit_;
-    /// Lower angle.
-    float lowerAngle_;
-    /// Upper angle.
-    float upperAngle_;
-    /// Enable motor.
-    bool enableMotor_;
-    /// Motor speed.
-    float motorSpeed_;
-    /// Max motor torque.
-    float maxMotorTorque_;
 };
 };
 
 
 }
 }

+ 8 - 12
Source/Engine/Urho2D/ConstraintRope2D.cpp

@@ -34,8 +34,7 @@ namespace Urho3D
 ConstraintRope2D::ConstraintRope2D(Context* context) :
 ConstraintRope2D::ConstraintRope2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
     ownerBodyAnchor_(Vector2::ZERO),
     ownerBodyAnchor_(Vector2::ZERO),
-    otherBodyAnchor_(Vector2::ZERO),
-    maxLength_(0.0f)
+    otherBodyAnchor_(Vector2::ZERO)
 {
 {
 
 
 }
 }
@@ -79,16 +78,16 @@ void ConstraintRope2D::SetOtherBodyAnchor(const Vector2& anchor)
 void ConstraintRope2D::SetMaxLength(float maxLength)
 void ConstraintRope2D::SetMaxLength(float maxLength)
 {
 {
     maxLength = Max(0.0f, maxLength);
     maxLength = Max(0.0f, maxLength);
-    if (maxLength == maxLength_)
+    if (maxLength == jointDef_.maxLength)
         return;
         return;
 
 
-    maxLength_ = maxLength;
+    jointDef_.maxLength = maxLength;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintRope2D::CreateJointDef()
+b2JointDef* ConstraintRope2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -98,14 +97,11 @@ b2JointDef* ConstraintRope2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2RopeJointDef* jointDef = new b2RopeJointDef;
-    InitializeJointDef(jointDef);
+    InitializeJointDef(&jointDef_);
+    jointDef_.localAnchorA = ToB2Vec2(ownerBodyAnchor_);
+    jointDef_.localAnchorB = ToB2Vec2(otherBodyAnchor_);
 
 
-    jointDef->localAnchorA = ToB2Vec2(ownerBodyAnchor_);
-    jointDef->localAnchorB = ToB2Vec2(otherBodyAnchor_);
-    jointDef->maxLength = maxLength_;
-
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 5 - 5
Source/Engine/Urho2D/ConstraintRope2D.h

@@ -52,18 +52,18 @@ public:
     /// Return other body anchor.
     /// Return other body anchor.
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     const Vector2& GetOtherBodyAnchor() const { return otherBodyAnchor_; }
     /// Return max length.
     /// Return max length.
-    float GetMaxLength() const { return maxLength_; }
+    float GetMaxLength() const { return jointDef_.maxLength; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2RopeJointDef jointDef_;
     /// Owner body anchor.
     /// Owner body anchor.
     Vector2 ownerBodyAnchor_;
     Vector2 ownerBodyAnchor_;
     /// Other body anchor.
     /// Other body anchor.
     Vector2 otherBodyAnchor_;
     Vector2 otherBodyAnchor_;
-    /// Max length.
-    float maxLength_;
 };
 };
 
 
 }
 }

+ 8 - 13
Source/Engine/Urho2D/ConstraintWeld2D.cpp

@@ -33,9 +33,7 @@ namespace Urho3D
 
 
 ConstraintWeld2D::ConstraintWeld2D(Context* context) :
 ConstraintWeld2D::ConstraintWeld2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
-    anchor_(Vector2::ZERO),
-    frequencyHz_(0.0f),
-    dampingRatio_(0.0f)
+    anchor_(Vector2::ZERO)
 {
 {
 }
 }
 
 
@@ -66,10 +64,10 @@ void ConstraintWeld2D::SetAnchor(const Vector2& anchor)
 
 
 void ConstraintWeld2D::SetFrequencyHz(float frequencyHz)
 void ConstraintWeld2D::SetFrequencyHz(float frequencyHz)
 {
 {
-    if (frequencyHz == frequencyHz_)
+    if (frequencyHz == jointDef_.frequencyHz)
         return;
         return;
 
 
-    frequencyHz_ = frequencyHz;
+    jointDef_.frequencyHz = frequencyHz;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -77,16 +75,16 @@ void ConstraintWeld2D::SetFrequencyHz(float frequencyHz)
 
 
 void ConstraintWeld2D::SetDampingRatio(float dampingRatio)
 void ConstraintWeld2D::SetDampingRatio(float dampingRatio)
 {
 {
-    if (dampingRatio == dampingRatio_)
+    if (dampingRatio == jointDef_.dampingRatio)
         return;
         return;
 
 
-    dampingRatio_ = dampingRatio;
+    jointDef_.dampingRatio = dampingRatio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintWeld2D::CreateJointDef()
+b2JointDef* ConstraintWeld2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -96,12 +94,9 @@ b2JointDef* ConstraintWeld2D::CreateJointDef()
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
 
 
-    b2WeldJointDef* jointDef = new b2WeldJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
-    jointDef->frequencyHz = frequencyHz_;
-    jointDef->dampingRatio = dampingRatio_;
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
 
 
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 6 - 8
Source/Engine/Urho2D/ConstraintWeld2D.h

@@ -50,20 +50,18 @@ public:
     /// Return anchor.
     /// Return anchor.
     const Vector2& GetAnchor() const { return anchor_; }
     const Vector2& GetAnchor() const { return anchor_; }
     /// Return frequency Hz.
     /// Return frequency Hz.
-    float GetFrequencyHz() const { return frequencyHz_; }
+    float GetFrequencyHz() const { return jointDef_.frequencyHz; }
     /// Return damping ratio.
     /// Return damping ratio.
-    float GetDampingRatio() const { return dampingRatio_; }
+    float GetDampingRatio() const { return jointDef_.dampingRatio; }
 
 
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2WeldJointDef jointDef_;
     /// Anchor.
     /// Anchor.
     Vector2 anchor_;
     Vector2 anchor_;
-    /// Frequency Hz.
-    float frequencyHz_;
-    /// Damping ratio.
-    float dampingRatio_;
 };
 };
 
 
 }
 }

+ 16 - 26
Source/Engine/Urho2D/ConstraintWheel2D.cpp

@@ -34,12 +34,7 @@ namespace Urho3D
 ConstraintWheel2D::ConstraintWheel2D(Context* context) :
 ConstraintWheel2D::ConstraintWheel2D(Context* context) :
     Constraint2D(context),
     Constraint2D(context),
     anchor_(Vector2::ZERO),
     anchor_(Vector2::ZERO),
-    axis_(Vector2::RIGHT),    
-    enableMotor_(false), 
-    maxMotorTorque_(0.0f),
-    motorSpeed_(0.0f),
-    frequencyHz_(2.0f),
-    dampingRatio_(0.7f)
+    axis_(Vector2::RIGHT)
 {
 {
 }
 }
 
 
@@ -83,12 +78,13 @@ void ConstraintWheel2D::SetAxis(const Vector2& axis)
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
+
 void ConstraintWheel2D::SetEnableMotor(bool enableMotor)
 void ConstraintWheel2D::SetEnableMotor(bool enableMotor)
 {
 {
-    if (enableMotor == enableMotor_)
+    if (enableMotor == jointDef_.enableMotor)
         return;
         return;
 
 
-    enableMotor_ = enableMotor;
+    jointDef_.enableMotor = enableMotor;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -96,10 +92,10 @@ void ConstraintWheel2D::SetEnableMotor(bool enableMotor)
 
 
 void ConstraintWheel2D::SetMaxMotorTorque(float maxMotorTorque)
 void ConstraintWheel2D::SetMaxMotorTorque(float maxMotorTorque)
 {
 {
-    if (maxMotorTorque == maxMotorTorque_)
+    if (maxMotorTorque == jointDef_.maxMotorTorque)
         return;
         return;
 
 
-    maxMotorTorque_ = maxMotorTorque;
+    jointDef_.maxMotorTorque = maxMotorTorque;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -107,10 +103,10 @@ void ConstraintWheel2D::SetMaxMotorTorque(float maxMotorTorque)
 
 
 void ConstraintWheel2D::SetMotorSpeed(float motorSpeed)
 void ConstraintWheel2D::SetMotorSpeed(float motorSpeed)
 {
 {
-    if (motorSpeed == motorSpeed_)
+    if (motorSpeed == jointDef_.motorSpeed)
         return;
         return;
 
 
-    motorSpeed_ = motorSpeed;
+    jointDef_.motorSpeed = motorSpeed;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -118,10 +114,10 @@ void ConstraintWheel2D::SetMotorSpeed(float motorSpeed)
 
 
 void ConstraintWheel2D::SetFrequencyHz(float frequencyHz)
 void ConstraintWheel2D::SetFrequencyHz(float frequencyHz)
 {
 {
-    if (frequencyHz == frequencyHz_)
+    if (frequencyHz == jointDef_.frequencyHz)
         return;
         return;
 
 
-    frequencyHz_ = frequencyHz;
+    jointDef_.frequencyHz = frequencyHz;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
@@ -129,16 +125,16 @@ void ConstraintWheel2D::SetFrequencyHz(float frequencyHz)
 
 
 void ConstraintWheel2D::SetDampingRatio(float dampingRatio)
 void ConstraintWheel2D::SetDampingRatio(float dampingRatio)
 {
 {
-    if (dampingRatio == dampingRatio_)
+    if (dampingRatio == jointDef_.dampingRatio)
         return;
         return;
 
 
-    dampingRatio_ = dampingRatio;
+    jointDef_.dampingRatio = dampingRatio;
 
 
     RecreateJoint();
     RecreateJoint();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
 
 
-b2JointDef* ConstraintWheel2D::CreateJointDef()
+b2JointDef* ConstraintWheel2D::GetJointDef()
 {
 {
     if (!ownerBody_ || !otherBody_)
     if (!ownerBody_ || !otherBody_)
         return 0;
         return 0;
@@ -147,16 +143,10 @@ b2JointDef* ConstraintWheel2D::CreateJointDef()
     b2Body* bodyB = otherBody_->GetBody();
     b2Body* bodyB = otherBody_->GetBody();
     if (!bodyA || !bodyB)
     if (!bodyA || !bodyB)
         return 0;
         return 0;
+    
+    jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_), ToB2Vec2(axis_));
 
 
-    b2WheelJointDef* jointDef = new b2WheelJointDef;
-    jointDef->Initialize(bodyA, bodyB, ToB2Vec2(anchor_), ToB2Vec2(axis_));
-    jointDef->enableMotor = enableMotor_;
-    jointDef->maxMotorTorque = maxMotorTorque_;
-    jointDef->motorSpeed = motorSpeed_;
-    jointDef->frequencyHz = frequencyHz_;
-    jointDef->dampingRatio = dampingRatio_;    
-
-    return jointDef;
+    return &jointDef_;
 }
 }
 
 
 }
 }

+ 10 - 18
Source/Engine/Urho2D/ConstraintWheel2D.h

@@ -60,34 +60,26 @@ public:
     /// Return axis.
     /// Return axis.
     const Vector2& GetAxis() const { return axis_; }
     const Vector2& GetAxis() const { return axis_; }
     /// Return enable motor.
     /// Return enable motor.
-    bool GetEnableMotor() const { return enableMotor_; }
+    bool GetEnableMotor() const { return jointDef_.enableMotor; }
     /// Return maxMotor torque.
     /// Return maxMotor torque.
-    float GetMaxMotorTorque() const { return maxMotorTorque_; }
+    float GetMaxMotorTorque() const { return jointDef_.maxMotorTorque; }
     /// Return motor speed.
     /// Return motor speed.
-    float GetMotorSpeed() const { return motorSpeed_; }
+    float GetMotorSpeed() const { return jointDef_.motorSpeed; }
     /// Return frequency Hz.
     /// Return frequency Hz.
-    float GetFrequencyHz() const { return frequencyHz_; }
+    float GetFrequencyHz() const { return jointDef_.frequencyHz; }
     /// Return damping ratio.
     /// Return damping ratio.
-    float GetDampingRatio() const { return dampingRatio_; }
-
+    float GetDampingRatio() const { return jointDef_.dampingRatio; }
+   
 private:
 private:
-    /// Create Joint def.
-    virtual b2JointDef* CreateJointDef();
+    /// Return joint def.
+    virtual b2JointDef* GetJointDef();
 
 
+    /// Box2D joint def.
+    b2WheelJointDef jointDef_;
     /// Anchor.
     /// Anchor.
     Vector2 anchor_;
     Vector2 anchor_;
     /// Axis.
     /// Axis.
     Vector2 axis_;
     Vector2 axis_;
-    /// Enable motor.
-    bool enableMotor_;
-    /// Max motor torque.
-    float maxMotorTorque_;
-    /// Motor speed.
-    float motorSpeed_;
-    /// Frequency Hz.
-    float frequencyHz_;
-    /// Damping ratio.
-    float dampingRatio_;
 };
 };
 
 
 }
 }