Emscripten workarounds and llvm syntax fixes
[platform/core/uifw/dali-core.git] / dali / internal / update / dynamics / scene-graph-dynamics-body.h
1 #ifndef __SCENE_GRAPH_DYNAMICS_BODY_H__
2 #define __SCENE_GRAPH_DYNAMICS_BODY_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 // INTERNAL HEADERS
21 #include <dali/internal/common/message.h>
22 #include <dali/internal/common/event-to-update.h>
23 #include <dali/internal/common/owner-pointer.h>
24 #include <dali/internal/event/dynamics/dynamics-declarations.h>
25 #include <dali/internal/update/common/double-buffered.h>
26 #include <dali/internal/update/modeling/scene-graph-mesh-declarations.h>
27 #include <dali/public-api/math/vector3.h>
28
29 namespace Dali
30 {
31
32 class MeshData;
33 class Quaternion;
34
35 namespace Integration
36 {
37
38 class DynamicsBody;
39 struct DynamicsBodySettings;
40
41 } // namespace Integration
42
43 namespace Internal
44 {
45
46 namespace SceneGraph
47 {
48
49 class Node;
50 class DynamicsBody;
51 class DynamicsShape;
52 class DynamicsWorld;
53
54 /**
55  * @copydoc Dali::DynamicsBody
56  */
57 class DynamicsBody
58 {
59 public:
60   /**
61    * Constructor.
62    * @param[in] world The object encapsulation the simulation world.
63    */
64   DynamicsBody(DynamicsWorld& world, Node& node);
65
66   /**
67    * Destructor.
68    */
69   virtual ~DynamicsBody();
70
71   /**
72    * Initialize the body
73    * @param[in] settings Configuration parameters for the new body
74    */
75   void Initialize(Integration::DynamicsBodySettings* settings, DynamicsShape* shape);
76
77   /**
78    * Set the mass
79    * @param[in] mass The new mass for the body
80    */
81   void SetMass( float mass );
82
83   ///@ copydoc Dali::DynamicsBody::SetElasticity
84   void SetElasticity( float elasticity );
85
86   /// @copydoc Dali::DynamicsBody::SetLinearVelocity
87   void SetLinearVelocity( const Vector3& velocity );
88
89   /**
90    * @copydoc Dali::DynamicsBody::SetLinearVelocity
91    * @param[in] bufferIndex Double buffered data index
92    */
93   Vector3 GetLinearVelocity( BufferIndex bufferIndex ) const;
94
95   /// @copydoc Dali::DynamicsBody::SetAngularVelocity
96   void SetAngularVelocity( const Vector3& velocity );
97
98   /**
99    * @copydoc Dali::DynamicsBody::SetLinearVelocity
100    * @param[in] bufferIndex Double buffered data index
101    */
102   Vector3 GetAngularVelocity( BufferIndex bufferIndex ) const;
103
104   /// @copydoc Dali::DynamicsBody::SetKinematic
105   void SetKinematic( bool flag );
106
107   /// @copydoc Dali::DynamicsBody::IsKinematic
108   bool IsKinematic() const;
109
110   /// @copydoc Dali::DynamicsBody::SetSleepEnabled
111   void SetSleepEnabled( bool flag );
112
113   /// @copydoc Dali::DynamicsBody::WakeUp
114   void WakeUp();
115
116   /// @copydoc Dali::DynamicsBody::AddAnchor
117   void AddAnchor( unsigned int index, const DynamicsBody* anchorBody, bool collisions );
118
119   /// @copydoc Dali::DynamicsBody::ConserveVolume
120   void ConserveVolume( bool flag );
121
122   /// @copydoc Dali::DynamicsBody::ConserveShape
123   void ConserveShape( bool flag );
124
125   /// @copydoc Dali::DynamicsBody::GetCollisionGroup
126   short int GetCollisionGroup() const;
127
128   /// @copydoc Dali::DynamicsBody::SetCollisionGroup
129   void SetCollisionGroup( short int collisionGroup );
130
131   /// @copydoc Dali::DynamicsBody::GetCollisionMask
132   short int GetCollisionMask() const;
133
134   /// @copydoc Dali::DynamicsBody::SetCollisionMask
135   void SetCollisionMask( short int collisionMask );
136
137   int GetType() const;
138
139   /**
140    * Get a pointer to the physics body object.
141    * @return A pointer to the physics body object.
142    */
143   Integration::DynamicsBody* GetBody() const;
144
145   /**
146    * Add and connect DynamicsBody to DynamicsWorld
147    */
148   void Connect();
149
150   /**
151    * Disconnect DynamicsBody from DynamicsWorld
152    */
153   void Disconnect();
154
155   /**
156    * Delete DynamicsBody from DynamicsWorld
157    */
158   void Delete();
159
160   /**
161    * TODO comment us
162    */
163   void SetMotionState();
164   void GetNodePositionAndRotation(Vector3& position, Quaternion& rotation);
165   void SetNodePositionAndRotation(const Vector3& position, const Quaternion& rotation);
166   void RefreshDynamics();
167   bool RefreshNode(BufferIndex updateBufferIndex);
168   void RefreshMesh(BufferIndex updateBufferIndex);
169
170 private:
171   // unimplemented copy constructor and assignment operator
172   DynamicsBody(const DynamicsBody&);
173   DynamicsBody& operator=(const DynamicsBody&);
174
175 public:
176   Dali::Integration::DynamicsBody* mBody;
177
178 private:
179   Node&          mNode;      // weak reference to the Node
180   DynamicsWorld& mWorld;
181   Mesh*          mMesh;
182
183   DoubleBufferedVector3 mLinearVelocity;
184   DoubleBufferedVector3 mAngularVelocity;
185
186 }; // class DynamicsBody
187
188 // Messages for DynamicsBody
189
190 inline void InitializeDynamicsBodyMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, Integration::DynamicsBodySettings* settings, const DynamicsShape& shape )
191 {
192   typedef MessageValue2< DynamicsBody, Integration::DynamicsBodySettings*, DynamicsShape* > LocalType;
193
194   // Reserve some memory inside the message queue
195   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
196
197   // Construct message in the message queue memory; note that delete should not be called on the return value
198   new (slot) LocalType( &body, &DynamicsBody::Initialize, settings, &const_cast<DynamicsShape&>(shape) );
199 }
200
201 inline void SetMassMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, float mass )
202 {
203   typedef MessageValue1< DynamicsBody, float > LocalType;
204
205   // Reserve some memory inside the message queue
206   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
207
208   // Construct message in the message queue memory; note that delete should not be called on the return value
209   new (slot) LocalType( &body, &DynamicsBody::SetMass, mass );
210 }
211
212 inline void SetElasticityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, float elasticity )
213 {
214   typedef MessageValue1< DynamicsBody, float > LocalType;
215
216   // Reserve some memory inside the message queue
217   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
218
219   // Construct message in the message queue memory; note that delete should not be called on the return value
220   new (slot) LocalType( &body, &DynamicsBody::SetElasticity, elasticity );
221 }
222
223 inline void SetLinearVelocityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, const Vector3& velocity )
224 {
225   typedef MessageValue1< DynamicsBody, Vector3 > LocalType;
226
227   // Reserve some memory inside the message queue
228   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
229
230   // Construct message in the message queue memory; note that delete should not be called on the return value
231   new (slot) LocalType( &body, &DynamicsBody::SetLinearVelocity, velocity );
232 }
233
234 inline void SetAngularVelocityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, const Vector3& velocity )
235 {
236   typedef MessageValue1< DynamicsBody, Vector3 > LocalType;
237
238   // Reserve some memory inside the message queue
239   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
240
241   // Construct message in the message queue memory; note that delete should not be called on the return value
242   new (slot) LocalType( &body, &DynamicsBody::SetAngularVelocity, velocity );
243 }
244
245 inline void SetKinematicMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
246 {
247   typedef MessageValue1< DynamicsBody, bool > LocalType;
248
249   // Reserve some memory inside the message queue
250   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
251
252   // Construct message in the message queue memory; note that delete should not be called on the return value
253   new (slot) LocalType( &body, &DynamicsBody::SetKinematic, flag );
254 }
255
256 inline void SetSleepEnabledMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
257 {
258   typedef MessageValue1< DynamicsBody, bool > LocalType;
259
260   // Reserve some memory inside the message queue
261   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
262
263   // Construct message in the message queue memory; note that delete should not be called on the return value
264   new (slot) LocalType( &body, &DynamicsBody::SetSleepEnabled, flag );
265 }
266
267 inline void WakeUpMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
268 {
269   typedef Message< DynamicsBody > LocalType;
270
271   // Reserve some memory inside the message queue
272   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
273
274   // Construct message in the message queue memory; note that delete should not be called on the return value
275   new (slot) LocalType( &body, &DynamicsBody::WakeUp );
276 }
277
278 inline void AddAnchorMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, unsigned int index, const DynamicsBody& anchorBody, bool collisions )
279 {
280   typedef MessageValue3< DynamicsBody, unsigned int, const DynamicsBody*, bool > LocalType;
281
282   // Reserve some memory inside the message queue
283   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
284
285   // Construct message in the message queue memory; note that delete should not be called on the return value
286   new (slot) LocalType( &body, &DynamicsBody::AddAnchor, index, &anchorBody, collisions );
287 }
288
289 inline void ConserveVolumeMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
290 {
291   typedef MessageValue1< DynamicsBody, bool > LocalType;
292
293   // Reserve some memory inside the message queue
294   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
295
296   // Construct message in the message queue memory; note that delete should not be called on the return value
297   new (slot) LocalType( &body, &DynamicsBody::ConserveVolume, flag );
298 }
299
300 inline void ConserveShapeMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
301 {
302   typedef MessageValue1< DynamicsBody, bool > LocalType;
303
304   // Reserve some memory inside the message queue
305   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
306
307   // Construct message in the message queue memory; note that delete should not be called on the return value
308   new (slot) LocalType( &body, &DynamicsBody::ConserveShape, flag );
309 }
310
311 inline void SetCollisionGroupMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, short int collisionGroup )
312 {
313   typedef MessageValue1< DynamicsBody, short int > LocalType;
314
315   // Reserve some memory inside the message queue
316   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
317
318   // Construct message in the message queue memory; note that delete should not be called on the return value
319   new (slot) LocalType( &body, &DynamicsBody::SetCollisionGroup, collisionGroup );
320 }
321
322 inline void SetCollisionMaskMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, short int collisionMask )
323 {
324   typedef MessageValue1< DynamicsBody, short int > LocalType;
325
326   // Reserve some memory inside the message queue
327   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
328
329   // Construct message in the message queue memory; note that delete should not be called on the return value
330   new (slot) LocalType( &body, &DynamicsBody::SetCollisionMask, collisionMask );
331 }
332
333 inline void ConnectMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
334 {
335   typedef Message< DynamicsBody > LocalType;
336
337   // Reserve some memory inside the message queue
338   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
339
340   // Construct message in the message queue memory; note that delete should not be called on the return value
341   new (slot) LocalType( &body, &DynamicsBody::Connect );
342 }
343
344 inline void DisconnectMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
345 {
346   typedef Message< DynamicsBody > LocalType;
347
348   // Reserve some memory inside the message queue
349   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
350
351   // Construct message in the message queue memory; note that delete should not be called on the return value
352   new (slot) LocalType( &body, &DynamicsBody::Disconnect );
353 }
354
355 inline void DeleteBodyMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
356 {
357   typedef Message< DynamicsBody > LocalType;
358
359   // Reserve some memory inside the message queue
360   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
361
362   // Construct message in the message queue memory; note that delete should not be called on the return value
363   new (slot) LocalType( &body, &DynamicsBody::Delete );
364 }
365
366 } // namespace SceneGraph
367
368 } // namespace Internal
369
370 } // namespace Dali
371
372 #endif // __SCENE_GRAPH_DYNAMICS_BODY_H__