Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / physics-engine / FUiEffects_PePointSurface.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FUiEffects_PePointSurface.cpp
20  * @brief       This file contains an implementation of PointSurface class methods
21  *
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FBaseErrors.h>
26 #include <FUiEffects_PePointSurface.h>
27 #include "../FUiEffects_EffectErrorMessages.h"
28
29 using namespace Tizen::Ui::Effects::_Runtime;
30 using namespace Tizen::Ui::Effects::_Utils;
31
32 namespace Tizen { namespace Ui { namespace Effects { namespace _PhysicsEngine
33 {
34
35 PointSurface::PointSurface(long objId, Vec3f& positionInit, float massInit, float pointEnvResist, bool fixed)
36         : ElementSurface(objId), __positionInit(positionInit), __massInit(massInit), __pointEnvResist(pointEnvResist)
37 {
38         this->__fixed = fixed;
39
40         // those must be initialised in PointSurface::Construct
41         this->__pEnvResist = null;
42         this->__pGravityAcc = null;
43         this->__pGroundLevel = null;
44
45         // initialize after creation
46         InitPoint();
47 }
48
49 PointSurface::~PointSurface()
50 {
51         return;
52 }
53
54 ESurface
55 PointSurface::GetType(void) const
56 {
57         return ESURFACE_POINT_SURFACE;
58 }
59
60 void
61 PointSurface::Initialize(void)
62 {
63         InitPoint();
64         return;
65 }
66
67 bool
68 PointSurface::Construct(const float* pEnvResist, const Vec3f* pGravAcc, const float* pGroundLevel)
69 {
70         SysAssertf(!(pEnvResist == null || pGravAcc == null || pGroundLevel == null), _UiEffectError::INTERNAL_ERROR);
71
72         this->__pEnvResist = pEnvResist;
73         this->__pGravityAcc = pGravAcc;
74         this->__pGroundLevel = pGroundLevel;
75         return true;
76 }
77
78 void
79 PointSurface::Calculate(float timeStep)
80 {
81         // don't move point if fixed setting is enabled
82         if (__fixed)
83         {
84                 __force.set(0.0f);
85                 return;
86         }
87
88         // add user force to total force
89         __force += __forceUser;
90
91         // apply gravity force (m * g)
92         __force += __mass * (*__pGravityAcc);
93
94         // apply environment resistance
95         __force -= __velocity * (__pointEnvResist * (*__pEnvResist));
96
97         __positionPrev = __position;  // save current position
98
99         // a = F(total) / m
100         // using explicit Euler time integration
101         // V = V0 + a * dt
102         // x = x0 + V * dt
103         __velocity += __force * timeStep / __mass;
104         __position += __velocity * timeStep;
105
106         // GroundLevel value needs to be specified
107         if ( __position.z < *__pGroundLevel )
108         {
109                 __position.z = *__pGroundLevel;
110         }
111
112         // reset force for next time step
113         //_vForceUser.set(0.0f);
114         __force.set(0.0f);
115         return;
116 }
117
118 bool
119 PointSurface::IsFixed(void) const
120 {
121         return __fixed;
122 }
123
124 PropertyCast
125 PointSurface::GetProperty(ElementProperty property) const
126 {
127         PropertyCast result;
128         switch (property)
129         {
130         // position coordinates
131         case N_POS_X:
132                 result.type = PropertyCast::NUMBER;
133                 result.value.numberValue = __position.x;
134                 break;
135         case N_POS_Y:
136                 result.type = PropertyCast::NUMBER;
137                 result.value.numberValue = __position.y;
138                 break;
139         case N_POS_Z:
140                 result.type = PropertyCast::NUMBER;
141                 result.value.numberValue = __position.z;
142                 break;
143         // initial position coordinates
144         case N_POS_X_INIT:
145                 result.type = PropertyCast::NUMBER;
146                 result.value.numberValue = __positionInit.x;
147                 break;
148         case N_POS_Y_INIT:
149                 result.type = PropertyCast::NUMBER;
150                 result.value.numberValue = __positionInit.y;
151                 break;
152         case N_POS_Z_INIT:
153                 result.type = PropertyCast::NUMBER;
154                 result.value.numberValue = __positionInit.z;
155                 break;
156         // velocity vector
157         case V_POS:
158                 result.type = PropertyCast::VEC3;
159                 result.value.vec3Value = &__position;
160                 break;
161         // initial velocity vector
162         case V_POS_INIT:
163                 result.type = PropertyCast::VEC3;
164                 result.value.vec3Value = &__positionInit;
165                 break;
166         // user force coordinates
167         case N_FUSER_X:
168                 result.type = PropertyCast::NUMBER;
169                 result.value.numberValue = __forceUser.x;
170                 break;
171         case N_FUSER_Y:
172                 result.type = PropertyCast::NUMBER;
173                 result.value.numberValue = __forceUser.y;
174                 break;
175         case N_FUSER_Z:
176                 result.type = PropertyCast::NUMBER;
177                 result.value.numberValue = __forceUser.z;
178                 break;
179         // user force vector
180         case V_FUSER:
181                 result.type = PropertyCast::VEC3;
182                 result.value.vec3Value = &__forceUser;
183                 break;
184         // node mass
185         case N_MASS:
186                 result.type = PropertyCast::NUMBER;
187                 result.value.numberValue = __mass;
188                 break;
189         // node resistance
190         case N_POINT_RESISTANCE:
191                 result.type = PropertyCast::NUMBER;
192                 result.value.numberValue = __pointEnvResist;
193                 break;
194         // fixation setting
195         case B_IS_FIXED:
196                 result.type = PropertyCast::BOOL;
197                 result.value.boolValue = __fixed;
198                 break;
199         default:
200                 result = ElementSurface::GetProperty(property);
201                 break;
202         }
203         return result;
204 }
205
206 bool
207 PointSurface::SetProperty(ElementProperty property, bool boolValue)
208 {
209         bool result = true;     // for successful set
210         switch (property)
211         {
212         // fixation setting
213         case B_IS_FIXED:
214                 __fixed = boolValue;
215                 break;
216         default:
217                 result = ElementSurface::SetProperty(property, boolValue);
218                 break;
219         }
220         return result;
221 }
222
223 bool
224 PointSurface::SetProperty(ElementProperty property, LUA_NUMBER numericValue)
225 {
226         bool result = true;     // for successful set
227         switch (property)
228         {
229         // position coordinates
230         case N_POS_X:
231                 __position.x = numericValue;
232                 break;
233         case N_POS_Y:
234                 __position.y = numericValue;
235                 break;
236         case N_POS_Z:
237                 __position.z = numericValue;
238                 break;
239         // user force coordinates
240         case N_FUSER_X:
241                 __forceUser.x = numericValue;
242                 break;
243         case N_FUSER_Y:
244                 __forceUser.y = numericValue;
245                 break;
246         case N_FUSER_Z:
247                 __forceUser.z = numericValue;
248                 break;
249         // node mass
250         case N_MASS:
251                 __mass = numericValue;
252                 break;
253         // node resistance
254         case N_POINT_RESISTANCE:
255                 __pointEnvResist = numericValue;
256                 break;
257         default:
258                 result = ElementSurface::SetProperty(property, numericValue);
259                 break;
260         }
261         return result;
262 }
263
264 bool
265 PointSurface::SetProperty(ElementProperty property, const Vec3f& vectorValue)
266 {
267         bool result = true;     // for successful set
268         switch (property)
269         {
270         // velocity vector
271         case V_POS:
272                 __position = vectorValue;
273                 break;
274         // user force vector
275         case V_FUSER:
276                 __forceUser = vectorValue;
277                 break;
278         default:
279                 result = ElementSurface::SetProperty(property, vectorValue);
280                 break;
281         }
282         return result;
283 }
284
285 const Vec3f&
286 PointSurface::GetPosition(void) const
287 {
288         return __position;
289 }
290
291 const Vec3f&
292 PointSurface::GetVelocity(void) const
293 {
294         return __velocity;
295 }
296
297 void
298 PointSurface::AddForce(Vec3f appliedForce)
299 {
300         __force += appliedForce;
301         return;
302 }
303
304 void
305 PointSurface::AddVelocity(Vec3f newVelocity)
306 {
307         __velocity += newVelocity;
308         return;
309 }
310
311 void
312 PointSurface::InitPoint(void)
313 {
314         // setting simulation initial position and mass
315         __position = __positionPrev = __positionInit;
316         __mass = __massInit;
317
318         // the point is considered still in the beginning
319         __velocity.set(0.0f);
320         __force.set(0.0f);
321         __forceUser.set(0.0f);
322
323         return;
324 }
325
326 } } } }//Tizen::Ui::Ext::Effects3d::_PhysicsEngine