annotate trunk/chipmunkd/constraints/cpDampedRotarySpring.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.cpDampedRotarySpring;
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 alias cpFloat function(cpConstraint *spring, cpFloat relativeAngle) cpDampedRotarySpringTorqueFunc;
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 //const cpConstraintClass *cpDampedRotarySpringGetClass();
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
12
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
13 struct cpDampedRotarySpring {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
14 cpConstraint constraint;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
15 cpFloat restAngle;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
16 cpFloat stiffness;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
17 cpFloat damping;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
18 cpDampedRotarySpringTorqueFunc springTorqueFunc;
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 cpFloat target_wrn;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
21 cpFloat w_coef;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
22
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
23 cpFloat iSum;
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
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
26 //cpDampedRotarySpring *cpDampedRotarySpringAlloc(void);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
27 //cpDampedRotarySpring *cpDampedRotarySpringInit(cpDampedRotarySpring *joint, cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
28 //cpConstraint *cpDampedRotarySpringNew(cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping);
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 //CP_DefineConstraintProperty(cpDampedRotarySpring, cpFloat, restAngle, RestAngle);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
31 //CP_DefineConstraintProperty(cpDampedRotarySpring, cpFloat, stiffness, Stiffness);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
32 //CP_DefineConstraintProperty(cpDampedRotarySpring, cpFloat, damping, Damping);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
33 //CP_DefineConstraintProperty(cpDampedRotarySpring, cpDampedRotarySpringTorqueFunc, springTorqueFunc, SpringTorqueFunc);
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 // cpDampedRotarySpring.c ---------------------------------
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
36
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
37 static cpFloat
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
38 defaultSpringTorque(cpDampedRotarySpring *spring, cpFloat relativeAngle){
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
39 return (relativeAngle - spring.restAngle)*spring.stiffness;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
40 }
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 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
43 preStep(cpDampedRotarySpring *spring, cpFloat dt, cpFloat dt_inv)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
44 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
45 mixin(CONSTRAINT_BEGIN!("spring", "a", "b"));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
46
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
47 cpFloat moment = a.i_inv + b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
48 spring.iSum = 1.0f/moment;
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 spring.w_coef = 1.0f - cpfexp(-spring.damping*dt*moment);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
51 spring.target_wrn = 0.0f;
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 // apply spring torque
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
54 cpFloat j_spring = spring.springTorqueFunc(cast(cpConstraint *)spring, a.a - b.a)*dt;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
55 a.w -= j_spring*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
56 b.w += j_spring*b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
57 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
58
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
59 static void
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
60 applyImpulse(cpDampedRotarySpring *spring)
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 mixin(CONSTRAINT_BEGIN!("spring", "a", "b"));
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 // compute relative velocity
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
65 cpFloat wrn = a.w - b.w;//normal_relative_velocity(a, b, r1, r2, n) - spring.target_vrn;
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 // compute velocity loss from drag
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
68 // not 100% certain this is derived correctly, though it makes sense
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
69 cpFloat w_damp = wrn*spring.w_coef;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
70 spring.target_wrn = wrn - w_damp;
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 //apply_impulses(a, b, spring.r1, spring.r2, cpvmult(spring.n, v_damp*spring.nMass));
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
73 cpFloat j_damp = w_damp*spring.iSum;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
74 a.w -= j_damp*a.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
75 b.w += j_damp*b.i_inv;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
76 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
77
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
78 static cpFloat
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
79 getImpulse(cpConstraint *constraint)
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
80 {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
81 return 0.0f;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
82 }
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
83
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
84 static /+const+/ cpConstraintClass klass = {
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
85 cast(cpConstraintPreStepFunction)&preStep,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
86 cast(cpConstraintApplyImpulseFunction)&applyImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
87 cast(cpConstraintGetImpulseFunction)&getImpulse,
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
88 };
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
89 mixin(CP_DefineClassGetter!("cpDampedRotarySpring"));
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 cpDampedRotarySpring *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
92 cpDampedRotarySpringAlloc()
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 cast(cpDampedRotarySpring *)cpmalloc(cpDampedRotarySpring.sizeof);
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 cpDampedRotarySpring *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
98 cpDampedRotarySpringInit(cpDampedRotarySpring *spring, cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping)
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 cpConstraintInit(cast(cpConstraint *)spring, &klass, a, b);
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 spring.restAngle = restAngle;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
103 spring.stiffness = stiffness;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
104 spring.damping = damping;
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
105 spring.springTorqueFunc = cast(cpDampedRotarySpringTorqueFunc)&defaultSpringTorque;
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 spring;
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 cpConstraint *
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
111 cpDampedRotarySpringNew(cpBody *a, cpBody *b, cpFloat restAngle, cpFloat stiffness, cpFloat damping)
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 return cast(cpConstraint *)cpDampedRotarySpringInit(cpDampedRotarySpringAlloc(), a, b, restAngle, stiffness, damping);
707dd4e10c28 ported rest of the constraints (chipmunk 5.3.2)
Extrawurst
parents:
diff changeset
114 }