License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / event / dynamics / dynamics-body-config-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/dynamics/dynamics-body-config-impl.h>
20
21 // EXTERNAL HEADERS
22
23 // INTERNAL HEADERS
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/dynamics/dynamics-body-settings.h>
26 #include <dali/internal/event/common/stage-impl.h>
27 #include <dali/internal/event/dynamics/dynamics-capsule-shape-impl.h>
28 #include <dali/internal/event/dynamics/dynamics-cone-shape-impl.h>
29 #include <dali/internal/event/dynamics/dynamics-cube-shape-impl.h>
30 #include <dali/internal/event/dynamics/dynamics-cylinder-shape-impl.h>
31 #include <dali/internal/event/dynamics/dynamics-mesh-shape-impl.h>
32 #include <dali/internal/event/dynamics/dynamics-sphere-shape-impl.h>
33 #include <dali/internal/event/modeling/mesh-impl.h>
34
35 namespace Dali
36 {
37
38 namespace Integration
39 {
40
41 const float DEFAULT_DYNAMICS_BODY_MASS(1.0f);
42 const float DEFAULT_DYNAMICS_BODY_ELASTICITY(0.85f);
43 const float DEFAULT_DYNAMICS_BODY_FRICTION(0.5f);
44 const float DEFAULT_DYNAMICS_BODY_LINEAR_DAMPING(0.0f);
45 const float DEFAULT_DYNAMICS_BODY_ANGULAR_DAMPING(0.0f);
46 const float DEFAULT_DYNAMICS_BODY_LINEAR_SLEEP_VELOCITY(80.0f);     // Assumes default world unit of 1/100
47 const float DEFAULT_DYNAMICS_BODY_ANGULAR_SLEEP_VELOCITY(1.0f);
48 const float DEFAULT_DYNAMICS_LINEAR_STIFFNESS(1.0f);
49 const float DEFAULT_DYNAMICS_ANCHOR_HARDNESS(0.7f);
50
51 } // namespace Integration
52
53 namespace Internal
54 {
55
56 DynamicsBodyConfig::DynamicsBodyConfig()
57 : mSettings(NULL)
58 {
59   DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
60
61   mSettings = new Integration::DynamicsBodySettings;
62   mShape = new DynamicsCubeShape( Vector3::ONE);
63 }
64
65 DynamicsBodyConfig::~DynamicsBodyConfig()
66 {
67   delete mSettings;
68 }
69
70 void DynamicsBodyConfig::SetType( const Dali::DynamicsBodyConfig::BodyType type )
71 {
72   mSettings->type = type;
73 }
74
75 Dali::DynamicsBodyConfig::BodyType DynamicsBodyConfig::GetType() const
76 {
77   return mSettings->type;
78 }
79
80 void DynamicsBodyConfig::SetShape( const Dali::DynamicsShape::ShapeType type, const Vector3& dimensions )
81 {
82   switch( type )
83   {
84     case Dali::DynamicsShape::CAPSULE:
85     {
86       mShape = new DynamicsCapsuleShape(dimensions.x, dimensions.y);
87       break;
88     }
89     case Dali::DynamicsShape::CONE:
90     {
91       mShape = new DynamicsConeShape(dimensions.x, dimensions.y);
92       break;
93     }
94     case Dali::DynamicsShape::CUBE:
95     {
96       mShape = new DynamicsCubeShape(dimensions);
97       break;
98     }
99     case Dali::DynamicsShape::CYLINDER:
100     {
101       mShape = new DynamicsCylinderShape(dimensions.x, dimensions.y);
102       break;
103     }
104     case Dali::DynamicsShape::MESH:
105     {
106       Dali::Mesh mesh( Dali::Mesh::NewPlane( dimensions.x, dimensions.y, dimensions.z, dimensions.z ) );
107       mShape = new DynamicsMeshShape( GetImplementation(mesh) );
108       break;
109     }
110     case Dali::DynamicsShape::SPHERE:
111     {
112       mShape = new DynamicsSphereShape(dimensions.x);
113       break;
114     }
115     default:
116     {
117       DALI_ASSERT_DEBUG(0 && "Unknown shape type" );
118       break;
119     }
120   }
121 }
122
123 void DynamicsBodyConfig::SetShape( DynamicsShapePtr shape )
124 {
125   mShape = shape;
126 }
127
128 DynamicsShapePtr DynamicsBodyConfig::GetShape() const
129 {
130   return mShape;
131 }
132
133 void DynamicsBodyConfig::SetMass( const float mass )
134 {
135   mSettings->mass = mass;
136 }
137
138 float DynamicsBodyConfig::GetMass() const
139 {
140   return mSettings->mass;
141 }
142
143 float DynamicsBodyConfig::GetElasticity() const
144 {
145   return mSettings->elasticity;
146 }
147
148 void DynamicsBodyConfig::SetElasticity(const float elasticity)
149 {
150   mSettings->elasticity = elasticity;
151 }
152
153 float DynamicsBodyConfig::GetFriction() const
154 {
155   return mSettings->friction;
156 }
157
158 void DynamicsBodyConfig::SetFriction(const float friction)
159 {
160   mSettings->friction = Clamp(friction, 0.0f, 1.0f);
161 }
162
163 float DynamicsBodyConfig::GetLinearDamping() const
164 {
165   return mSettings->linearDamping;
166 }
167
168 void DynamicsBodyConfig::SetLinearDamping( const float damping )
169 {
170   mSettings->linearDamping = Clamp(damping, 0.0f, 1.0f);
171 }
172
173 float DynamicsBodyConfig::GetAngularDamping() const
174 {
175   return mSettings->angularDamping;
176 }
177
178 void DynamicsBodyConfig::SetAngularDamping(const float damping)
179 {
180   mSettings->angularDamping = Clamp(damping, 0.0f, 1.0f);
181 }
182
183 float DynamicsBodyConfig::GetLinearSleepVelocity() const
184 {
185   return mSettings->linearSleepVelocity;
186 }
187
188 void DynamicsBodyConfig::SetLinearSleepVelocity( const float sleepVelocity )
189 {
190   mSettings->linearSleepVelocity = sleepVelocity;
191 }
192
193 float DynamicsBodyConfig::GetAngularSleepVelocity() const
194 {
195   return mSettings->angularSleepVelocity;
196 }
197
198 void DynamicsBodyConfig::SetAngularSleepVelocity(const float sleepVelocity)
199 {
200   mSettings->angularSleepVelocity = sleepVelocity;
201 }
202
203 short int DynamicsBodyConfig::GetCollisionGroup() const
204 {
205   return mSettings->collisionGroup;
206 }
207
208 void DynamicsBodyConfig::SetCollisionGroup(const short int collisionGroup)
209 {
210   mSettings->collisionGroup = collisionGroup;
211   mSettings->isCollisionFilterSet = true;
212 }
213
214 short int DynamicsBodyConfig::GetCollisionMask() const
215 {
216   return mSettings->collisionMask;
217 }
218
219 void DynamicsBodyConfig::SetCollisionMask(const short int collisionMask)
220 {
221   mSettings->collisionMask = collisionMask;
222   mSettings->isCollisionFilterSet = true;
223 }
224
225 bool DynamicsBodyConfig::IsCollisionFilterSet() const
226 {
227   return mSettings->isCollisionFilterSet;
228 }
229
230 float DynamicsBodyConfig::GetStiffness() const
231 {
232   return mSettings->linearStiffness;
233 }
234
235 void DynamicsBodyConfig::SetStiffness( const float stiffness )
236 {
237   mSettings->linearStiffness = stiffness;
238 }
239
240 float DynamicsBodyConfig::GetAnchorHardness() const
241 {
242   return mSettings->anchorHardness;
243 }
244
245 void DynamicsBodyConfig::SetAnchorHardness(const float hardness)
246 {
247   mSettings->anchorHardness = Clamp(hardness, 0.0f, 1.0f);
248 }
249
250 float DynamicsBodyConfig::GetVolumeConservation() const
251 {
252   return mSettings->volumeConservation;
253 }
254
255 void DynamicsBodyConfig::SetVolumeConservation(const float conservation)
256 {
257   mSettings->volumeConservation = conservation;
258 }
259
260 float DynamicsBodyConfig::GetShapeConservation() const
261 {
262   return mSettings->shapeConservation;
263 }
264
265 void DynamicsBodyConfig::SetShapeConservation(const float conservation)
266 {
267   mSettings->shapeConservation = conservation;
268 }
269
270 Integration::DynamicsBodySettings* DynamicsBodyConfig::GetSettings() const
271 {
272   return mSettings;
273 }
274
275 } // namespace Internal
276
277 } // namespace Dali