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 cpSegmentQueryInfo {
00031 cpShape *shape;
00033 cpFloat t;
00035 cpVect n;
00036 } cpSegmentQueryInfo;
00037
00039 typedef enum cpShapeType{
00040 CP_CIRCLE_SHAPE,
00041 CP_SEGMENT_SHAPE,
00042 CP_POLY_SHAPE,
00043 CP_NUM_SHAPES
00044 } cpShapeType;
00045
00046 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpVect p, cpVect rot);
00047 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
00048 typedef cpBool (*cpShapePointQueryImpl)(cpShape *shape, cpVect p);
00049 typedef void (*cpShapeSegmentQueryImpl)(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
00050
00052 struct cpShapeClass {
00053 cpShapeType type;
00054
00055 cpShapeCacheDataImpl cacheData;
00056 cpShapeDestroyImpl destroy;
00057 cpShapePointQueryImpl pointQuery;
00058 cpShapeSegmentQueryImpl segmentQuery;
00059 };
00060
00062 struct cpShape {
00063 CP_PRIVATE(const cpShapeClass *klass);
00064
00066 cpBody *body;
00067
00069 cpBB bb;
00070
00073 cpBool sensor;
00074
00076 cpFloat e;
00078 cpFloat u;
00080 cpVect surface_v;
00081
00085 cpDataPointer data;
00086
00088 cpCollisionType collision_type;
00090 cpGroup group;
00091
00092 cpLayers layers;
00093
00094 CP_PRIVATE(cpSpace *space);
00095
00096 CP_PRIVATE(cpShape *next);
00097 CP_PRIVATE(cpShape *prev);
00098
00099 CP_PRIVATE(cpHashValue hashid);
00100 };
00101
00103 void cpShapeDestroy(cpShape *shape);
00105 void cpShapeFree(cpShape *shape);
00106
00108 cpBB cpShapeCacheBB(cpShape *shape);
00110 cpBB cpShapeUpdate(cpShape *shape, cpVect pos, cpVect rot);
00111
00113 cpBool cpShapePointQuery(cpShape *shape, cpVect p);
00114
00115 #define CP_DefineShapeStructGetter(type, member, name) \
00116 static inline type cpShapeGet##name(const cpShape *shape){return shape->member;}
00117
00118 #define CP_DefineShapeStructSetter(type, member, name, activates) \
00119 static inline void cpShapeSet##name(cpShape *shape, type value){ \
00120 if(activates && shape->body) cpBodyActivate(shape->body); \
00121 shape->member = value; \
00122 }
00123
00124 #define CP_DefineShapeStructProperty(type, member, name, activates) \
00125 CP_DefineShapeStructGetter(type, member, name) \
00126 CP_DefineShapeStructSetter(type, member, name, activates)
00127
00128 CP_DefineShapeStructGetter(cpBody*, body, Body);
00129 void cpShapeSetBody(cpShape *shape, cpBody *body);
00130
00131 CP_DefineShapeStructGetter(cpBB, bb, BB);
00132 CP_DefineShapeStructProperty(cpBool, sensor, Sensor, cpTrue);
00133 CP_DefineShapeStructProperty(cpFloat, e, Elasticity, cpFalse);
00134 CP_DefineShapeStructProperty(cpFloat, u, Friction, cpTrue);
00135 CP_DefineShapeStructProperty(cpVect, surface_v, SurfaceVelocity, cpTrue);
00136 CP_DefineShapeStructProperty(cpDataPointer, data, UserData, cpFalse);
00137 CP_DefineShapeStructProperty(cpCollisionType, collision_type, CollisionType, cpTrue);
00138 CP_DefineShapeStructProperty(cpGroup, group, Group, cpTrue);
00139 CP_DefineShapeStructProperty(cpLayers, layers, Layers, cpTrue);
00140
00144 void cpResetShapeIdCounter(void);
00145
00147 cpBool cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
00148
00150 static inline cpVect cpSegmentQueryHitPoint(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
00151 {
00152 return cpvlerp(start, end, info.t);
00153 }
00154
00156 static inline cpFloat cpSegmentQueryHitDist(const cpVect start, const cpVect end, const cpSegmentQueryInfo info)
00157 {
00158 return cpvdist(start, end)*info.t;
00159 }
00160
00161 #define CP_DeclareShapeGetter(struct, type, name) type struct##Get##name(const cpShape *shape)
00162
00165
00167 typedef struct cpCircleShape {
00168 cpShape shape;
00169
00170 cpVect c, tc;
00171 cpFloat r;
00172 } cpCircleShape;
00173
00175 cpCircleShape* cpCircleShapeAlloc(void);
00177 cpCircleShape* cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset);
00179 cpShape* cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset);
00180
00181 CP_DeclareShapeGetter(cpCircleShape, cpVect, Offset);
00182 CP_DeclareShapeGetter(cpCircleShape, cpFloat, Radius);
00183
00186
00188 typedef struct cpSegmentShape {
00189 cpShape shape;
00190
00191 cpVect a, b, n;
00192 cpVect ta, tb, tn;
00193 cpFloat r;
00194
00195 cpVect a_tangent, b_tangent;
00196 } cpSegmentShape;
00197
00199 cpSegmentShape* cpSegmentShapeAlloc(void);
00201 cpSegmentShape* cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat radius);
00203 cpShape* cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat radius);
00204
00205 void cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next);
00206
00207 CP_DeclareShapeGetter(cpSegmentShape, cpVect, A);
00208 CP_DeclareShapeGetter(cpSegmentShape, cpVect, B);
00209 CP_DeclareShapeGetter(cpSegmentShape, cpVect, Normal);
00210 CP_DeclareShapeGetter(cpSegmentShape, cpFloat, Radius);
00211