Debian Codegraph @

src/cpGearJoint.c

raw symbols SCIP
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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
22 #include "chipmunk/chipmunk_private.h"
23
24 static void
25 preStep(cpGearJoint *joint, cpFloat dt)
26 {
27 cpBody *a = joint->constraint.a;
28 cpBody *b = joint->constraint.b;
29
30 // calculate moment of inertia coefficient.
31 joint->iSum = 1.0f/(a->i_inv*joint->ratio_inv + joint->ratio*b->i_inv);
32
33 // calculate bias velocity
34 cpFloat maxBias = joint->constraint.maxBias;
35 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*(b->a*joint->ratio - a->a - joint->phase)/dt, -maxBias, maxBias);
36 }
37
38 static void
39 applyCachedImpulse(cpGearJoint *joint, cpFloat dt_coef)
40 {
41 cpBody *a = joint->constraint.a;
42 cpBody *b = joint->constraint.b;
43
44 cpFloat j = joint->jAcc*dt_coef;
45 a->w -= j*a->i_inv*joint->ratio_inv;
46 b->w += j*b->i_inv;
47 }
48
49 static void
50 applyImpulse(cpGearJoint *joint, cpFloat dt)
51 {
52 cpBody *a = joint->constraint.a;
53 cpBody *b = joint->constraint.b;
54
55 // compute relative rotational velocity
56 cpFloat wr = b->w*joint->ratio - a->w;
57
58 cpFloat jMax = joint->constraint.maxForce*dt;
59
60 // compute normal impulse
61 cpFloat j = (joint->bias - wr)*joint->iSum;
62 cpFloat jOld = joint->jAcc;
63 joint->jAcc = cpfclamp(jOld + j, -jMax, jMax);
64 j = joint->jAcc - jOld;
65
66 // apply impulse
67 a->w -= j*a->i_inv*joint->ratio_inv;
68 b->w += j*b->i_inv;
69 }
70
71 static cpFloat
72 getImpulse(cpGearJoint *joint)
73 {
74 return cpfabs(joint->jAcc);
75 }
76
77 static const cpConstraintClass klass = {
78 (cpConstraintPreStepImpl)preStep,
79 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
80 (cpConstraintApplyImpulseImpl)applyImpulse,
81 (cpConstraintGetImpulseImpl)getImpulse,
82 };
83
84 cpGearJoint *
85 cpGearJointAlloc(void)
86 {
87 return (cpGearJoint *)cpcalloc(1, sizeof(cpGearJoint));
88 }
89
90 cpGearJoint *
91 cpGearJointInit(cpGearJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
92 {
93 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
94
95 joint->phase = phase;
96 joint->ratio = ratio;
97 joint->ratio_inv = 1.0f/ratio;
98
99 joint->jAcc = 0.0f;
100
101 return joint;
102 }
103
104 cpConstraint *
105 cpGearJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
106 {
107 return (cpConstraint *)cpGearJointInit(cpGearJointAlloc(), a, b, phase, ratio);
108 }
109
110 cpBool
111 cpConstraintIsGearJoint(const cpConstraint *constraint)
112 {
113 return (constraint->klass == &klass);
114 }
115
116 cpFloat
117 cpGearJointGetPhase(const cpConstraint *constraint)
118 {
119 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
120 return ((cpGearJoint *)constraint)->phase;
121 }
122
123 void
124 cpGearJointSetPhase(cpConstraint *constraint, cpFloat phase)
125 {
126 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
127 cpConstraintActivateBodies(constraint);
128 ((cpGearJoint *)constraint)->phase = phase;
129 }
130
131 cpFloat
132 cpGearJointGetRatio(const cpConstraint *constraint)
133 {
134 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
135 return ((cpGearJoint *)constraint)->ratio;
136 }
137
138 void
139 cpGearJointSetRatio(cpConstraint *constraint, cpFloat ratio)
140 {
141 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
142 cpConstraintActivateBodies(constraint);
143 ((cpGearJoint *)constraint)->ratio = ratio;
144 ((cpGearJoint *)constraint)->ratio_inv = 1.0f/ratio;
145 }