[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / chipmunk2d / src / cpRatchetJoint.c
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(cpRatchetJoint *joint, cpFloat dt)
26 {
27         cpBody *a = joint->constraint.a;
28         cpBody *b = joint->constraint.b;
29         
30         cpFloat angle = joint->angle;
31         cpFloat phase = joint->phase;
32         cpFloat ratchet = joint->ratchet;
33         
34         cpFloat delta = b->a - a->a;
35         cpFloat diff = angle - delta;
36         cpFloat pdist = 0.0f;
37         
38         if(diff*ratchet > 0.0f){
39                 pdist = diff;
40         } else {
41                 joint->angle = cpffloor((delta - phase)/ratchet)*ratchet + phase;
42         }
43         
44         // calculate moment of inertia coefficient.
45         joint->iSum = 1.0f/(a->i_inv + b->i_inv);
46         
47         // calculate bias velocity
48         cpFloat maxBias = joint->constraint.maxBias;
49         joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*pdist/dt, -maxBias, maxBias);
50
51         // If the bias is 0, the joint is not at a limit. Reset the impulse.
52         if(!joint->bias) joint->jAcc = 0.0f;
53 }
54
55 static void
56 applyCachedImpulse(cpRatchetJoint *joint, cpFloat dt_coef)
57 {
58         cpBody *a = joint->constraint.a;
59         cpBody *b = joint->constraint.b;
60         
61         cpFloat j = joint->jAcc*dt_coef;
62         a->w -= j*a->i_inv;
63         b->w += j*b->i_inv;
64 }
65
66 static void
67 applyImpulse(cpRatchetJoint *joint, cpFloat dt)
68 {
69         if(!joint->bias) return; // early exit
70
71         cpBody *a = joint->constraint.a;
72         cpBody *b = joint->constraint.b;
73         
74         // compute relative rotational velocity
75         cpFloat wr = b->w - a->w;
76         cpFloat ratchet = joint->ratchet;
77         
78         cpFloat jMax = joint->constraint.maxForce*dt;
79         
80         // compute normal impulse       
81         cpFloat j = -(joint->bias + wr)*joint->iSum;
82         cpFloat jOld = joint->jAcc;
83         joint->jAcc = cpfclamp((jOld + j)*ratchet, 0.0f, jMax*cpfabs(ratchet))/ratchet;
84         j = joint->jAcc - jOld;
85         
86         // apply impulse
87         a->w -= j*a->i_inv;
88         b->w += j*b->i_inv;
89 }
90
91 static cpFloat
92 getImpulse(cpRatchetJoint *joint)
93 {
94         return cpfabs(joint->jAcc);
95 }
96
97 static const cpConstraintClass klass = {
98         (cpConstraintPreStepImpl)preStep,
99         (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
100         (cpConstraintApplyImpulseImpl)applyImpulse,
101         (cpConstraintGetImpulseImpl)getImpulse,
102 };
103
104 cpRatchetJoint *
105 cpRatchetJointAlloc(void)
106 {
107         return (cpRatchetJoint *)cpcalloc(1, sizeof(cpRatchetJoint));
108 }
109
110 cpRatchetJoint *
111 cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
112 {
113         cpConstraintInit((cpConstraint *)joint, &klass, a, b);
114         
115         joint->angle = 0.0f;
116         joint->phase = phase;
117         joint->ratchet = ratchet;
118         
119         // STATIC_BODY_CHECK
120         joint->angle = (b ? b->a : 0.0f) - (a ? a->a : 0.0f);
121         
122         return joint;
123 }
124
125 cpConstraint *
126 cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
127 {
128         return (cpConstraint *)cpRatchetJointInit(cpRatchetJointAlloc(), a, b, phase, ratchet);
129 }
130
131 cpBool
132 cpConstraintIsRatchetJoint(const cpConstraint *constraint)
133 {
134         return (constraint->klass == &klass);
135 }
136
137 cpFloat
138 cpRatchetJointGetAngle(const cpConstraint *constraint)
139 {
140         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
141         return ((cpRatchetJoint *)constraint)->angle;
142 }
143
144 void
145 cpRatchetJointSetAngle(cpConstraint *constraint, cpFloat angle)
146 {
147         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
148         cpConstraintActivateBodies(constraint);
149         ((cpRatchetJoint *)constraint)->angle = angle;
150 }
151
152 cpFloat
153 cpRatchetJointGetPhase(const cpConstraint *constraint)
154 {
155         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
156         return ((cpRatchetJoint *)constraint)->phase;
157 }
158
159 void
160 cpRatchetJointSetPhase(cpConstraint *constraint, cpFloat phase)
161 {
162         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
163         cpConstraintActivateBodies(constraint);
164         ((cpRatchetJoint *)constraint)->phase = phase;
165 }
166 cpFloat
167 cpRatchetJointGetRatchet(const cpConstraint *constraint)
168 {
169         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
170         return ((cpRatchetJoint *)constraint)->ratchet;
171 }
172
173 void
174 cpRatchetJointSetRatchet(cpConstraint *constraint, cpFloat ratchet)
175 {
176         cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
177         cpConstraintActivateBodies(constraint);
178         ((cpRatchetJoint *)constraint)->ratchet = ratchet;
179 }