Chipmunk Pro API Reference  6.1.3
 All Classes Functions Variables Typedefs Properties Groups Pages
cpArbiter.h
1 /* Copyright (c) 2007 Scott Lembcke
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
27 
31 typedef cpBool (*cpCollisionBeginFunc)(cpArbiter *arb, cpSpace *space, void *data);
34 typedef cpBool (*cpCollisionPreSolveFunc)(cpArbiter *arb, cpSpace *space, void *data);
36 typedef void (*cpCollisionPostSolveFunc)(cpArbiter *arb, cpSpace *space, void *data);
38 typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, cpSpace *space, void *data);
39 
41 struct cpCollisionHandler {
45  cpCollisionPreSolveFunc preSolve;
46  cpCollisionPostSolveFunc postSolve;
47  cpCollisionSeparateFunc separate;
48  void *data;
49 };
50 
51 typedef struct cpContact cpContact;
52 
53 #define CP_MAX_CONTACTS_PER_ARBITER 4
54 
56 typedef enum cpArbiterState {
57  // Arbiter is active and its the first collision.
58  cpArbiterStateFirstColl,
59  // Arbiter is active and its not the first collision.
60  cpArbiterStateNormal,
61  // Collision has been explicitly ignored.
62  // Either by returning false from a begin collision handler or calling cpArbiterIgnore().
63  cpArbiterStateIgnore,
64  // Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
65  cpArbiterStateCached,
66 } cpArbiterState;
67 
69 struct cpArbiterThread {
70  // Links to next and previous arbiters in the contact graph.
71  struct cpArbiter *next, *prev;
72 };
73 
75 struct cpArbiter {
85 
90 
91  CP_PRIVATE(cpShape *a);
92  CP_PRIVATE(cpShape *b);
93  CP_PRIVATE(cpBody *body_a);
94  CP_PRIVATE(cpBody *body_b);
95 
96  CP_PRIVATE(struct cpArbiterThread thread_a);
97  CP_PRIVATE(struct cpArbiterThread thread_b);
98 
99  CP_PRIVATE(int numContacts);
100  CP_PRIVATE(cpContact *contacts);
101 
102  CP_PRIVATE(cpTimestamp stamp);
103  CP_PRIVATE(cpCollisionHandler *handler);
104  CP_PRIVATE(cpBool swappedColl);
105  CP_PRIVATE(cpArbiterState state);
106 };
107 
108 #define CP_DefineArbiterStructGetter(type, member, name) \
109 static inline type cpArbiterGet##name(const cpArbiter *arb){return arb->member;}
110 
111 #define CP_DefineArbiterStructSetter(type, member, name) \
112 static inline void cpArbiterSet##name(cpArbiter *arb, type value){arb->member = value;}
113 
114 #define CP_DefineArbiterStructProperty(type, member, name) \
115 CP_DefineArbiterStructGetter(type, member, name) \
116 CP_DefineArbiterStructSetter(type, member, name)
117 
118 CP_DefineArbiterStructProperty(cpFloat, e, Elasticity)
119 CP_DefineArbiterStructProperty(cpFloat, u, Friction)
120 CP_DefineArbiterStructProperty(cpVect, surface_vr, SurfaceVelocity)
121 CP_DefineArbiterStructProperty(cpDataPointer, data, UserData)
122 
125 cpVect cpArbiterTotalImpulse(const cpArbiter *arb);
131 cpFloat cpArbiterTotalKE(const cpArbiter *arb);
132 
133 
137 void cpArbiterIgnore(cpArbiter *arb);
138 
142 static inline void cpArbiterGetShapes(const cpArbiter *arb, cpShape **a, cpShape **b)
143 {
144  if(arb->CP_PRIVATE(swappedColl)){
145  (*a) = arb->CP_PRIVATE(b), (*b) = arb->CP_PRIVATE(a);
146  } else {
147  (*a) = arb->CP_PRIVATE(a), (*b) = arb->CP_PRIVATE(b);
148  }
149 }
151 #define CP_ARBITER_GET_SHAPES(arb, a, b) cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
152 
156 static inline void cpArbiterGetBodies(const cpArbiter *arb, cpBody **a, cpBody **b)
157 {
158  CP_ARBITER_GET_SHAPES(arb, shape_a, shape_b);
159  (*a) = shape_a->body;
160  (*b) = shape_b->body;
161 }
163 #define CP_ARBITER_GET_BODIES(arb, a, b) cpBody *a, *b; cpArbiterGetBodies(arb, &a, &b);
164 
166 typedef struct cpContactPointSet {
168  int count;
169 
171  struct {
178  } points[CP_MAX_CONTACTS_PER_ARBITER];
182 
186 int cpArbiterGetCount(const cpArbiter *arb);
188 cpVect cpArbiterGetNormal(const cpArbiter *arb, int i);
190 cpVect cpArbiterGetPoint(const cpArbiter *arb, int i);
192 cpFloat cpArbiterGetDepth(const cpArbiter *arb, int i);
193