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
28namespace love
29{
30namespace physics
31{
32namespace box2d
33{
34
35love::Type PrismaticJoint::type("PrismaticJoint", &Joint::type);
36
37PrismaticJoint::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
46PrismaticJoint::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
56PrismaticJoint::~PrismaticJoint()
57{
58}
59
60void 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
70float PrismaticJoint::getJointTranslation() const
71{
72 return Physics::scaleUp(joint->GetJointTranslation());
73}
74
75float PrismaticJoint::getJointSpeed() const
76{
77 return Physics::scaleUp(joint->GetJointSpeed());
78}
79
80void PrismaticJoint::setMotorEnabled(bool enable)
81{
82 return joint->EnableMotor(enable);
83}
84
85bool PrismaticJoint::isMotorEnabled() const
86{
87 return joint->IsMotorEnabled();
88}
89
90void PrismaticJoint::setMaxMotorForce(float force)
91{
92 joint->SetMaxMotorForce(Physics::scaleDown(force));
93}
94
95void PrismaticJoint::setMotorSpeed(float speed)
96{
97 joint->SetMotorSpeed(Physics::scaleDown(speed));
98}
99
100float PrismaticJoint::getMotorSpeed() const
101{
102 return Physics::scaleUp(joint->GetMotorSpeed());
103}
104
105float PrismaticJoint::getMotorForce(float inv_dt) const
106{
107 return Physics::scaleUp(joint->GetMotorForce(inv_dt));
108}
109
110float PrismaticJoint::getMaxMotorForce() const
111{
112 return Physics::scaleUp(joint->GetMaxMotorForce());
113}
114
115void PrismaticJoint::setLimitsEnabled(bool enable)
116{
117 joint->EnableLimit(enable);
118}
119
120bool PrismaticJoint::areLimitsEnabled() const
121{
122 return joint->IsLimitEnabled();
123}
124
125void PrismaticJoint::setUpperLimit(float limit)
126{
127 joint->SetLimits(joint->GetLowerLimit(), Physics::scaleDown(limit));
128}
129
130void PrismaticJoint::setLowerLimit(float limit)
131{
132 joint->SetLimits(Physics::scaleDown(limit), joint->GetUpperLimit());
133}
134
135void PrismaticJoint::setLimits(float lower, float upper)
136{
137 joint->SetLimits(Physics::scaleDown(lower), Physics::scaleDown(upper));
138}
139
140float PrismaticJoint::getLowerLimit() const
141{
142 return Physics::scaleUp(joint->GetLowerLimit());
143}
144
145float PrismaticJoint::getUpperLimit() const
146{
147 return Physics::scaleUp(joint->GetUpperLimit());
148}
149
150int 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
157int 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
166float PrismaticJoint::getReferenceAngle() const
167{
168 return joint->GetReferenceAngle();
169}
170
171} // box2d
172} // physics
173} // love
174