Debian Codegraph @

src/cpSimpleMotor.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(cpSimpleMotor *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 + b->i_inv);
32 }
33
34 static void
35 applyCachedImpulse(cpSimpleMotor *joint, cpFloat dt_coef)
36 {
37 cpBody *a = joint->constraint.a;
38 cpBody *b = joint->constraint.b;
39
40 cpFloat j = joint->jAcc*dt_coef;
41 a->w -= j*a->i_inv;
42 b->w += j*b->i_inv;
43 }
44
45 static void
46 applyImpulse(cpSimpleMotor *joint, cpFloat dt)
47 {
48 cpBody *a = joint->constraint.a;
49 cpBody *b = joint->constraint.b;
50
51 // compute relative rotational velocity
52 cpFloat wr = b->w - a->w + joint->rate;
53
54 cpFloat jMax = joint->constraint.maxForce*dt;
55
56 // compute normal impulse
57 cpFloat j = -wr*joint->iSum;
58 cpFloat jOld = joint->jAcc;
59 joint->jAcc = cpfclamp(jOld + j, -jMax, jMax);
60 j = joint->jAcc - jOld;
61
62 // apply impulse
63 a->w -= j*a->i_inv;
64 b->w += j*b->i_inv;
65 }
66
67 static cpFloat
68 getImpulse(cpSimpleMotor *joint)
69 {
70 return cpfabs(joint->jAcc);
71 }
72
73 static const cpConstraintClass klass = {
74 (cpConstraintPreStepImpl)preStep,
75 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
76 (cpConstraintApplyImpulseImpl)applyImpulse,
77 (cpConstraintGetImpulseImpl)getImpulse,
78 };
79
80 cpSimpleMotor *
81 cpSimpleMotorAlloc(void)
82 {
83 return (cpSimpleMotor *)cpcalloc(1, sizeof(cpSimpleMotor));
84 }
85
86 cpSimpleMotor *
87 cpSimpleMotorInit(cpSimpleMotor *joint, cpBody *a, cpBody *b, cpFloat rate)
88 {
89 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
90
91 joint->rate = rate;
92
93 joint->jAcc = 0.0f;
94
95 return joint;
96 }
97
98 cpConstraint *
99 cpSimpleMotorNew(cpBody *a, cpBody *b, cpFloat rate)
100 {
101 return (cpConstraint *)cpSimpleMotorInit(cpSimpleMotorAlloc(), a, b, rate);
102 }
103
104 cpBool
105 cpConstraintIsSimpleMotor(const cpConstraint *constraint)
106 {
107 return (constraint->klass == &klass);
108 }
109
110 cpFloat
111 cpSimpleMotorGetRate(const cpConstraint *constraint)
112 {
113 cpAssertHard(cpConstraintIsSimpleMotor(constraint), "Constraint is not a pin joint.");
114 return ((cpSimpleMotor *)constraint)->rate;
115 }
116
117 void
118 cpSimpleMotorSetRate(cpConstraint *constraint, cpFloat rate)
119 {
120 cpAssertHard(cpConstraintIsSimpleMotor(constraint), "Constraint is not a pin joint.");
121 cpConstraintActivateBodies(constraint);
122 ((cpSimpleMotor *)constraint)->rate = rate;
123 }