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