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
23 #include "../Precompiled.h"
24
25 #include "../Core/Context.h"
26 #include "../Urho2D/ConstraintMotor2D.h"
27 #include "../Urho2D/PhysicsUtils2D.h"
28 #include "../Urho2D/RigidBody2D.h"
29
30 #include "../DebugNew.h"
31
32 namespace Urho3D
33 {
34
35 extern const char* URHO2D_CATEGORY;
36
ConstraintMotor2D(Context * context)37 ConstraintMotor2D::ConstraintMotor2D(Context* context) :
38 Constraint2D(context),
39 linearOffset_(Vector2::ZERO)
40 {
41
42 }
43
~ConstraintMotor2D()44 ConstraintMotor2D::~ConstraintMotor2D()
45 {
46 }
47
RegisterObject(Context * context)48 void ConstraintMotor2D::RegisterObject(Context* context)
49 {
50 context->RegisterFactory<ConstraintMotor2D>(URHO2D_CATEGORY);
51
52 URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
53 URHO3D_ACCESSOR_ATTRIBUTE("Linear Offset", GetLinearOffset, SetLinearOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
54 URHO3D_ACCESSOR_ATTRIBUTE("Angular Offset", GetAngularOffset, SetAngularOffset, float, 0.0f, AM_DEFAULT);
55 URHO3D_ACCESSOR_ATTRIBUTE("Max Force", GetMaxForce, SetMaxForce, float, 1.0f, AM_DEFAULT);
56 URHO3D_ACCESSOR_ATTRIBUTE("Max Torque", GetMaxTorque, SetMaxTorque, float, 1.0f, AM_DEFAULT);
57 URHO3D_ACCESSOR_ATTRIBUTE("Correction Factor", GetCorrectionFactor, SetCorrectionFactor, float, 0.3f, AM_DEFAULT);
58 URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
59 }
60
SetLinearOffset(const Vector2 & linearOffset)61 void ConstraintMotor2D::SetLinearOffset(const Vector2& linearOffset)
62 {
63 if (linearOffset == linearOffset_)
64 return;
65
66 linearOffset_ = linearOffset;
67
68 if (joint_)
69 static_cast<b2MotorJoint*>(joint_)->SetLinearOffset(ToB2Vec2(linearOffset));
70 else
71 RecreateJoint();
72
73 MarkNetworkUpdate();
74 }
75
SetAngularOffset(float angularOffset)76 void ConstraintMotor2D::SetAngularOffset(float angularOffset)
77 {
78 if (angularOffset == jointDef_.angularOffset)
79 return;
80
81 jointDef_.angularOffset = angularOffset;
82
83 if (joint_)
84 static_cast<b2MotorJoint*>(joint_)->SetAngularOffset(angularOffset);
85 else
86 RecreateJoint();
87
88 MarkNetworkUpdate();
89 }
90
SetMaxForce(float maxForce)91 void ConstraintMotor2D::SetMaxForce(float maxForce)
92 {
93 if (maxForce == jointDef_.maxForce)
94 return;
95
96 jointDef_.maxForce = maxForce;
97
98 if (joint_)
99 static_cast<b2MotorJoint*>(joint_)->SetMaxForce(maxForce);
100 else
101 RecreateJoint();
102
103 MarkNetworkUpdate();
104 }
105
SetMaxTorque(float maxTorque)106 void ConstraintMotor2D::SetMaxTorque(float maxTorque)
107 {
108 if (maxTorque == jointDef_.maxTorque)
109 return;
110
111 jointDef_.maxTorque = maxTorque;
112
113 if (joint_)
114 static_cast<b2MotorJoint*>(joint_)->SetMaxTorque(maxTorque);
115 else
116 RecreateJoint();
117
118 MarkNetworkUpdate();
119 }
120
SetCorrectionFactor(float correctionFactor)121 void ConstraintMotor2D::SetCorrectionFactor(float correctionFactor)
122 {
123 if (correctionFactor == jointDef_.correctionFactor)
124 return;
125
126 jointDef_.correctionFactor = correctionFactor;
127
128 if (joint_)
129 static_cast<b2MotorJoint*>(joint_)->SetCorrectionFactor(correctionFactor);
130 else
131 RecreateJoint();
132
133 MarkNetworkUpdate();
134 }
135
GetJointDef()136 b2JointDef* ConstraintMotor2D::GetJointDef()
137 {
138 if (!ownerBody_ || !otherBody_)
139 return 0;
140
141 b2Body* bodyA = ownerBody_->GetBody();
142 b2Body* bodyB = otherBody_->GetBody();
143 if (!bodyA || !bodyB)
144 return 0;
145
146 jointDef_.Initialize(bodyA, bodyB);
147 jointDef_.linearOffset = ToB2Vec2(linearOffset_);
148
149 return &jointDef_;
150 }
151
152 }
153