4
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 module chipmunkd.constraints.cpConstraint;
|
|
5
|
|
6 import chipmunkd.chipmunk;
|
|
7 import chipmunkd.chipmunk_types_h;
|
|
8 import chipmunkd.cpVect,chipmunkd.cpVect_h;
|
|
9 import chipmunkd.cpBody;
|
|
10
|
|
11 alias void function(cpConstraint *constraint, cpFloat dt, cpFloat dt_inv) cpConstraintPreStepFunction;
|
|
12 alias void function(cpConstraint *constraint) cpConstraintApplyImpulseFunction;
|
|
13 alias cpFloat function(cpConstraint *constraint) cpConstraintGetImpulseFunction;
|
|
14
|
|
15 struct cpConstraintClass {
|
|
16 cpConstraintPreStepFunction preStep;
|
|
17 cpConstraintApplyImpulseFunction applyImpulse;
|
|
18 cpConstraintGetImpulseFunction getImpulse;
|
|
19 }
|
|
20
|
|
21
|
|
22
|
|
23 struct cpConstraint {
|
|
24 cpConstraintClass *klass;
|
|
25
|
|
26 cpBody *a;
|
|
27 cpBody *b;
|
|
28 cpFloat maxForce;
|
|
29 cpFloat biasCoef;
|
|
30 cpFloat maxBias;
|
|
31
|
|
32 cpDataPointer data;
|
|
33 }
|
|
34
|
|
35 //#ifdef CP_USE_DEPRECATED_API_4
|
|
36 //typedef cpConstraint cpJoint;
|
|
37 //#endif
|
|
38
|
|
39 //void cpConstraintDestroy(cpConstraint *constraint);
|
|
40 //void cpConstraintFree(cpConstraint *constraint);
|
|
41
|
|
42 static void
|
|
43 cpConstraintActivateBodies(cpConstraint *constraint)
|
|
44 {
|
|
45 cpBody *a = constraint.a; if(a) cpBodyActivate(a);
|
|
46 cpBody *b = constraint.b; if(b) cpBodyActivate(b);
|
|
47 }
|
|
48
|
|
49 static cpFloat
|
|
50 cpConstraintGetImpulse(cpConstraint *constraint)
|
|
51 {
|
|
52 return constraint.klass.getImpulse(constraint);
|
|
53 }
|
|
54
|
|
55 template cpConstraintCheckCast(string constraint, string _struct)
|
|
56 {
|
|
57 enum cpConstraintCheckCast =
|
|
58 "assert("~constraint~".klass == "~_struct~"GetClass(), \"Constraint is not a "~_struct~"\");";
|
|
59 }
|
|
60
|
|
61 //#define cpConstraintCheckCast(constraint, struct) \
|
|
62 // cpAssert(constraint->klass == struct##GetClass(), "Constraint is not a "#struct);
|
|
63
|
|
64 template CP_DefineConstraintGetter(string _struct,string type,string member,string name)
|
|
65 {
|
|
66 enum CP_DefineConstraintGetter =
|
|
67 "static "~type~" "~_struct~"Get"~name~"(cpConstraint *constraint){"~
|
|
68 cpConstraintCheckCast!("constraint",_struct)~
|
|
69 "cpConstraintActivateBodies(constraint);"~
|
|
70 "return (cast("~_struct~"*)constraint)."~member~";"~
|
|
71 "}";
|
|
72 }
|
|
73
|
|
74 //#define CP_DefineConstraintGetter(struct, type, member, name) \
|
|
75 //static inline type \
|
|
76 //struct##Get##name(const cpConstraint *constraint){ \
|
|
77 // cpConstraintCheckCast(constraint, struct); \
|
|
78 // return ((struct *)constraint)->member; \
|
|
79 //} \
|
|
80
|
|
81 template CP_DefineConstraintSetter(string _struct,string type,string member,string name)
|
|
82 {
|
|
83 enum CP_DefineConstraintSetter =
|
|
84 "static void "~_struct~"Set"~name~"(cpConstraint *constraint,"~type~" value){"~
|
|
85 cpConstraintCheckCast!("constraint",_struct)~
|
|
86 "cpConstraintActivateBodies(constraint);"~
|
|
87 "(cast("~_struct~"*)constraint)."~member~" = value;"~
|
|
88 "}";
|
|
89 }
|
|
90
|
|
91 //#define CP_DefineConstraintSetter(struct, type, member, name) \
|
|
92 //static inline void \
|
|
93 //struct##Set##name(cpConstraint *constraint, type value){ \
|
|
94 // cpConstraintCheckCast(constraint, struct); \
|
|
95 // cpConstraintActivateBodies(constraint); \
|
|
96 // ((struct *)constraint)->member = value; \
|
|
97 //} \
|
|
98
|
|
99 template CP_DefineConstraintProperty(string _struct,string type,string member,string name)
|
|
100 {
|
|
101 enum CP_DefineConstraintProperty =
|
|
102 CP_DefineConstraintGetter!(_struct,type,member,name)~CP_DefineConstraintSetter!(_struct,type,member,name);
|
|
103
|
|
104 }
|
|
105
|
|
106 //#define CP_DefineConstraintProperty(struct, type, member, name) \
|
|
107 //CP_DefineConstraintGetter(struct, type, member, name) \
|
|
108 //CP_DefineConstraintSetter(struct, type, member, name)
|
|
109 //TODO:
|
|
110 //// Built in Joint types
|
|
111 public import chipmunkd.constraints.cpPinJoint;
|
6
|
112 public import chipmunkd.constraints.cpSlideJoint;
|
4
|
113 public import chipmunkd.constraints.cpPivotJoint;
|
6
|
114 public import chipmunkd.constraints.cpGrooveJoint;
|
4
|
115 public import chipmunkd.constraints.cpDampedSpring;
|
6
|
116 public import chipmunkd.constraints.cpDampedRotarySpring;
|
|
117 public import chipmunkd.constraints.cpRotaryLimitJoint;
|
|
118 public import chipmunkd.constraints.cpRatchetJoint;
|
|
119 public import chipmunkd.constraints.cpGearJoint;
|
|
120 public import chipmunkd.constraints.cpSimpleMotor;
|
4
|
121
|
|
122
|
|
123 cpFloat cp_constraint_bias_coef = 0.1f;
|
|
124
|
|
125 void cpConstraintDestroy(cpConstraint *constraint){}
|
|
126
|
|
127 void
|
|
128 cpConstraintFree(cpConstraint *constraint)
|
|
129 {
|
|
130 if(constraint){
|
|
131 cpConstraintDestroy(constraint);
|
|
132 cpfree(constraint);
|
|
133 }
|
|
134 }
|
|
135
|
|
136 void
|
|
137 cpConstraintInit(cpConstraint *constraint, /+const+/ cpConstraintClass *klass, cpBody *a, cpBody *b)
|
|
138 {
|
|
139 constraint.klass = klass;
|
|
140 constraint.a = a;
|
|
141 constraint.b = b;
|
|
142
|
|
143 constraint.maxForce = cast(cpFloat)INFINITY;
|
|
144 constraint.biasCoef = cp_constraint_bias_coef;
|
|
145 constraint.maxBias = cast(cpFloat)INFINITY;
|
|
146 }
|