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_FIXTURE_H
22#define LOVE_PHYSICS_BOX2D_FIXTURE_H
23
24// LOVE
25#include "physics/Shape.h"
26#include "physics/box2d/Body.h"
27#include "physics/box2d/Shape.h"
28#include "common/Object.h"
29#include "common/Reference.h"
30
31// Box2D
32#include <Box2D/Box2D.h>
33
34namespace love
35{
36namespace physics
37{
38namespace box2d
39{
40
41class World;
42
43/**
44 * This struct is stored in a void pointer
45 * in the Box2D Fixture class. For now, all we
46 * need is a Lua reference to arbitrary data,
47 * but we might need more later.
48 **/
49struct fixtureudata
50{
51 // Reference to arbitrary data.
52 Reference *ref = nullptr;
53};
54
55/**
56 * A Fixture is used to attach a shape to a body for collision detection.
57 * A Fixture inherits its transform from its parent. Fixtures hold
58 * additional non-geometric data such as friction, collision filters,
59 * etc.
60 **/
61class Fixture : public Object
62{
63public:
64 friend class Physics;
65
66 static love::Type type;
67
68 /**
69 * Creates a Fixture.
70 **/
71 Fixture(Body *body, Shape *shape, float density);
72
73 virtual ~Fixture();
74
75 /**
76 * Gets the type of the Fixture's Shape. Useful for
77 * debug drawing.
78 **/
79 Shape::Type getType();
80
81 /**
82 * Gets the Shape attached to this Fixture.
83 **/
84 Shape *getShape();
85
86 /**
87 * Returns true if the fixture is active in a Box2D world.
88 **/
89 bool isValid() const;
90
91 /**
92 * Checks whether this Fixture acts as a sensor.
93 * @return True if sensor, false otherwise.
94 **/
95 bool isSensor() const;
96
97 /**
98 * Set whether this Fixture should be a sensor or not.
99 * @param sensor True if sensor, false if not.
100 **/
101 void setSensor(bool sensor);
102
103 /**
104 * Gets the Body this Fixture is attached to.
105 **/
106 Body *getBody() const;
107
108 /**
109 * Sets the filter data. An integer array is used even though the
110 * first two elements are unsigned shorts. The elements are:
111 * category (16-bits), mask (16-bits) and group (32-bits/int).
112 **/
113 void setFilterData(int *v);
114
115 /**
116 * Gets the filter data. An integer array is used even though the
117 * first two elements are unsigned shorts. The elements are:
118 * category (16-bits), mask (16-bits) and group (32-bits/int).
119 **/
120 void getFilterData(int *v);
121
122 /**
123 * This function stores an in-C reference to
124 * arbitrary Lua data in the Box2D Fixture object.
125 **/
126 int setUserData(lua_State *L);
127
128 /**
129 * Gets the data set with setData. If no
130 * data is set, nil is returned.
131 **/
132 int getUserData(lua_State *L);
133
134 /**
135 * Sets the friction of the Fixture.
136 * @param friction The new friction.
137 **/
138 void setFriction(float friction);
139
140 /**
141 * Sets the restitution for the Fixture.
142 * @param restitution The restitution.
143 **/
144 void setRestitution(float restitution);
145
146 /**
147 * Sets the density of the Fixture.
148 * @param density The density of the Fixture.
149 **/
150 void setDensity(float density);
151
152 /**
153 * Gets the friction of the Fixture.
154 * @returns The friction.
155 **/
156 float getFriction() const;
157
158 /**
159 * Gets the restitution of the Fixture.
160 * @return The restitution of the Fixture.
161 **/
162 float getRestitution() const;
163
164 /**
165 * Gets the density of the Fixture.
166 * @return The density.
167 **/
168 float getDensity() const;
169
170 /**
171 * Checks if a point is inside the Fixture.
172 * @param x The x-component of the point.
173 * @param y The y-component of the point.
174 **/
175 bool testPoint(float x, float y) const;
176
177 /**
178 * Cast a ray against this Fixture.
179 **/
180 int rayCast(lua_State *L) const;
181
182 void setGroupIndex(int index);
183 int getGroupIndex() const;
184
185 int setCategory(lua_State *L);
186 int setMask(lua_State *L);
187 int getCategory(lua_State *L);
188 int getMask(lua_State *L);
189 uint16 getBits(lua_State *L);
190 int pushBits(lua_State *L, uint16 bits);
191
192 /**
193 * Gets the bounding box for this Fixture.
194 * The function returns eight values which can be
195 * passed directly to love.graphics.polygon.
196 **/
197 int getBoundingBox(lua_State *L) const;
198
199 /**
200 * Gets the mass data for this Fixture.
201 * This operation may be expensive.
202 **/
203 int getMassData(lua_State *L) const;
204
205 /**
206 * Destroys this fixture.
207 **/
208 void destroy(bool implicit = false);
209
210protected:
211
212 void checkCreateShape();
213
214 Body *body;
215 fixtureudata *udata;
216 b2Fixture *fixture;
217
218 StrongRef<Shape> shape;
219
220};
221
222} // box2d
223} // physics
224} // love
225
226#endif // LOVE_PHYSICS_BOX2D_FIXTURE_H
227