00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00024
00025 typedef struct cpConstraintClass cpConstraintClass;
00026
00027 typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
00028 typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
00029 typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
00030 typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
00031
00033 struct cpConstraintClass {
00034 cpConstraintPreStepImpl preStep;
00035 cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
00036 cpConstraintApplyImpulseImpl applyImpulse;
00037 cpConstraintGetImpulseImpl getImpulse;
00038 };
00039
00041 typedef void (*cpConstraintPreSolveFunc)(cpConstraint *constraint, cpSpace *space);
00043 typedef void (*cpConstraintPostSolveFunc)(cpConstraint *constraint, cpSpace *space);
00044
00045
00047 struct cpConstraint {
00048 CP_PRIVATE(const cpConstraintClass *klass);
00049
00051 cpBody *a;
00053 cpBody *b;
00054
00055 CP_PRIVATE(cpSpace *space);
00056
00057 CP_PRIVATE(cpConstraint *next_a);
00058 CP_PRIVATE(cpConstraint *next_b);
00059
00062 cpFloat maxForce;
00066 cpFloat errorBias;
00069 cpFloat maxBias;
00070
00073 cpConstraintPreSolveFunc preSolve;
00074
00077 cpConstraintPostSolveFunc postSolve;
00078
00082 cpDataPointer data;
00083 };
00084
00086 void cpConstraintDestroy(cpConstraint *constraint);
00088 void cpConstraintFree(cpConstraint *constraint);
00089
00091 static inline void cpConstraintActivateBodies(cpConstraint *constraint)
00092 {
00093 cpBody *a = constraint->a; if(a) cpBodyActivate(a);
00094 cpBody *b = constraint->b; if(b) cpBodyActivate(b);
00095 }
00096
00098 #define CP_DefineConstraintStructGetter(type, member, name) \
00099 static inline type cpConstraint##Get##name(const cpConstraint *constraint){return constraint->member;}
00100
00102 #define CP_DefineConstraintStructSetter(type, member, name) \
00103 static inline void cpConstraint##Set##name(cpConstraint *constraint, type value){ \
00104 cpConstraintActivateBodies(constraint); \
00105 constraint->member = value; \
00106 }
00107
00109 #define CP_DefineConstraintStructProperty(type, member, name) \
00110 CP_DefineConstraintStructGetter(type, member, name) \
00111 CP_DefineConstraintStructSetter(type, member, name)
00112
00113 CP_DefineConstraintStructGetter(cpSpace*, CP_PRIVATE(space), Space)
00114
00115 CP_DefineConstraintStructGetter(cpBody*, a, A)
00116 CP_DefineConstraintStructGetter(cpBody*, b, B)
00117 CP_DefineConstraintStructProperty(cpFloat, maxForce, MaxForce)
00118 CP_DefineConstraintStructProperty(cpFloat, errorBias, ErrorBias)
00119 CP_DefineConstraintStructProperty(cpFloat, maxBias, MaxBias)
00120 CP_DefineConstraintStructProperty(cpConstraintPreSolveFunc, preSolve, PreSolveFunc)
00121 CP_DefineConstraintStructProperty(cpConstraintPostSolveFunc, postSolve, PostSolveFunc)
00122 CP_DefineConstraintStructProperty(cpDataPointer, data, UserData)
00123
00124
00125 static inline cpFloat cpConstraintGetImpulse(cpConstraint *constraint)
00126 {
00127 return constraint->CP_PRIVATE(klass)->getImpulse(constraint);
00128 }
00129
00131
00132 #define cpConstraintCheckCast(constraint, struct) \
00133 cpAssertHard(constraint->CP_PRIVATE(klass) == struct##GetClass(), "Constraint is not a "#struct)
00134
00135 #define CP_DefineConstraintGetter(struct, type, member, name) \
00136 static inline type struct##Get##name(const cpConstraint *constraint){ \
00137 cpConstraintCheckCast(constraint, struct); \
00138 return ((struct *)constraint)->member; \
00139 }
00140
00141 #define CP_DefineConstraintSetter(struct, type, member, name) \
00142 static inline void struct##Set##name(cpConstraint *constraint, type value){ \
00143 cpConstraintCheckCast(constraint, struct); \
00144 cpConstraintActivateBodies(constraint); \
00145 ((struct *)constraint)->member = value; \
00146 }
00147
00148 #define CP_DefineConstraintProperty(struct, type, member, name) \
00149 CP_DefineConstraintGetter(struct, type, member, name) \
00150 CP_DefineConstraintSetter(struct, type, member, name)
00151
00152 #include "cpPinJoint.h"
00153 #include "cpSlideJoint.h"
00154 #include "cpPivotJoint.h"
00155 #include "cpGrooveJoint.h"
00156 #include "cpDampedSpring.h"
00157 #include "cpDampedRotarySpring.h"
00158 #include "cpRotaryLimitJoint.h"
00159 #include "cpRatchetJoint.h"
00160 #include "cpGearJoint.h"
00161 #include "cpSimpleMotor.h"