annotate trunk/chipmunkd/constraints/cpRatchetJoint.d @ 7:b68f10432182

some properties and some comments
author Extrawurst
date Thu, 02 Dec 2010 23:46:18 +0100
parents 707dd4e10c28
children c03a41d47b60
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.cpRatchetJoint;
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 *cpRatchetJointGetClass();
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 cpRatchetJoint {
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 angle, phase, ratchet;
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 //cpRatchetJoint *cpRatchetJointAlloc(void);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
22 //cpRatchetJoint *cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
23 //cpConstraint *cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet);
7
b68f10432182 some properties and some comments
Extrawurst
parents: 6
diff changeset
24 //TODO
6
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
25 //CP_DefineConstraintProperty(cpRatchetJoint, cpFloat, angle, Angle);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
26 //CP_DefineConstraintProperty(cpRatchetJoint, cpFloat, phase, Phase);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
27 //CP_DefineConstraintProperty(cpRatchetJoint, cpFloat, ratchet, Ratchet);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
28
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
29 // cpRatchetJoint.c ---------------------------------
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
30
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
31 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
32 preStep(cpRatchetJoint *joint, cpFloat dt, cpFloat dt_inv)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
33 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
34 mixin(CONSTRAINT_BEGIN!("joint", "a", "b"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
35
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
36 cpFloat angle = joint.angle;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
37 cpFloat phase = joint.phase;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
38 cpFloat ratchet = joint.ratchet;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
39
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
40 cpFloat delta = b.a - a.a;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
41 cpFloat diff = angle - delta;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
42 cpFloat pdist = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
43
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
44 if(diff*ratchet > 0.0f){
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
45 pdist = diff;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
46 } else {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
47 joint.angle = cpffloor((delta - phase)/ratchet)*ratchet + phase;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
48 }
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 // calculate moment of inertia coefficient.
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
51 joint.iSum = 1.0f/(a.i_inv + b.i_inv);
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 // calculate bias velocity
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
54 cpFloat maxBias = joint.constraint.maxBias;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
55 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
56
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
57 // compute max impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
58 joint.jMax = mixin(J_MAX!("joint", "dt"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
59
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
60 // 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
61 if(!joint.bias)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
62 joint.jAcc = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
63
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
64 // apply joint torque
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
65 a.w -= joint.jAcc*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
66 b.w += joint.jAcc*b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
67 }
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 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
70 applyImpulse(cpRatchetJoint *joint)
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 if(!joint.bias) return; // early exit
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
73
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
74 mixin(CONSTRAINT_BEGIN!("joint", "a", "b"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
75
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
76 // compute relative rotational velocity
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
77 cpFloat wr = b.w - a.w;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
78 cpFloat ratchet = joint.ratchet;
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 // compute normal impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
81 cpFloat j = -(joint.bias + wr)*joint.iSum;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
82 cpFloat jOld = joint.jAcc;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
83 joint.jAcc = cpfclamp((jOld + j)*ratchet, 0.0f, joint.jMax*cpfabs(ratchet))/ratchet;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
84 j = joint.jAcc - jOld;
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 // apply impulse
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
87 a.w -= j*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
88 b.w += j*b.i_inv;
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
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
91 static cpFloat
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
92 getImpulse(cpRatchetJoint *joint)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
93 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
94 return cpfabs(joint.jAcc);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
95 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
96
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
97 static /+const+/ cpConstraintClass klass = {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
98 cast(cpConstraintPreStepFunction)&preStep,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
99 cast(cpConstraintApplyImpulseFunction)&applyImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
100 cast(cpConstraintGetImpulseFunction)&getImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
101 };
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
102 mixin(CP_DefineClassGetter!("cpRatchetJoint"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
103
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
104 cpRatchetJoint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
105 cpRatchetJointAlloc()
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
106 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
107 return cast(cpRatchetJoint *)cpmalloc(cpRatchetJoint.sizeof);
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
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
110 cpRatchetJoint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
111 cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
112 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
113 cpConstraintInit(cast(cpConstraint *)joint, &klass, a, b);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
114
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
115 joint.angle = 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
116 joint.phase = phase;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
117 joint.ratchet = ratchet;
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 // STATIC_BODY_CHECK
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
120 joint.angle = (b ? b.a : 0.0f) - (a ? a.a : 0.0f);
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 joint;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
123 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
124
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
125 cpConstraint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
126 cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
127 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
128 return cast(cpConstraint *)cpRatchetJointInit(cpRatchetJointAlloc(), a, b, phase, ratchet);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
129 }