|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDE FILES |
|
19 |
|
20 #include "HgVgSpring.h" |
|
21 #include "HgVgConstants.h" |
|
22 |
|
23 #include <e32math.h> |
|
24 |
|
25 using namespace HgVgConstants; |
|
26 |
|
27 |
|
28 THgVgSpring::THgVgSpring(TReal aSpringK, TReal aSpringDamping, |
|
29 TReal aSpringMaxVelocity, TReal aPositionSnap, |
|
30 TReal aVelocitySnap) : |
|
31 iPrevX(0), |
|
32 iPrevY(0), |
|
33 iX(0), |
|
34 iY(0), |
|
35 iEndX(0), |
|
36 iEndY(0), |
|
37 iVelX(0), |
|
38 iVelY(0), |
|
39 iAccumulator(0), |
|
40 iSpringK(aSpringK), |
|
41 iSpringDamping(aSpringDamping), |
|
42 iMaxSpringVelocity(aSpringMaxVelocity), |
|
43 iPositionSnap(aPositionSnap), |
|
44 iVelocitySnap(aVelocitySnap), |
|
45 iStartX(0), |
|
46 iStartY(0) |
|
47 { |
|
48 |
|
49 } |
|
50 |
|
51 void THgVgSpring::SetConstants(TReal aSpringK, TReal aSpringDamping, |
|
52 TReal aSpringMaxVelocity, TReal aPositionSnap, |
|
53 TReal aVelocitySnap) |
|
54 { |
|
55 iSpringK = aSpringK; |
|
56 iSpringDamping = aSpringDamping; |
|
57 iMaxSpringVelocity = aSpringMaxVelocity; |
|
58 iPositionSnap = aPositionSnap; |
|
59 iVelocitySnap = aVelocitySnap; |
|
60 } |
|
61 |
|
62 |
|
63 void THgVgSpring::Reset(TReal aX, TReal aY) |
|
64 { |
|
65 iEndX = aX; |
|
66 iEndY = aY; |
|
67 Reset(); |
|
68 } |
|
69 |
|
70 void THgVgSpring::Reset() |
|
71 { |
|
72 iPrevX = iX = iStartX = iEndX; |
|
73 iPrevY = iY = iStartY = iEndY; |
|
74 iAccumulator = 0; |
|
75 iVelX = 0; |
|
76 iVelY = 0; |
|
77 } |
|
78 |
|
79 void THgVgSpring::SetXY(TReal aX, TReal aY) |
|
80 { |
|
81 iStartX = iPrevX = iX = aX; |
|
82 iStartY = iPrevY = iY = aY; |
|
83 } |
|
84 |
|
85 void THgVgSpring::SetEnd(TReal aX, TReal aY) |
|
86 { |
|
87 iStartX = iEndX; |
|
88 iStartY = iEndY; |
|
89 iEndX = aX; |
|
90 iEndY = aY; |
|
91 } |
|
92 |
|
93 void THgVgSpring::SetVelocity(TReal aVx, TReal aVy) |
|
94 { |
|
95 iVelX = aVx; |
|
96 iVelY = aVy; |
|
97 } |
|
98 |
|
99 TBool THgVgSpring::IntegratePhysics(TReal aDeltaTime, TBool aEndCondition) |
|
100 { |
|
101 iAccumulator += aDeltaTime; |
|
102 while (iAccumulator >= KTimeStep) |
|
103 { |
|
104 iPrevX = iX; |
|
105 iPrevY = iY; |
|
106 |
|
107 // calculate spring force F = -kx - bv |
|
108 TReal Fx = -(iX - iEndX) * iSpringK - iVelX * iSpringDamping; |
|
109 TReal Fy = -(iY - iEndY) * iSpringK - iVelY * iSpringDamping; |
|
110 |
|
111 // do euler integration |
|
112 iVelX += Fx * KTimeStep; |
|
113 iVelY += Fy * KTimeStep; |
|
114 |
|
115 // limit velocity to certain max |
|
116 if (Abs(iVelX) > iMaxSpringVelocity) |
|
117 iVelX = (iVelX < 0) ? -iMaxSpringVelocity : iMaxSpringVelocity; |
|
118 |
|
119 if (Abs(iVelY) > iMaxSpringVelocity) |
|
120 iVelY = (iVelY < 0) ? -iMaxSpringVelocity : iMaxSpringVelocity; |
|
121 |
|
122 // do euler integration |
|
123 iX += iVelX * KTimeStep; |
|
124 iY += iVelY * KTimeStep; |
|
125 |
|
126 // decrease accumulator |
|
127 iAccumulator -= KTimeStep; |
|
128 |
|
129 // snap to item if close enough and speed is small enough |
|
130 if (Abs(iX - iEndX) < iPositionSnap && Abs(iVelX) < iVelocitySnap && |
|
131 Abs(iY - iEndY) < iPositionSnap && Abs(iVelY) < iVelocitySnap && aEndCondition) |
|
132 { |
|
133 Reset(); |
|
134 return ETrue; |
|
135 } |
|
136 |
|
137 } |
|
138 |
|
139 return EFalse; |
|
140 } |
|
141 |
|
142 TReal THgVgSpring::GetX() const |
|
143 { |
|
144 return iX; |
|
145 } |
|
146 |
|
147 TReal THgVgSpring::GetY() const |
|
148 { |
|
149 return iY; |
|
150 } |
|
151 |
|
152 TReal THgVgSpring::EndX() const |
|
153 { |
|
154 return iEndX; |
|
155 } |
|
156 |
|
157 TReal THgVgSpring::EndY() const |
|
158 { |
|
159 return iEndY; |
|
160 } |
|
161 |
|
162 TReal THgVgSpring::VelX() const |
|
163 { |
|
164 return iVelX; |
|
165 } |
|
166 |
|
167 TReal THgVgSpring::VelY() const |
|
168 { |
|
169 return iVelY; |
|
170 } |
|
171 |
|
172 TReal THgVgSpring::GetInterpolatedX() const |
|
173 { |
|
174 TReal a = iAccumulator / KTimeStep; |
|
175 return iX * (1.0 - a) + iPrevX * a; |
|
176 } |
|
177 |
|
178 TReal THgVgSpring::GetInterpolatedY() const |
|
179 { |
|
180 TReal a = iAccumulator / KTimeStep; |
|
181 return iY * (1.0 - a) + iPrevY * a; |
|
182 } |
|
183 |
|
184 TReal THgVgSpring::StartX() const |
|
185 { |
|
186 return iStartX; |
|
187 } |
|
188 |
|
189 TReal THgVgSpring::StartY() const |
|
190 { |
|
191 return iStartY; |
|
192 } |
|
193 |
|
194 |
|
195 // End of File |