1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "RevoluteJoint.h" |
22 | |
23 | #include "common/math.h" |
24 | |
25 | // Module |
26 | #include "Body.h" |
27 | #include "World.h" |
28 | #include "Physics.h" |
29 | |
30 | namespace love |
31 | { |
32 | namespace physics |
33 | { |
34 | namespace box2d |
35 | { |
36 | |
37 | love::Type RevoluteJoint::type("RevoluteJoint" , &Joint::type); |
38 | |
39 | RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) |
40 | : Joint(body1, body2) |
41 | , joint(NULL) |
42 | { |
43 | b2RevoluteJointDef def; |
44 | init(def, body1, body2, xA, yA, xB, yB, collideConnected); |
45 | joint = (b2RevoluteJoint *)createJoint(&def); |
46 | } |
47 | |
48 | RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle) |
49 | : Joint(body1, body2) |
50 | , joint(NULL) |
51 | { |
52 | b2RevoluteJointDef def; |
53 | init(def, body1, body2, xA, yA, xB, yB, collideConnected); |
54 | def.referenceAngle = referenceAngle; |
55 | joint = (b2RevoluteJoint *)createJoint(&def); |
56 | } |
57 | |
58 | RevoluteJoint::~RevoluteJoint() |
59 | { |
60 | } |
61 | |
62 | void RevoluteJoint::init(b2RevoluteJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) |
63 | { |
64 | def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); |
65 | def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); |
66 | def.collideConnected = collideConnected; |
67 | } |
68 | |
69 | float RevoluteJoint::getJointAngle() const |
70 | { |
71 | return joint->GetJointAngle(); |
72 | } |
73 | |
74 | float RevoluteJoint::getJointSpeed() const |
75 | { |
76 | return joint->GetJointSpeed(); |
77 | } |
78 | |
79 | void RevoluteJoint::setMotorEnabled(bool enable) |
80 | { |
81 | return joint->EnableMotor(enable); |
82 | } |
83 | |
84 | bool RevoluteJoint::isMotorEnabled() const |
85 | { |
86 | return joint->IsMotorEnabled(); |
87 | } |
88 | |
89 | void RevoluteJoint::setMaxMotorTorque(float torque) |
90 | { |
91 | joint->SetMaxMotorTorque(Physics::scaleDown(Physics::scaleDown(torque))); |
92 | } |
93 | |
94 | void RevoluteJoint::setMotorSpeed(float speed) |
95 | { |
96 | joint->SetMotorSpeed(speed); |
97 | } |
98 | |
99 | float RevoluteJoint::getMotorSpeed() const |
100 | { |
101 | return joint->GetMotorSpeed(); |
102 | } |
103 | |
104 | float RevoluteJoint::getMotorTorque(float inv_dt) const |
105 | { |
106 | return Physics::scaleUp(Physics::scaleUp(joint->GetMotorTorque(inv_dt))); |
107 | } |
108 | |
109 | float RevoluteJoint::getMaxMotorTorque() const |
110 | { |
111 | return Physics::scaleUp(Physics::scaleUp(joint->GetMaxMotorTorque())); |
112 | } |
113 | |
114 | void RevoluteJoint::setLimitsEnabled(bool enable) |
115 | { |
116 | joint->EnableLimit(enable); |
117 | } |
118 | |
119 | bool RevoluteJoint::areLimitsEnabled() const |
120 | { |
121 | return joint->IsLimitEnabled(); |
122 | } |
123 | |
124 | void RevoluteJoint::setUpperLimit(float limit) |
125 | { |
126 | joint->SetLimits(joint->GetLowerLimit(), limit); |
127 | } |
128 | |
129 | void RevoluteJoint::setLowerLimit(float limit) |
130 | { |
131 | joint->SetLimits(limit, joint->GetUpperLimit()); |
132 | } |
133 | |
134 | void RevoluteJoint::setLimits(float lower, float upper) |
135 | { |
136 | joint->SetLimits(lower, upper); |
137 | } |
138 | |
139 | float RevoluteJoint::getLowerLimit() const |
140 | { |
141 | return joint->GetLowerLimit(); |
142 | } |
143 | |
144 | float RevoluteJoint::getUpperLimit() const |
145 | { |
146 | return joint->GetUpperLimit(); |
147 | } |
148 | |
149 | int RevoluteJoint::getLimits(lua_State *L) |
150 | { |
151 | lua_pushnumber(L, joint->GetLowerLimit()); |
152 | lua_pushnumber(L, joint->GetUpperLimit()); |
153 | return 2; |
154 | } |
155 | |
156 | float RevoluteJoint::getReferenceAngle() const |
157 | { |
158 | return joint->GetReferenceAngle(); |
159 | } |
160 | |
161 | } // box2d |
162 | } // physics |
163 | } // love |
164 | |