Chipmunk2D Pro API Reference  6.2.1
 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 2
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 
121 // Get the relative surface velocity of the two shapes in contact.
122 cpVect cpArbiterGetSurfaceVelocity(cpArbiter *arb);
123 
124 // Override the relative surface velocity of the two shapes in contact.
125 // By default this is calculated to be the difference of the two
126 // surface velocities clamped to the tangent plane.
127 void cpArbiterSetSurfaceVelocity(cpArbiter *arb, cpVect vr);
128 
129 CP_DefineArbiterStructProperty(cpDataPointer, data, UserData)
130 
139 cpFloat cpArbiterTotalKE(const cpArbiter *arb);
140 
141 
145 void cpArbiterIgnore(cpArbiter *arb);
146 
150 static inline void cpArbiterGetShapes(const cpArbiter *arb, cpShape **a, cpShape **b)
151 {
152  if(arb->CP_PRIVATE(swappedColl)){
153  (*a) = arb->CP_PRIVATE(b), (*b) = arb->CP_PRIVATE(a);
154  } else {
155  (*a) = arb->CP_PRIVATE(a), (*b) = arb->CP_PRIVATE(b);
156  }
157 }
159 #define CP_ARBITER_GET_SHAPES(__arb__, __a__, __b__) cpShape *__a__, *__b__; cpArbiterGetShapes(__arb__, &__a__, &__b__);
160 
164 static inline void cpArbiterGetBodies(const cpArbiter *arb, cpBody **a, cpBody **b)
165 {
166  CP_ARBITER_GET_SHAPES(arb, shape_a, shape_b);
167  (*a) = shape_a->body;
168  (*b) = shape_b->body;
169 }
171 #define CP_ARBITER_GET_BODIES(__arb__, __a__, __b__) cpBody *__a__, *__b__; cpArbiterGetBodies(__arb__, &__a__, &__b__);
172 
174 typedef struct cpContactPointSet {
176  int count;
177 
179  struct {
186  } points[CP_MAX_CONTACTS_PER_ARBITER];
188 
191 
195 
199 int cpArbiterGetCount(const cpArbiter *arb);
201 cpVect cpArbiterGetNormal(const cpArbiter *arb, int i);
203 cpVect cpArbiterGetPoint(const cpArbiter *arb, int i);
205 cpFloat cpArbiterGetDepth(const cpArbiter *arb, int i);
206