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 "WheelJoint.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 WheelJoint::type("WheelJoint", &Joint::type);
36
37WheelJoint::WheelJoint(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 b2WheelJointDef def;
42
43 def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA)), b2Vec2(ax,ay));
44 def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB)));
45 def.collideConnected = collideConnected;
46 joint = (b2WheelJoint *)createJoint(&def);
47}
48
49WheelJoint::~WheelJoint()
50{
51}
52
53float WheelJoint::getJointTranslation() const
54{
55 return Physics::scaleUp(joint->GetJointTranslation());
56}
57
58float WheelJoint::getJointSpeed() const
59{
60 return Physics::scaleUp(joint->GetJointSpeed());
61}
62
63void WheelJoint::setMotorEnabled(bool enable)
64{
65 return joint->EnableMotor(enable);
66}
67
68bool WheelJoint::isMotorEnabled() const
69{
70 return joint->IsMotorEnabled();
71}
72
73void WheelJoint::setMotorSpeed(float speed)
74{
75 joint->SetMotorSpeed(speed);
76}
77
78float WheelJoint::getMotorSpeed() const
79{
80 return joint->GetMotorSpeed();
81}
82
83void WheelJoint::setMaxMotorTorque(float torque)
84{
85 joint->SetMaxMotorTorque(Physics::scaleDown(Physics::scaleDown(torque)));
86}
87
88float WheelJoint::getMaxMotorTorque() const
89{
90 return Physics::scaleUp(Physics::scaleUp(joint->GetMaxMotorTorque()));
91}
92
93float WheelJoint::getMotorTorque(float inv_dt) const
94{
95 return Physics::scaleUp(Physics::scaleUp(joint->GetMotorTorque(inv_dt)));
96}
97
98void WheelJoint::setSpringFrequency(float hz)
99{
100 joint->SetSpringFrequencyHz(hz);
101}
102
103float WheelJoint::getSpringFrequency() const
104{
105 return joint->GetSpringFrequencyHz();
106}
107
108void WheelJoint::setSpringDampingRatio(float ratio)
109{
110 joint->SetSpringDampingRatio(ratio);
111}
112
113float WheelJoint::getSpringDampingRatio() const
114{
115 return joint->GetSpringDampingRatio();
116}
117
118int WheelJoint::getAxis(lua_State *L)
119{
120 b2Vec2 axis = joint->GetLocalAxisA();
121 getBodyA()->getWorldVector(axis.x, axis.y, axis.x, axis.y);
122 lua_pushnumber(L, axis.x);
123 lua_pushnumber(L, axis.y);
124 return 2;
125}
126
127} // box2d
128} // physics
129} // love
130