Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
chipmunk_private.h
1/* Copyright (c) 2025 Victor Blomqvist
2 * Copyright (c) 2007-2024 Scott Lembcke and Howling Moon Software
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
23#ifndef CHIPMUNK_PRIVATE_H
24#define CHIPMUNK_PRIVATE_H
25
26#include "chipmunk/chipmunk.h"
27#include "chipmunk/chipmunk_structs.h"
28
29
30#define CP_HASH_COEF_A (3344921057ul)
31#define CP_HASH_COEF_B (1566083941ul)
32// Hash to single value. CP_HASH_PAIR(A, B) == CP_HASH_PAIR(B, A)
33#define CP_HASH_PAIR(A, B) ((cpHashValue)(A)*CP_HASH_COEF_A ^ (cpHashValue)(B)*CP_HASH_COEF_A)
34// Hash to single value. CP_HASH_PAIR(A, B) != CP_HASH_PAIR(B, A)
35#define CP_HASH_PAIR_ORDERED(A, B) ((cpHashValue)(A)*CP_HASH_COEF_A ^ (cpHashValue)(B)*CP_HASH_COEF_B)
36
37// TODO: Eww. Magic numbers.
38#define MAGIC_EPSILON 1e-5
39
40
41//MARK: cpArray
42
43cpArray *cpArrayNew(int size);
44
45void cpArrayFree(cpArray *arr);
46
47void cpArrayPush(cpArray *arr, void *object);
48void *cpArrayPop(cpArray *arr);
49void cpArrayDeleteObj(cpArray *arr, void *obj);
50cpBool cpArrayContains(cpArray *arr, void *ptr);
51
52void cpArrayFreeEach(cpArray *arr, void (freeFunc)(void*));
53
54
55//MARK: cpHashSet
56
57typedef cpBool (*cpHashSetEqlFunc)(const void *ptr, const void *elt);
58typedef void *(*cpHashSetTransFunc)(const void *ptr, void *data);
59
60cpHashSet *cpHashSetNew(int size, cpHashSetEqlFunc eqlFunc);
61void cpHashSetSetDefaultValue(cpHashSet *set, void *default_value);
62
63void cpHashSetFree(cpHashSet *set);
64
65int cpHashSetCount(cpHashSet *set);
66const void *cpHashSetInsert(cpHashSet *set, cpHashValue hash, const void *ptr, cpHashSetTransFunc trans, void *data);
67const void *cpHashSetRemove(cpHashSet *set, cpHashValue hash, const void *ptr);
68const void *cpHashSetFind(cpHashSet *set, cpHashValue hash, const void *ptr);
69
70typedef void (*cpHashSetIteratorFunc)(void *elt, void *data);
71void cpHashSetEach(cpHashSet *set, cpHashSetIteratorFunc func, void *data);
72
73typedef cpBool (*cpHashSetFilterFunc)(void *elt, void *data);
74void cpHashSetFilter(cpHashSet *set, cpHashSetFilterFunc func, void *data);
75
76
77//MARK: Bodies
78
79void cpBodyAddShape(cpBody *body, cpShape *shape);
80void cpBodyRemoveShape(cpBody *body, cpShape *shape);
81
82//void cpBodyAccumulateMassForShape(cpBody *body, cpShape *shape);
83void cpBodyAccumulateMassFromShapes(cpBody *body);
84
85void cpBodyRemoveConstraint(cpBody *body, cpConstraint *constraint);
86
87
88//MARK: Spatial Index Functions
89
90cpSpatialIndex *cpSpatialIndexInit(cpSpatialIndex *index, cpSpatialIndexClass *klass, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex *staticIndex);
91
92
93//MARK: Arbiters
94
95cpArbiter* cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b);
96
97static inline struct cpArbiterThread *
98cpArbiterThreadForBody(cpArbiter *arb, cpBody *body)
99{
100 return (arb->body_a == body ? &arb->thread_a : &arb->thread_b);
101}
102
103void cpArbiterUnthread(cpArbiter *arb);
104
105void cpArbiterUpdate(cpArbiter *arb, struct cpCollisionInfo *info, cpSpace *space);
106void cpArbiterPreStep(cpArbiter *arb, cpFloat dt, cpFloat bias, cpFloat slop);
107void cpArbiterApplyCachedImpulse(cpArbiter *arb, cpFloat dt_coef);
108void cpArbiterApplyImpulse(cpArbiter *arb);
109
110
111//MARK: Shapes/Collisions
112
113cpShape *cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body, struct cpShapeMassInfo massInfo);
114
115static inline cpBool
116cpShapeActive(cpShape *shape)
117{
118 // checks if the shape is added to a shape list.
119 // TODO could this just check the space now?
120 return (shape->prev || (shape->body && shape->body->shapeList == shape));
121}
122
123// Note: This function returns contact points with r1/r2 in absolute coordinates, not body relative.
124struct cpCollisionInfo cpCollide(const cpShape *a, const cpShape *b, cpCollisionID id, struct cpContact *contacts);
125
126static inline void
127CircleSegmentQuery(cpShape *shape, cpVect center, cpFloat r1, cpVect a, cpVect b, cpFloat r2, cpSegmentQueryInfo *info)
128{
129 cpVect da = cpvsub(a, center);
130 cpVect db = cpvsub(b, center);
131 cpFloat rsum = r1 + r2;
132
133 cpFloat qa = cpvdot(da, da) - 2.0f*cpvdot(da, db) + cpvdot(db, db);
134 cpFloat qb = cpvdot(da, db) - cpvdot(da, da);
135 cpFloat det = qb*qb - qa*(cpvdot(da, da) - rsum*rsum);
136
137 if(det >= 0.0f){
138 cpFloat t = (-qb - cpfsqrt(det))/(qa);
139 if(0.0f<= t && t <= 1.0f){
140 cpVect n = cpvnormalize(cpvlerp(da, db, t));
141
142 info->shape = shape;
143 info->point = cpvsub(cpvlerp(a, b, t), cpvmult(n, r2));
144 info->normal = n;
145 info->alpha = t;
146 }
147 }
148}
149
150static inline cpBool
151cpShapeFilterReject(cpShapeFilter a, cpShapeFilter b)
152{
153 // Reject the collision if:
154 return (
155 // They are in the same non-zero group.
156 (a.group != 0 && a.group == b.group) ||
157 // One of the category/mask combinations fails.
158 (a.categories & b.mask) == 0 ||
159 (b.categories & a.mask) == 0
160 );
161}
162
163void cpLoopIndexes(const cpVect *verts, int count, int *start, int *end);
164
165
166//MARK: Constraints
167// TODO naming conventions here
168
169void cpConstraintInit(cpConstraint *constraint, const struct cpConstraintClass *klass, cpBody *a, cpBody *b);
170
171static inline void
172cpConstraintActivateBodies(cpConstraint *constraint)
173{
174 cpBody *a = constraint->a; cpBodyActivate(a);
175 cpBody *b = constraint->b; cpBodyActivate(b);
176}
177
178static inline cpVect
179relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2){
180 cpVect v1_sum = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
181 cpVect v2_sum = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
182
183 return cpvsub(v2_sum, v1_sum);
184}
185
186static inline cpFloat
187normal_relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n){
188 return cpvdot(relative_velocity(a, b, r1, r2), n);
189}
190
191static inline void
192apply_impulse(cpBody *body, cpVect j, cpVect r){
193 body->v = cpvadd(body->v, cpvmult(j, body->m_inv));
194 body->w += body->i_inv*cpvcross(r, j);
195}
196
197static inline void
198apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
199{
200 apply_impulse(a, cpvneg(j), r1);
201 apply_impulse(b, j, r2);
202}
203
204static inline void
205apply_bias_impulse(cpBody *body, cpVect j, cpVect r)
206{
207 body->v_bias = cpvadd(body->v_bias, cpvmult(j, body->m_inv));
208 body->w_bias += body->i_inv*cpvcross(r, j);
209}
210
211static inline void
212apply_bias_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
213{
214 apply_bias_impulse(a, cpvneg(j), r1);
215 apply_bias_impulse(b, j, r2);
216}
217
218static inline cpFloat
219k_scalar_body(cpBody *body, cpVect r, cpVect n)
220{
221 cpFloat rcn = cpvcross(r, n);
222 return body->m_inv + body->i_inv*rcn*rcn;
223}
224
225static inline cpFloat
226k_scalar(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n)
227{
228 cpFloat value = k_scalar_body(a, r1, n) + k_scalar_body(b, r2, n);
229 cpAssertSoft(value != 0.0, "Unsolvable collision or constraint.");
230
231 return value;
232}
233
234static inline cpMat2x2
235k_tensor(cpBody *a, cpBody *b, cpVect r1, cpVect r2)
236{
237 cpFloat m_sum = a->m_inv + b->m_inv;
238
239 // start with Identity*m_sum
240 cpFloat k11 = m_sum, k12 = 0.0f;
241 cpFloat k21 = 0.0f, k22 = m_sum;
242
243 // add the influence from r1
244 cpFloat a_i_inv = a->i_inv;
245 cpFloat r1xsq = r1.x * r1.x * a_i_inv;
246 cpFloat r1ysq = r1.y * r1.y * a_i_inv;
247 cpFloat r1nxy = -r1.x * r1.y * a_i_inv;
248 k11 += r1ysq; k12 += r1nxy;
249 k21 += r1nxy; k22 += r1xsq;
250
251 // add the influnce from r2
252 cpFloat b_i_inv = b->i_inv;
253 cpFloat r2xsq = r2.x * r2.x * b_i_inv;
254 cpFloat r2ysq = r2.y * r2.y * b_i_inv;
255 cpFloat r2nxy = -r2.x * r2.y * b_i_inv;
256 k11 += r2ysq; k12 += r2nxy;
257 k21 += r2nxy; k22 += r2xsq;
258
259 // invert
260 cpFloat det = k11*k22 - k12*k21;
261 cpAssertSoft(det != 0.0, "Unsolvable constraint.");
262
263 cpFloat det_inv = 1.0f/det;
264 return cpMat2x2New(
265 k22*det_inv, -k12*det_inv,
266 -k21*det_inv, k11*det_inv
267 );
268}
269
270static inline cpFloat
271bias_coef(cpFloat errorBias, cpFloat dt)
272{
273 return 1.0f - cpfpow(errorBias, dt);
274}
275
276
277//MARK: Spaces
278
279#define cpAssertSpaceUnlocked(space) \
280 cpAssertHard(!space->locked, \
281 "This operation cannot be done safely during a call to cpSpaceStep() or during a query. " \
282 "Put these calls into a post-step callback." \
283 );
284
285void cpSpaceSetStaticBody(cpSpace *space, cpBody *body);
286
287extern cpCollisionHandler cpCollisionHandlerDoNothing;
288
289void cpSpaceProcessComponents(cpSpace *space, cpFloat dt);
290
291void cpSpacePushFreshContactBuffer(cpSpace *space);
292struct cpContact *cpContactBufferGetArray(cpSpace *space);
293void cpSpacePushContacts(cpSpace *space, int count);
294
295cpPostStepCallback *cpSpaceGetPostStepCallback(cpSpace *space, void *key);
296
297cpBool cpSpaceArbiterSetFilter(cpArbiter *arb, cpSpace *space);
298void cpSpaceFilterArbiters(cpSpace *space, cpBody *body, cpShape *filter);
299
300void cpSpaceActivateBody(cpSpace *space, cpBody *body);
301void cpSpaceLock(cpSpace *space);
302void cpSpaceUnlock(cpSpace *space, cpBool runPostStep);
303
304static inline void
305cpSpaceUncacheArbiter(cpSpace *space, cpArbiter *arb)
306{
307 const cpShape *a = arb->a, *b = arb->b;
308 const cpShape *shape_pair[] = {a, b};
309 cpHashValue arbHashID = CP_HASH_PAIR((cpHashValue)a, (cpHashValue)b);
310 cpHashSetRemove(space->cachedArbiters, arbHashID, shape_pair);
311 cpArrayDeleteObj(space->arbiters, arb);
312}
313
314static inline cpArray *
315cpSpaceArrayForBodyType(cpSpace *space, cpBodyType type)
316{
317 return (type == CP_BODY_TYPE_STATIC ? space->staticBodies : space->dynamicBodies);
318}
319
320void cpShapeUpdateFunc(cpShape *shape, void *unused);
321cpCollisionID cpSpaceCollideShapes(cpShape *a, cpShape *b, cpCollisionID id, cpSpace *space);
322
323
324//MARK: Foreach loops
325
326static inline cpConstraint *
327cpConstraintNext(cpConstraint *node, cpBody *body)
328{
329 return (node->a == body ? node->next_a : node->next_b);
330}
331
332#define CP_BODY_FOREACH_CONSTRAINT(bdy, var)\
333 for(cpConstraint *var = bdy->constraintList; var; var = cpConstraintNext(var, bdy))
334
335static inline cpArbiter *
336cpArbiterNext(cpArbiter *node, cpBody *body)
337{
338 return (node->body_a == body ? node->thread_a.next : node->thread_b.next);
339}
340
341#define CP_BODY_FOREACH_ARBITER(bdy, var)\
342 for(cpArbiter *var = bdy->arbiterList; var; var = cpArbiterNext(var, bdy))
343
344#define CP_BODY_FOREACH_SHAPE(body, var)\
345 for(cpShape *var = body->shapeList; var; var = var->next)
346
347#define CP_BODY_FOREACH_COMPONENT(root, var)\
348 for(cpBody *var = root; var; var = var->sleeping.next)
349
350#endif
uint32_t cpCollisionID
Type used internally to cache colliding object info for cpCollideShapes().
Definition chipmunk_types.h:172
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
uintptr_t cpHashValue
Hash value type.
Definition chipmunk_types.h:167
CP_EXPORT void cpBodyActivate(cpBody *body)
Wake up a sleeping or idle body.
cpBodyType
Definition cpBody.h:29
@ CP_BODY_TYPE_STATIC
A static body is a body that never (or rarely) moves.
Definition cpBody.h:41
cpBB(* cpSpatialIndexBBFunc)(void *obj)
Spatial index bounding box callback function type.
Definition cpSpatialIndex.h:46
static cpVect cpvnormalize(const cpVect v)
Returns a normalized copy of v.
Definition cpVect.h:147
static cpVect cpvadd(const cpVect v1, const cpVect v2)
Add two vectors.
Definition cpVect.h:49
static cpFloat cpvdot(const cpVect v1, const cpVect v2)
Vector dot product.
Definition cpVect.h:73
static cpVect cpvmult(const cpVect v, const cpFloat s)
Scalar multiplication.
Definition cpVect.h:67
static cpVect cpvneg(const cpVect v)
Negate a vector.
Definition cpVect.h:61
static cpVect cpvlerp(const cpVect v1, const cpVect v2, const cpFloat t)
Linearly interpolate between v1 and v2.
Definition cpVect.h:141
static cpVect cpvperp(const cpVect v)
Returns a perpendicular vector. (90 degree rotation)
Definition cpVect.h:87
static cpFloat cpvcross(const cpVect v1, const cpVect v2)
2D vector cross product analog.
Definition cpVect.h:81
static cpVect cpvsub(const cpVect v1, const cpVect v2)
Subtract two vectors.
Definition cpVect.h:55
Definition chipmunk_structs.h:124
Definition chipmunk_structs.h:97
Definition chipmunk_structs.h:31
Definition chipmunk_structs.h:36
Struct that holds function callback pointers to configure custom collision handling.
Definition cpSpace.h:42
Definition chipmunk_structs.h:113
Definition chipmunk_structs.h:244
Definition chipmunk_structs.h:251
Definition chipmunk_structs.h:101
Definition chipmunk_types.h:264
Definition chipmunk_structs.h:444
Segment query info struct.
Definition cpShape.h:41
const cpShape * shape
The shape that was hit, or NULL if no collision occured.
Definition cpShape.h:43
cpVect normal
The normal of the surface hit.
Definition cpShape.h:47
cpVect point
The point of impact.
Definition cpShape.h:45
cpFloat alpha
The normalized distance along the query segment in the range [0, 1].
Definition cpShape.h:49
Definition chipmunk_structs.h:169
Fast collision filtering type that is used to determine if two objects collide before calling collisi...
Definition cpShape.h:53
cpBitmask mask
A bitmask of user definable category types that this object object collides with.
Definition cpShape.h:62
cpGroup group
Two objects with the same non-zero group value do not collide.
Definition cpShape.h:56
cpBitmask categories
A bitmask of user definable categories that this object belongs to.
Definition cpShape.h:59
Definition chipmunk_structs.h:178
Definition chipmunk_structs.h:148
Definition chipmunk_structs.h:397
Definition cpSpatialIndex.h:134
Definition chipmunk_types.h:251