annotate trunk/chipmunkd/constraints/cpRotaryLimitJoint.d @ 6:707dd4e10c28

ported rest of the constraints (chipmunk 5.3.2)
author Extrawurst
date Thu, 02 Dec 2010 22:26:04 +0100
parents
children b68f10432182
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
1
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
2 // written in the D programming language
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
3
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
4 module chipmunkd.constraints.cpRotaryLimitJoint;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
5
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
6 import chipmunkd.chipmunk;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
7 import chipmunkd.constraints.util;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
8
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
9 //const cpConstraintClass *cpRotaryLimitJointGetClass();
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
10
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
11 struct cpRotaryLimitJoint {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
12 cpConstraint constraint;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
13 cpFloat min, max;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
14
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
15 cpFloat iSum;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
16
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
17 cpFloat bias;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
18 cpFloat jAcc, jMax;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
19 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
20
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
21 //cpRotaryLimitJoint *cpRotaryLimitJointAlloc(void);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
22 //cpRotaryLimitJoint *cpRotaryLimitJointInit(cpRotaryLimitJoint *joint, cpBody *a, cpBody *b, cpFloat min, cpFloat max);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
23 //cpConstraint *cpRotaryLimitJointNew(cpBody *a, cpBody *b, cpFloat min, cpFloat max);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
24 //
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
25 //CP_DefineConstraintProperty(cpRotaryLimitJoint, cpFloat, min, Min);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
26 //CP_DefineConstraintProperty(cpRotaryLimitJoint, cpFloat, max, Max);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
27
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
28 // cpRotaryLimitJoint.c ---------------------------------
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
29
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
30 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
31 preStep(cpRotaryLimitJoint *joint, cpFloat dt, cpFloat dt_inv)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
32 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
33 mixin(CONSTRAINT_BEGIN!("joint", "a", "b"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
34
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
35 cpFloat dist = b.a - a.a;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
36 cpFloat pdist = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
37 if(dist > joint.max) {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
38 pdist = joint.max - dist;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
39 } else if(dist < joint.min) {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
40 pdist = joint.min - dist;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
41 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
42
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
43 // calculate moment of inertia coefficient.
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
44 joint.iSum = 1.0f/(a.i_inv + b.i_inv);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
45
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
46 // calculate bias velocity
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
47 cpFloat maxBias = joint.constraint.maxBias;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
48 joint.bias = cpfclamp(-joint.constraint.biasCoef*dt_inv*(pdist), -maxBias, maxBias);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
49
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
50 // compute max impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
51 joint.jMax = mixin(J_MAX!("joint", "dt"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
52
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
53 // If the bias is 0, the joint is not at a limit. Reset the impulse.
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
54 if(!joint.bias)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
55 joint.jAcc = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
56
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
57 // apply joint torque
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
58 a.w -= joint.jAcc*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
59 b.w += joint.jAcc*b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
60 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
61
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
62 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
63 applyImpulse(cpRotaryLimitJoint *joint)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
64 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
65 if(!joint.bias) return; // early exit
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
66
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
67 mixin(CONSTRAINT_BEGIN!("joint", "a", "b"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
68
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
69 // compute relative rotational velocity
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
70 cpFloat wr = b.w - a.w;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
71
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
72 // compute normal impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
73 cpFloat j = -(joint.bias + wr)*joint.iSum;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
74 cpFloat jOld = joint.jAcc;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
75 if(joint.bias < 0.0f){
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
76 joint.jAcc = cpfclamp(jOld + j, 0.0f, joint.jMax);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
77 } else {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
78 joint.jAcc = cpfclamp(jOld + j, -joint.jMax, 0.0f);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
79 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
80 j = joint.jAcc - jOld;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
81
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
82 // apply impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
83 a.w -= j*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
84 b.w += j*b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
85 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
86
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
87 static cpFloat
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
88 getImpulse(cpRotaryLimitJoint *joint)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
89 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
90 return cpfabs(joint.jAcc);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
91 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
92
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
93 static /+const+/ cpConstraintClass klass = {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
94 cast(cpConstraintPreStepFunction)&preStep,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
95 cast(cpConstraintApplyImpulseFunction)&applyImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
96 cast(cpConstraintGetImpulseFunction)&getImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
97 };
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
98 mixin(CP_DefineClassGetter!("cpRotaryLimitJoint"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
99
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
100 cpRotaryLimitJoint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
101 cpRotaryLimitJointAlloc()
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
102 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
103 return cast(cpRotaryLimitJoint *)cpmalloc(cpRotaryLimitJoint.sizeof);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
104 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
105
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
106 cpRotaryLimitJoint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
107 cpRotaryLimitJointInit(cpRotaryLimitJoint *joint, cpBody *a, cpBody *b, cpFloat min, cpFloat max)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
108 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
109 cpConstraintInit(cast(cpConstraint *)joint, &klass, a, b);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
110
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
111 joint.min = min;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
112 joint.max = max;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
113
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
114 joint.jAcc = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
115
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
116 return joint;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
117 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
118
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
119 cpConstraint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
120 cpRotaryLimitJointNew(cpBody *a, cpBody *b, cpFloat min, cpFloat max)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
121 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
122 return cast(cpConstraint *)cpRotaryLimitJointInit(cpRotaryLimitJointAlloc(), a, b, min, max);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
123 }