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 "PrismaticJoint.h" |
22 | |
23 | // Module |
24 | #include "Body.h" |
25 | #include "World.h" |
26 | #include "Physics.h" |
27 | |
28 | namespace love |
29 | { |
30 | namespace physics |
31 | { |
32 | namespace box2d |
33 | { |
34 | |
35 | love::Type PrismaticJoint::type("PrismaticJoint" , &Joint::type); |
36 | |
37 | PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) |
38 | : Joint(body1, body2) |
39 | , joint(NULL) |
40 | { |
41 | b2PrismaticJointDef def; |
42 | init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); |
43 | joint = (b2PrismaticJoint *)createJoint(&def); |
44 | } |
45 | |
46 | PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle) |
47 | : Joint(body1, body2) |
48 | , joint(NULL) |
49 | { |
50 | b2PrismaticJointDef def; |
51 | init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); |
52 | def.referenceAngle = referenceAngle; |
53 | joint = (b2PrismaticJoint *)createJoint(&def); |
54 | } |
55 | |
56 | PrismaticJoint::~PrismaticJoint() |
57 | { |
58 | } |
59 | |
60 | void PrismaticJoint::init(b2PrismaticJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) |
61 | { |
62 | def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA)), b2Vec2(ax,ay)); |
63 | def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); |
64 | def.lowerTranslation = 0.0f; |
65 | def.upperTranslation = 100.0f; |
66 | def.enableLimit = true; |
67 | def.collideConnected = collideConnected; |
68 | } |
69 | |
70 | float PrismaticJoint::getJointTranslation() const |
71 | { |
72 | return Physics::scaleUp(joint->GetJointTranslation()); |
73 | } |
74 | |
75 | float PrismaticJoint::getJointSpeed() const |
76 | { |
77 | return Physics::scaleUp(joint->GetJointSpeed()); |
78 | } |
79 | |
80 | void PrismaticJoint::setMotorEnabled(bool enable) |
81 | { |
82 | return joint->EnableMotor(enable); |
83 | } |
84 | |
85 | bool PrismaticJoint::isMotorEnabled() const |
86 | { |
87 | return joint->IsMotorEnabled(); |
88 | } |
89 | |
90 | void PrismaticJoint::setMaxMotorForce(float force) |
91 | { |
92 | joint->SetMaxMotorForce(Physics::scaleDown(force)); |
93 | } |
94 | |
95 | void PrismaticJoint::setMotorSpeed(float speed) |
96 | { |
97 | joint->SetMotorSpeed(Physics::scaleDown(speed)); |
98 | } |
99 | |
100 | float PrismaticJoint::getMotorSpeed() const |
101 | { |
102 | return Physics::scaleUp(joint->GetMotorSpeed()); |
103 | } |
104 | |
105 | float PrismaticJoint::getMotorForce(float inv_dt) const |
106 | { |
107 | return Physics::scaleUp(joint->GetMotorForce(inv_dt)); |
108 | } |
109 | |
110 | float PrismaticJoint::getMaxMotorForce() const |
111 | { |
112 | return Physics::scaleUp(joint->GetMaxMotorForce()); |
113 | } |
114 | |
115 | void PrismaticJoint::setLimitsEnabled(bool enable) |
116 | { |
117 | joint->EnableLimit(enable); |
118 | } |
119 | |
120 | bool PrismaticJoint::areLimitsEnabled() const |
121 | { |
122 | return joint->IsLimitEnabled(); |
123 | } |
124 | |
125 | void PrismaticJoint::setUpperLimit(float limit) |
126 | { |
127 | joint->SetLimits(joint->GetLowerLimit(), Physics::scaleDown(limit)); |
128 | } |
129 | |
130 | void PrismaticJoint::setLowerLimit(float limit) |
131 | { |
132 | joint->SetLimits(Physics::scaleDown(limit), joint->GetUpperLimit()); |
133 | } |
134 | |
135 | void PrismaticJoint::setLimits(float lower, float upper) |
136 | { |
137 | joint->SetLimits(Physics::scaleDown(lower), Physics::scaleDown(upper)); |
138 | } |
139 | |
140 | float PrismaticJoint::getLowerLimit() const |
141 | { |
142 | return Physics::scaleUp(joint->GetLowerLimit()); |
143 | } |
144 | |
145 | float PrismaticJoint::getUpperLimit() const |
146 | { |
147 | return Physics::scaleUp(joint->GetUpperLimit()); |
148 | } |
149 | |
150 | int PrismaticJoint::getLimits(lua_State *L) |
151 | { |
152 | lua_pushnumber(L, Physics::scaleUp(joint->GetLowerLimit())); |
153 | lua_pushnumber(L, Physics::scaleUp(joint->GetUpperLimit())); |
154 | return 2; |
155 | } |
156 | |
157 | int PrismaticJoint::getAxis(lua_State *L) |
158 | { |
159 | b2Vec2 axis = joint->GetLocalAxisA(); |
160 | getBodyA()->getWorldVector(axis.x, axis.y, axis.x, axis.y); |
161 | lua_pushnumber(L, axis.x); |
162 | lua_pushnumber(L, axis.y); |
163 | return 2; |
164 | } |
165 | |
166 | float PrismaticJoint::getReferenceAngle() const |
167 | { |
168 | return joint->GetReferenceAngle(); |
169 | } |
170 | |
171 | } // box2d |
172 | } // physics |
173 | } // love |
174 | |