00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00025
00026 typedef struct cpShapeClass cpShapeClass;
00027
00029 typedef struct cpNearestPointQueryInfo {
00031 cpShape *shape;
00033 cpVect p;
00035 cpFloat d;
00036 } cpNearestPointQueryInfo;
00037
00039 typedef struct cpSegmentQueryInfo {
00041 cpShape *shape;
00043 cpFloat t;
00045 cpVect n;
00046 } cpSegmentQueryInfo;
00047
00049 typedef enum cpShapeType{
00050 CP_CIRCLE_SHAPE,
00051 CP_SEGMENT_SHAPE,
00052 CP_POLY_SHAPE,
00053 CP_NUM_SHAPES
00054 } cpShapeType;
00055
00056 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpVect p, cpVect rot);
00057 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
00058 typedef void (*cpShapeNearestPointQueryImpl)(cpShape *shape, cpVect p, cpNearestPointQueryInfo *info);
00059 typedef void (*cpShapeSegmentQueryImpl)(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
00060
00062 struct cpShapeClass {
00063 cpShapeType type;
00064
00065 cpShapeCacheDataImpl cacheData;
00066 cpShapeDestroyImpl destroy;
00067 cpShapeNearestPointQueryImpl nearestPointQuery;
00068 cpShapeSegmentQueryImpl segmentQuery;
00069 };
00070
00072 struct cpShape {
00073 CP_PRIVATE(const cpShapeClass *klass);
00074
00076 cpBody *body;
00077
00079 cpBB bb;
00080
00083 cpBool sensor;
00084
00086 cpFloat e;
00088 cpFloat u;
00090 cpVect surface_v;
00091
00095 cpDataPointer data;
00096
00098 cpCollisionType collision_type;
00100 cpGroup group;
00101
00102 cpLayers layers;
00103
00104 CP_PRIVATE(cpSpace *space);
00105
00106 CP_PRIVATE(cpShape *next);
00107 CP_PRIVATE(cpShape *prev);
00108
00109 CP_PRIVATE(cpHashValue hashid);
00110 };
00111
00113 void cpShapeDestroy(cpShape *shape);
00115 void cpShapeFree(cpShape *shape);
00116
00118 cpBB cpShapeCacheBB(cpShape *shape);
00120 cpBB cpShapeUpdate(cpShape *shape, cpVect pos, cpVect rot);
00121
00123 cpBool cpShapePointQuery(cpShape *shape, cpVect p);
00124
00127 cpFloat cpShapeNearestPointQuery(cpShape *shape, cpVect p, cpNearestPointQueryInfo *out);
00128
00130 cpBool cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
00131
00133 static inline cpVect cpSegmentQueryHitPoint(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
00134 {
00135 return cpvlerp(start, end, info.t);
00136 }
00137
00139 static inline cpFloat cpSegmentQueryHitDist(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
00140 {
00141 return cpvdist(start, end)*info.t;
00142 }
00143
00144 #define CP_DefineShapeStructGetter(type, member, name) \
00145 static inline type cpShapeGet##name(const cpShape *shape){return shape->member;}
00146
00147 #define CP_DefineShapeStructSetter(type, member, name, activates) \
00148 static inline void cpShapeSet##name(cpShape *shape, type value){ \
00149 if(activates && shape->body) cpBodyActivate(shape->body); \
00150 shape->member = value; \
00151 }
00152
00153 #define CP_DefineShapeStructProperty(type, member, name, activates) \
00154 CP_DefineShapeStructGetter(type, member, name) \
00155 CP_DefineShapeStructSetter(type, member, name, activates)
00156
00157 CP_DefineShapeStructGetter(cpSpace*, CP_PRIVATE(space), Space)
00158
00159 CP_DefineShapeStructGetter(cpBody*, body, Body)
00160 void cpShapeSetBody(cpShape *shape, cpBody *body);
00161
00162 CP_DefineShapeStructGetter(cpBB, bb, BB)
00163 CP_DefineShapeStructProperty(cpBool, sensor, Sensor, cpTrue)
00164 CP_DefineShapeStructProperty(cpFloat, e, Elasticity, cpFalse)
00165 CP_DefineShapeStructProperty(cpFloat, u, Friction, cpTrue)
00166 CP_DefineShapeStructProperty(cpVect, surface_v, SurfaceVelocity, cpTrue)
00167 CP_DefineShapeStructProperty(cpDataPointer, data, UserData, cpFalse)
00168 CP_DefineShapeStructProperty(cpCollisionType, collision_type, CollisionType, cpTrue)
00169 CP_DefineShapeStructProperty(cpGroup, group, Group, cpTrue)
00170 CP_DefineShapeStructProperty(cpLayers, layers, Layers, cpTrue)
00171
00175 void cpResetShapeIdCounter(void);
00176
00177 #define CP_DeclareShapeGetter(struct, type, name) type struct##Get##name(const cpShape *shape)
00178
00181
00183 typedef struct cpCircleShape {
00184 cpShape shape;
00185
00186 cpVect c, tc;
00187 cpFloat r;
00188 } cpCircleShape;
00189
00191 cpCircleShape* cpCircleShapeAlloc(void);
00193 cpCircleShape* cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
00195 cpShape* cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
00196
00197 CP_DeclareShapeGetter(cpCircleShape, cpVect, Offset);
00198 CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
00199
00202
00204 typedef struct cpSegmentShape {
00205 cpShape shape;
00206
00207 cpVect a, b, n;
00208 cpVect ta, tb, tn;
00209 cpFloat r;
00210
00211 cpVect a_tangent, b_tangent;
00212 } cpSegmentShape;
00213
00215 cpSegmentShape* cpSegmentShapeAlloc(void);
00217 cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
00219 cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
00220
00221 void cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next);
00222
00223 CP_DeclareShapeGetter(cpSegmentShape, cpVect, A);
00224 CP_DeclareShapeGetter(cpSegmentShape, cpVect, B);
00225 CP_DeclareShapeGetter(cpSegmentShape, cpVect, Normal);
00226 CP_DeclareShapeGetter(cpSegmentShape, cpFloat, Radius);
00227