Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
cpShape.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 
25 
26 typedef struct cpShapeClass cpShapeClass;
27 
29 typedef struct cpNearestPointQueryInfo {
40 
42 typedef struct cpSegmentQueryInfo {
50 
52 typedef enum cpShapeType{
53  CP_CIRCLE_SHAPE,
54  CP_SEGMENT_SHAPE,
55  CP_POLY_SHAPE,
56  CP_NUM_SHAPES
57 } cpShapeType;
58 
59 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpVect p, cpVect rot);
60 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
61 typedef void (*cpShapeNearestPointQueryImpl)(cpShape *shape, cpVect p, cpNearestPointQueryInfo *info);
62 typedef void (*cpShapeSegmentQueryImpl)(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
63 
65 struct cpShapeClass {
66  cpShapeType type;
67 
68  cpShapeCacheDataImpl cacheData;
69  cpShapeDestroyImpl destroy;
70  cpShapeNearestPointQueryImpl nearestPointQuery;
71  cpShapeSegmentQueryImpl segmentQuery;
72 };
73 
75 struct cpShape {
76  CP_PRIVATE(const cpShapeClass *klass);
77 
80 
83 
87 
94 
99 
104  // Layer bitmask for this shape. Shapes only collide if the bitwise and of their layers is non-zero.
105  cpLayers layers;
106 
107  CP_PRIVATE(cpSpace *space);
108 
109  CP_PRIVATE(cpShape *next);
110  CP_PRIVATE(cpShape *prev);
111 
112  CP_PRIVATE(cpHashValue hashid);
113 };
114 
116 void cpShapeDestroy(cpShape *shape);
118 void cpShapeFree(cpShape *shape);
119 
121 cpBB cpShapeCacheBB(cpShape *shape);
123 cpBB cpShapeUpdate(cpShape *shape, cpVect pos, cpVect rot);
124 
127 
131 
134 
136 static inline cpVect cpSegmentQueryHitPoint(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
137 {
138  return cpvlerp(start, end, info.t);
139 }
140 
142 static inline cpFloat cpSegmentQueryHitDist(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
143 {
144  return cpvdist(start, end)*info.t;
145 }
146 
147 #define CP_DefineShapeStructGetter(type, member, name) \
148 static inline type cpShapeGet##name(const cpShape *shape){return shape->member;}
149 
150 #define CP_DefineShapeStructSetter(type, member, name, activates) \
151 static inline void cpShapeSet##name(cpShape *shape, type value){ \
152  if(activates && shape->body) cpBodyActivate(shape->body); \
153  shape->member = value; \
154 }
155 
156 #define CP_DefineShapeStructProperty(type, member, name, activates) \
157 CP_DefineShapeStructGetter(type, member, name) \
158 CP_DefineShapeStructSetter(type, member, name, activates)
159 
160 CP_DefineShapeStructGetter(cpSpace*, CP_PRIVATE(space), Space)
161 
162 CP_DefineShapeStructGetter(cpBody*, body, Body)
163 void cpShapeSetBody(cpShape *shape, cpBody *body);
164 
165 CP_DefineShapeStructGetter(cpBB, bb, BB)
166 CP_DefineShapeStructProperty(cpBool, sensor, Sensor, cpTrue)
167 CP_DefineShapeStructProperty(cpFloat, e, Elasticity, cpFalse)
168 CP_DefineShapeStructProperty(cpFloat, u, Friction, cpTrue)
169 CP_DefineShapeStructProperty(cpVect, surface_v, SurfaceVelocity, cpTrue)
170 CP_DefineShapeStructProperty(cpDataPointer, data, UserData, cpFalse)
171 CP_DefineShapeStructProperty(cpCollisionType, collision_type, CollisionType, cpTrue)
172 CP_DefineShapeStructProperty(cpGroup, group, Group, cpTrue)
173 CP_DefineShapeStructProperty(cpLayers, layers, Layers, cpTrue)
174 
178 void cpResetShapeIdCounter(void);
179 
180 #define CP_DeclareShapeGetter(struct, type, name) type struct##Get##name(const cpShape *shape)
181 
184 
186 typedef struct cpCircleShape {
187  cpShape shape;
188 
189  cpVect c, tc;
190  cpFloat r;
191 } cpCircleShape;
192 
194 cpCircleShape* cpCircleShapeAlloc(void);
196 cpCircleShape* cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
198 cpShape* cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
199 
200 CP_DeclareShapeGetter(cpCircleShape, cpVect, Offset);
201 CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
202 
205 
207 typedef struct cpSegmentShape {
208  cpShape shape;
209 
210  cpVect a, b, n;
211  cpVect ta, tb, tn;
212  cpFloat r;
213 
214  cpVect a_tangent, b_tangent;
215 } cpSegmentShape;
216 
218 cpSegmentShape* cpSegmentShapeAlloc(void);
220 cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
222 cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
223 
225 void cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next);
226 
227 CP_DeclareShapeGetter(cpSegmentShape, cpVect, A);
228 CP_DeclareShapeGetter(cpSegmentShape, cpVect, B);
229 CP_DeclareShapeGetter(cpSegmentShape, cpVect, Normal);
230 CP_DeclareShapeGetter(cpSegmentShape, cpFloat, Radius);
231