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 "MouseJoint.h"
22
23// Module
24#include "Body.h"
25#include "World.h"
26#include "Physics.h"
27
28#include <float.h>
29
30namespace love
31{
32namespace physics
33{
34namespace box2d
35{
36
37love::Type MouseJoint::type("MouseJoint", &Joint::type);
38
39MouseJoint::MouseJoint(Body *body1, float x, float y)
40 : Joint(body1)
41 , joint(NULL)
42{
43 if (body1->getType() == Body::BODY_KINEMATIC)
44 throw love::Exception("Cannot attach a MouseJoint to a kinematic body");
45
46 b2MouseJointDef def;
47
48 def.bodyA = body1->world->getGroundBody();
49 def.bodyB = body1->body;
50 def.maxForce = 1000.0f * body1->body->GetMass();
51 def.target = Physics::scaleDown(b2Vec2(x,y));
52 joint = (b2MouseJoint *)createJoint(&def);
53}
54
55MouseJoint::~MouseJoint()
56{
57}
58
59void MouseJoint::setTarget(float x, float y)
60{
61 joint->SetTarget(Physics::scaleDown(b2Vec2(x, y)));
62}
63
64int MouseJoint::getTarget(lua_State *L)
65{
66 lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().x));
67 lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().y));
68 return 2;
69}
70
71void MouseJoint::setMaxForce(float force)
72{
73 joint->SetMaxForce(Physics::scaleDown(force));
74}
75
76float MouseJoint::getMaxForce() const
77{
78 return Physics::scaleUp(joint->GetMaxForce());
79}
80
81void MouseJoint::setFrequency(float hz)
82{
83 // This is kind of a crappy check. The frequency is used in an internal
84 // box2d calculation whose result must be > FLT_EPSILON, but other variables
85 // go into that calculation...
86 if (hz <= FLT_EPSILON * 2)
87 throw love::Exception("MouseJoint frequency must be a positive number.");
88
89 joint->SetFrequency(hz);
90}
91
92float MouseJoint::getFrequency() const
93{
94 return joint->GetFrequency();
95}
96
97void MouseJoint::setDampingRatio(float d)
98{
99 joint->SetDampingRatio(d);
100}
101
102float MouseJoint::getDampingRatio() const
103{
104 return joint->GetDampingRatio();
105}
106
107Body *MouseJoint::getBodyA() const
108{
109 return Joint::getBodyB();
110}
111
112Body *MouseJoint::getBodyB() const
113{
114 return nullptr;
115}
116
117} // box2d
118} // physics
119} // love
120