Chipmunk Pro API Reference  6.1.3
 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 {
37 
39 typedef struct cpSegmentQueryInfo {
47 
49 typedef enum cpShapeType{
50  CP_CIRCLE_SHAPE,
51  CP_SEGMENT_SHAPE,
52  CP_POLY_SHAPE,
53  CP_NUM_SHAPES
54 } cpShapeType;
55 
56 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpVect p, cpVect rot);
57 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
58 typedef void (*cpShapeNearestPointQueryImpl)(cpShape *shape, cpVect p, cpNearestPointQueryInfo *info);
59 typedef void (*cpShapeSegmentQueryImpl)(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
60 
62 struct cpShapeClass {
63  cpShapeType type;
64 
65  cpShapeCacheDataImpl cacheData;
66  cpShapeDestroyImpl destroy;
67  cpShapeNearestPointQueryImpl nearestPointQuery;
68  cpShapeSegmentQueryImpl segmentQuery;
69 };
70 
72 struct cpShape {
73  CP_PRIVATE(const cpShapeClass *klass);
74 
77 
80 
84 
91 
96 
101  // Layer bitmask for this shape. Shapes only collide if the bitwise and of their layers is non-zero.
102  cpLayers layers;
103 
104  CP_PRIVATE(cpSpace *space);
105 
106  CP_PRIVATE(cpShape *next);
107  CP_PRIVATE(cpShape *prev);
108 
109  CP_PRIVATE(cpHashValue hashid);
110 };
111 
113 void cpShapeDestroy(cpShape *shape);
115 void cpShapeFree(cpShape *shape);
116 
118 cpBB cpShapeCacheBB(cpShape *shape);
120 cpBB cpShapeUpdate(cpShape *shape, cpVect pos, cpVect rot);
121 
124 
128 
131 
133 static inline cpVect cpSegmentQueryHitPoint(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
134 {
135  return cpvlerp(start, end, info.t);
136 }
137 
139 static inline cpFloat cpSegmentQueryHitDist(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
140 {
141  return cpvdist(start, end)*info.t;
142 }
143 
144 #define CP_DefineShapeStructGetter(type, member, name) \
145 static inline type cpShapeGet##name(const cpShape *shape){return shape->member;}
146 
147 #define CP_DefineShapeStructSetter(type, member, name, activates) \
148 static inline void cpShapeSet##name(cpShape *shape, type value){ \
149  if(activates && shape->body) cpBodyActivate(shape->body); \
150  shape->member = value; \
151 }
152 
153 #define CP_DefineShapeStructProperty(type, member, name, activates) \
154 CP_DefineShapeStructGetter(type, member, name) \
155 CP_DefineShapeStructSetter(type, member, name, activates)
156 
157 CP_DefineShapeStructGetter(cpSpace*, CP_PRIVATE(space), Space)
158 
159 CP_DefineShapeStructGetter(cpBody*, body, Body)
160 void cpShapeSetBody(cpShape *shape, cpBody *body);
161 
162 CP_DefineShapeStructGetter(cpBB, bb, BB)
163 CP_DefineShapeStructProperty(cpBool, sensor, Sensor, cpTrue)
164 CP_DefineShapeStructProperty(cpFloat, e, Elasticity, cpFalse)
165 CP_DefineShapeStructProperty(cpFloat, u, Friction, cpTrue)
166 CP_DefineShapeStructProperty(cpVect, surface_v, SurfaceVelocity, cpTrue)
167 CP_DefineShapeStructProperty(cpDataPointer, data, UserData, cpFalse)
168 CP_DefineShapeStructProperty(cpCollisionType, collision_type, CollisionType, cpTrue)
169 CP_DefineShapeStructProperty(cpGroup, group, Group, cpTrue)
170 CP_DefineShapeStructProperty(cpLayers, layers, Layers, cpTrue)
171 
175 void cpResetShapeIdCounter(void);
176 
177 #define CP_DeclareShapeGetter(struct, type, name) type struct##Get##name(const cpShape *shape)
178 
181 
183 typedef struct cpCircleShape {
184  cpShape shape;
185 
186  cpVect c, tc;
187  cpFloat r;
188 } cpCircleShape;
189 
191 cpCircleShape* cpCircleShapeAlloc(void);
193 cpCircleShape* cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
195 cpShape* cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
196 
197 CP_DeclareShapeGetter(cpCircleShape, cpVect, Offset);
198 CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
199 
202 
204 typedef struct cpSegmentShape {
205  cpShape shape;
206 
207  cpVect a, b, n;
208  cpVect ta, tb, tn;
209  cpFloat r;
210 
211  cpVect a_tangent, b_tangent;
212 } cpSegmentShape;
213 
215 cpSegmentShape* cpSegmentShapeAlloc(void);
217 cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
219 cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
220 
221 void cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next);
222 
223 CP_DeclareShapeGetter(cpSegmentShape, cpVect, A);
224 CP_DeclareShapeGetter(cpSegmentShape, cpVect, B);
225 CP_DeclareShapeGetter(cpSegmentShape, cpVect, Normal);
226 CP_DeclareShapeGetter(cpSegmentShape, cpFloat, Radius);
227