Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
cpConstraint.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 
24 
25 typedef struct cpConstraintClass cpConstraintClass;
26 
27 typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
28 typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
29 typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
30 typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
31 
33 struct cpConstraintClass {
34  cpConstraintPreStepImpl preStep;
35  cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
36  cpConstraintApplyImpulseImpl applyImpulse;
37  cpConstraintGetImpulseImpl getImpulse;
38 };
39 
41 typedef void (*cpConstraintPreSolveFunc)(cpConstraint *constraint, cpSpace *space);
43 typedef void (*cpConstraintPostSolveFunc)(cpConstraint *constraint, cpSpace *space);
44 
45 
47 struct cpConstraint {
48  CP_PRIVATE(const cpConstraintClass *klass);
49 
54 
55  CP_PRIVATE(cpSpace *space);
56 
57  CP_PRIVATE(cpConstraint *next_a);
58  CP_PRIVATE(cpConstraint *next_b);
59 
70 
74 
78 
83 };
84 
86 void cpConstraintDestroy(cpConstraint *constraint);
88 void cpConstraintFree(cpConstraint *constraint);
89 
91 static inline void cpConstraintActivateBodies(cpConstraint *constraint)
92 {
93  cpBody *a = constraint->a; if(a) cpBodyActivate(a);
94  cpBody *b = constraint->b; if(b) cpBodyActivate(b);
95 }
96 
98 #define CP_DefineConstraintStructGetter(type, member, name) \
99 static inline type cpConstraint##Get##name(const cpConstraint *constraint){return constraint->member;}
100 
102 #define CP_DefineConstraintStructSetter(type, member, name) \
103 static inline void cpConstraint##Set##name(cpConstraint *constraint, type value){ \
104  cpConstraintActivateBodies(constraint); \
105  constraint->member = value; \
106 }
107 
109 #define CP_DefineConstraintStructProperty(type, member, name) \
110 CP_DefineConstraintStructGetter(type, member, name) \
111 CP_DefineConstraintStructSetter(type, member, name)
112 
113 CP_DefineConstraintStructGetter(cpSpace*, CP_PRIVATE(space), Space)
114 
115 CP_DefineConstraintStructGetter(cpBody*, a, A)
116 CP_DefineConstraintStructGetter(cpBody*, b, B)
117 CP_DefineConstraintStructProperty(cpFloat, maxForce, MaxForce)
118 CP_DefineConstraintStructProperty(cpFloat, errorBias, ErrorBias)
119 CP_DefineConstraintStructProperty(cpFloat, maxBias, MaxBias)
120 CP_DefineConstraintStructProperty(cpConstraintPreSolveFunc, preSolve, PreSolveFunc)
121 CP_DefineConstraintStructProperty(cpConstraintPostSolveFunc, postSolve, PostSolveFunc)
122 CP_DefineConstraintStructProperty(cpDataPointer, data, UserData)
123 
124 // Get the last impulse applied by this constraint.
125 static inline cpFloat cpConstraintGetImpulse(cpConstraint *constraint)
126 {
127  return constraint->CP_PRIVATE(klass)->getImpulse(constraint);
128 }
129 
131 
132 #define cpConstraintCheckCast(constraint, struct) \
133  cpAssertHard(constraint->CP_PRIVATE(klass) == struct##GetClass(), "Constraint is not a "#struct)
134 
135 #define CP_DefineConstraintGetter(struct, type, member, name) \
136 static inline type struct##Get##name(const cpConstraint *constraint){ \
137  cpConstraintCheckCast(constraint, struct); \
138  return ((struct *)constraint)->member; \
139 }
140 
141 #define CP_DefineConstraintSetter(struct, type, member, name) \
142 static inline void struct##Set##name(cpConstraint *constraint, type value){ \
143  cpConstraintCheckCast(constraint, struct); \
144  cpConstraintActivateBodies(constraint); \
145  ((struct *)constraint)->member = value; \
146 }
147 
148 #define CP_DefineConstraintProperty(struct, type, member, name) \
149 CP_DefineConstraintGetter(struct, type, member, name) \
150 CP_DefineConstraintSetter(struct, type, member, name)
151 
152 #include "cpPinJoint.h"
153 #include "cpSlideJoint.h"
154 #include "cpPivotJoint.h"
155 #include "cpGrooveJoint.h"
156 #include "cpDampedSpring.h"
157 #include "cpDampedRotarySpring.h"
158 #include "cpRotaryLimitJoint.h"
159 #include "cpRatchetJoint.h"
160 #include "cpGearJoint.h"
161 #include "cpSimpleMotor.h"