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#ifndef LOVE_PHYSICS_BOX2D_PHYSICS_H
22#define LOVE_PHYSICS_BOX2D_PHYSICS_H
23
24// LOVE
25#include "common/Module.h"
26#include "World.h"
27#include "Contact.h"
28#include "Body.h"
29#include "Fixture.h"
30#include "Shape.h"
31#include "CircleShape.h"
32#include "PolygonShape.h"
33#include "EdgeShape.h"
34#include "ChainShape.h"
35#include "Joint.h"
36#include "MouseJoint.h"
37#include "DistanceJoint.h"
38#include "PrismaticJoint.h"
39#include "RevoluteJoint.h"
40#include "PulleyJoint.h"
41#include "GearJoint.h"
42#include "FrictionJoint.h"
43#include "WeldJoint.h"
44#include "WheelJoint.h"
45#include "RopeJoint.h"
46#include "MotorJoint.h"
47
48namespace love
49{
50namespace physics
51{
52namespace box2d
53{
54
55class Physics : public Module
56{
57public:
58
59 /**
60 * 30 pixels in one meter by default.
61 **/
62 static const int DEFAULT_METER = 30;
63
64 Physics();
65 virtual ~Physics();
66
67 // Implements Module.
68 const char *getName() const;
69 virtual ModuleType getModuleType() const { return M_PHYSICS; }
70
71 /**
72 * Creates a new World.
73 * @param gx Gravity along x-axis.
74 * @param gy Gravity along y-axis.
75 * @param sleep Whether the World allows sleep.
76 **/
77 World *newWorld(float gx, float gy, bool sleep);
78
79 /**
80 * Creates a new Body at the specified position.
81 * @param world The world to create the Body in.
82 * @param x The position along the x-axis.
83 * @param y The position along the y-axis.
84 * @param type The type of body to create.
85 **/
86 Body *newBody(World *world, float x, float y, Body::Type type);
87
88 /**
89 * Creates a new Body at (0, 0)
90 * @param world The world to create the Body in.
91 * @param type The type of Body to create.
92 **/
93 Body *newBody(World *world, Body::Type type);
94
95 /**
96 * Creates a new CircleShape at (0, 0).
97 * @param radius The radius of the circle.
98 **/
99 CircleShape *newCircleShape(float radius);
100
101 /**
102 * Creates a new CircleShape at (x,y) in local coordinates.
103 * @param x The offset along the x-axis.
104 * @param y The offset along the y-axis.
105 * @param radius The radius of the circle.
106 **/
107 CircleShape *newCircleShape(float x, float y, float radius);
108
109 /**
110 * Shorthand for creating rectangular PolygonShapes. The rectangle
111 * will be created at the local origin.
112 * @param w The width of the rectangle.
113 * @param h The height of the rectangle.
114 **/
115 PolygonShape *newRectangleShape(float w, float h);
116
117 /**
118 * Shorthand for creating rectangular PolygonShapes. The rectangle
119 * will be created at (x,y) in local coordinates.
120 * @param x The offset along the x-axis.
121 * @param y The offset along the y-axis.
122 * @param w The width of the rectangle.
123 * @param h The height of the rectangle.
124 **/
125 PolygonShape *newRectangleShape(float x, float y, float w, float h);
126
127 /**
128 * Shorthand for creating rectangular PolygonShapes. The rectangle
129 * will be created at (x,y) in local coordinates.
130 * @param x The offset along the x-axis.
131 * @param y The offset along the y-axis.
132 * @param w The width of the rectangle.
133 * @param h The height of the rectangle.
134 * @param angle The angle of the rectangle. (rad)
135 **/
136 PolygonShape *newRectangleShape(float x, float y, float w, float h, float angle);
137
138 /**
139 * Creates a new EdgeShape. The edge will be created from
140 * (x1,y1) to (x2,y2) in local coordinates.
141 * @param x1 The x coordinate of the first point.
142 * @param y1 The y coordinate of the first point.
143 * @param x2 The x coordinate of the second point.
144 * @param y2 The y coordinate of the second point.
145 **/
146 EdgeShape *newEdgeShape(float x1, float y1, float x2, float y2);
147
148 /**
149 * Creates a new PolygonShape from a variable number of vertices.
150 **/
151 int newPolygonShape(lua_State *L);
152
153 /**
154 * Creates a new ChainShape from a variable number of vertices.
155 **/
156 int newChainShape(lua_State *L);
157
158 /**
159 * Creates a new DistanceJoint connecting body1 with body2.
160 * @param x1 Anchor1 along the x-axis. (World coordinates)
161 * @param y1 Anchor1 along the y-axis. (World coordinates)
162 * @param x2 Anchor2 along the x-axis. (World coordinates)
163 * @param y2 Anchor2 along the y-axis. (World coordinates)
164 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
165 **/
166 DistanceJoint *newDistanceJoint(Body *body1, Body *body2, float x1, float y1, float x2, float y2, bool collideConnected);
167
168 /**
169 * Creates a new MouseJoint connecting the body with an arbitrary point.
170 * @param x Anchor along the x-axis. (World coordinates)
171 * @param y Anchor along the y-axis. (World coordinates)
172 **/
173 MouseJoint *newMouseJoint(Body *body, float x, float y);
174
175 /**
176 * Creates a new RevoluteJoint connecting body1 with body2.
177 * @param xA Anchor for body 1 along the x-axis. (World coordinates)
178 * @param yA Anchor for body 1 along the y-axis. (World coordinates)
179 * @param xB Anchor for body 2 along the x-axis. (World coordinates)
180 * @param yB Anchor for body 2 along the y-axis. (World coordinates)
181 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
182 * @param referenceAngle The reference angle.
183 **/
184 RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected);
185
186 RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle);
187
188 /**
189 * Creates a new PrismaticJoint connecting body1 with body2.
190 * @param xA World-anchor for body1 along the x-axis.
191 * @param yA World-anchor for body1 along the y-axis.
192 * @param xB World-anchor for body2 along the x-axis.
193 * @param yB World-anchor for body2 along the y-axis.
194 * @param ax The x-component of the world-axis.
195 * @param ay The y-component of the world-axis.
196 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
197 * @param referenceAngle The reference angle.
198 **/
199 PrismaticJoint *newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected);
200
201 PrismaticJoint *newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle);
202
203 /**
204 * Creates a new PulleyJoint connecting body1 with body2.
205 * @param groundAnchor1 World ground-anchor for body1.
206 * @param groundAnchor2 World ground-anchor for body2.
207 * @param anchor1 World anchor on body1.
208 * @param anchor2 World anchor on body2.
209 * @param ratio The pulley ratio.
210 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to true.
211 **/
212 PulleyJoint *newPulleyJoint(Body *body1, Body *body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio, bool collideConnected);
213
214 /**
215 * Creates a new GearJoint connecting joint1 with joint2.
216 * @param joint1 The first joint.
217 * @param joint2 The second joint.
218 * @param ratio The gear ratio.
219 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
220 **/
221 GearJoint *newGearJoint(Joint *joint1, Joint *joint2, float ratio, bool collideConnected);
222
223 /**
224 * Creates a new FrictionJoint connecting body1 with body2.
225 * @param xA Anchor for body 1 along the x-axis. (World coordinates)
226 * @param yA Anchor for body 1 along the y-axis. (World coordinates)
227 * @param xB Anchor for body 2 along the x-axis. (World coordinates)
228 * @param yB Anchor for body 2 along the y-axis. (World coordinates)
229 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
230 **/
231 FrictionJoint *newFrictionJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected);
232
233 /**
234 * Creates a new WeldJoint connecting body1 with body2.
235 * @param xA Anchor for body 1 along the x-axis. (World coordinates)
236 * @param yA Anchor for body 1 along the y-axis. (World coordinates)
237 * @param xB Anchor for body 2 along the x-axis. (World coordinates)
238 * @param yB Anchor for body 2 along the y-axis. (World coordinates)
239 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
240 * @param referenceAngle The reference angle.
241 **/
242 WeldJoint *newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected);
243
244 WeldJoint *newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle);
245
246 /**
247 * Creates a new WheelJoint connecting body1 with body2.
248 * @param xA Anchor for body 1 along the x-axis. (World coordinates)
249 * @param yA Anchor for body 1 along the y-axis. (World coordinates)
250 * @param xB Anchor for body 2 along the x-axis. (World coordinates)
251 * @param yB Anchor for body 2 along the y-axis. (World coordinates)
252 * @param ax The x-component of the world-axis.
253 * @param ay The y-component of the world-axis.
254 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
255 **/
256 WheelJoint *newWheelJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected);
257
258 /**
259 * Creates a new RopeJoint connecting body1 with body2.
260 * @param x1 Anchor1 along the x-axis. (Local coordinates)
261 * @param y1 Anchor1 along the y-axis. (Local coordinates)
262 * @param x2 Anchor2 along the x-axis. (Local coordinates)
263 * @param y2 Anchor2 along the y-axis. (Local coordinates)
264 * @param maxLength The maximum distance for the bodies.
265 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
266 **/
267 RopeJoint *newRopeJoint(Body *body1, Body *body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected);
268
269 /**
270 * Creates a new MotorJoint controlling the relative motion between body1
271 * and body2.
272 **/
273 MotorJoint *newMotorJoint(Body *body1, Body *body2);
274 MotorJoint *newMotorJoint(Body *body1, Body *body2, float correctionFactor, bool collideConnected);
275
276 /**
277 * Creates a new Fixture attaching shape to body.
278 * @param body The body to attach the Fixture to.
279 * @param shape The shape to attach to the Fixture,
280 * @param density The density of the Fixture.
281 **/
282
283 Fixture *newFixture(Body *body, Shape *shape, float density);
284
285 /**
286 * Calculates the distance between two Fixtures.
287 * @param fixtureA The first Fixture.
288 * @param fixtureB The sceond Fixture.
289 * @return The distance between them, and the two points closest
290 * to each other.
291 **/
292 int getDistance(lua_State *L);
293
294 /**
295 * Sets the number of pixels in one meter.
296 * @param scale The number of pixels in one meter. (1m ~= 3.3ft).
297 **/
298 static void setMeter(float scale);
299
300 /**
301 * Gets the number of pixels in one meter.
302 * @return The number of pixels in one meter. (1m ~= 3.3ft).
303 **/
304 static float getMeter();
305
306 /**
307 * Scales a value down according to the current meter in pixels.
308 * @param f The unscaled input value.
309 **/
310 static float scaleDown(float f);
311
312 /**
313 * Scales a value up according to the current meter in pixels.
314 * @param f The unscaled input value.
315 **/
316 static float scaleUp(float f);
317
318 /**
319 * Scales a point down according to the current meter
320 * in pixels, for instance x = x0/meter, y = x0/meter.
321 * @param x The x-coordinate of the point to scale.
322 * @param y The y-coordinate of the point to scale.
323 **/
324 static void scaleDown(float &x, float &y);
325
326 /**
327 * Scales a point up according to the current meter
328 * in pixels, for instance x = x0/meter, y = x0/meter.
329 * @param x The x-coordinate of the point to scale.
330 * @param y The y-coordinate of the point to scale.
331 **/
332 static void scaleUp(float &x, float &y);
333
334 /**
335 * Scales a b2Vec2 down according to the current meter in pixels.
336 * @param v The unscaled input vector.
337 * @return The scaled vector.
338 **/
339 static b2Vec2 scaleDown(const b2Vec2 &v);
340
341 /**
342 * Scales a b2Vec up according to the current meter in pixels.
343 * @param v The unscaled input vector.
344 * @return The scaled vector.
345 **/
346 static b2Vec2 scaleUp(const b2Vec2 &v);
347
348 /**
349 * Scales a b2AABB down according to the current meter in pixels.
350 * @param aabb The unscaled input AABB.
351 * @return The scaled AABB.
352 **/
353 static b2AABB scaleDown(const b2AABB &aabb);
354
355 /**
356 * Scales a b2AABB up according to the current meter in pixels.
357 * @param aabb The unscaled input AABB.
358 * @return The scaled AABB.
359 **/
360 static b2AABB scaleUp(const b2AABB &aabb);
361
362private:
363
364 // The length of one meter in pixels.
365 static float meter;
366}; // Physics
367
368} // box2d
369} // physics
370} // love
371
372#endif // LOVE_PHYSICS_BOX2D_PHYSICS_H
373