1 #ifndef __SCENE_GRAPH_DYNAMICS_BODY_H__
2 #define __SCENE_GRAPH_DYNAMICS_BODY_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
39 struct DynamicsBodySettings;
41 } // namespace Integration
55 * @copydoc Dali::DynamicsBody
62 * @param[in] world The object encapsulation the simulation world.
64 DynamicsBody(DynamicsWorld& world, Node& node);
69 virtual ~DynamicsBody();
73 * @param[in] settings Configuration parameters for the new body
75 void Initialize(Integration::DynamicsBodySettings* settings, DynamicsShape* shape);
79 * @param[in] mass The new mass for the body
81 void SetMass( float mass );
83 ///@ copydoc Dali::DynamicsBody::SetElasticity
84 void SetElasticity( float elasticity );
86 /// @copydoc Dali::DynamicsBody::SetLinearVelocity
87 void SetLinearVelocity( const Vector3& velocity );
90 * @copydoc Dali::DynamicsBody::SetLinearVelocity
91 * @param[in] bufferIndex Double buffered data index
93 Vector3 GetLinearVelocity( BufferIndex bufferIndex ) const;
95 /// @copydoc Dali::DynamicsBody::SetAngularVelocity
96 void SetAngularVelocity( const Vector3& velocity );
99 * @copydoc Dali::DynamicsBody::SetLinearVelocity
100 * @param[in] bufferIndex Double buffered data index
102 Vector3 GetAngularVelocity( BufferIndex bufferIndex ) const;
104 /// @copydoc Dali::DynamicsBody::SetKinematic
105 void SetKinematic( bool flag );
107 /// @copydoc Dali::DynamicsBody::IsKinematic
108 bool IsKinematic() const;
110 /// @copydoc Dali::DynamicsBody::SetSleepEnabled
111 void SetSleepEnabled( bool flag );
113 /// @copydoc Dali::DynamicsBody::WakeUp
116 /// @copydoc Dali::DynamicsBody::AddAnchor
117 void AddAnchor( unsigned int index, const DynamicsBody* anchorBody, bool collisions );
119 /// @copydoc Dali::DynamicsBody::ConserveVolume
120 void ConserveVolume( bool flag );
122 /// @copydoc Dali::DynamicsBody::ConserveShape
123 void ConserveShape( bool flag );
125 /// @copydoc Dali::DynamicsBody::GetCollisionGroup
126 short int GetCollisionGroup() const;
128 /// @copydoc Dali::DynamicsBody::SetCollisionGroup
129 void SetCollisionGroup( short int collisionGroup );
131 /// @copydoc Dali::DynamicsBody::GetCollisionMask
132 short int GetCollisionMask() const;
134 /// @copydoc Dali::DynamicsBody::SetCollisionMask
135 void SetCollisionMask( short int collisionMask );
140 * Get a pointer to the physics body object.
141 * @return A pointer to the physics body object.
143 Integration::DynamicsBody* GetBody() const;
146 * Add and connect DynamicsBody to DynamicsWorld
151 * Disconnect DynamicsBody from DynamicsWorld
156 * Delete DynamicsBody from DynamicsWorld
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);
171 // unimplemented copy constructor and assignment operator
172 DynamicsBody(const DynamicsBody&);
173 DynamicsBody& operator=(const DynamicsBody&);
176 Dali::Integration::DynamicsBody* mBody;
179 Node& mNode; // weak reference to the Node
180 DynamicsWorld& mWorld;
183 DoubleBufferedVector3 mLinearVelocity;
184 DoubleBufferedVector3 mAngularVelocity;
186 }; // class DynamicsBody
188 // Messages for DynamicsBody
190 inline void InitializeDynamicsBodyMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, Integration::DynamicsBodySettings* settings, const DynamicsShape& shape )
192 typedef MessageValue2< DynamicsBody, Integration::DynamicsBodySettings*, DynamicsShape* > LocalType;
194 // Reserve some memory inside the message queue
195 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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) );
201 inline void SetMassMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, float mass )
203 typedef MessageValue1< DynamicsBody, float > LocalType;
205 // Reserve some memory inside the message queue
206 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
212 inline void SetElasticityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, float elasticity )
214 typedef MessageValue1< DynamicsBody, float > LocalType;
216 // Reserve some memory inside the message queue
217 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
223 inline void SetLinearVelocityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, const Vector3& velocity )
225 typedef MessageValue1< DynamicsBody, Vector3 > LocalType;
227 // Reserve some memory inside the message queue
228 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
234 inline void SetAngularVelocityMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, const Vector3& velocity )
236 typedef MessageValue1< DynamicsBody, Vector3 > LocalType;
238 // Reserve some memory inside the message queue
239 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
245 inline void SetKinematicMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
247 typedef MessageValue1< DynamicsBody, bool > LocalType;
249 // Reserve some memory inside the message queue
250 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
256 inline void SetSleepEnabledMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
258 typedef MessageValue1< DynamicsBody, bool > LocalType;
260 // Reserve some memory inside the message queue
261 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
267 inline void WakeUpMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
269 typedef Message< DynamicsBody > LocalType;
271 // Reserve some memory inside the message queue
272 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
278 inline void AddAnchorMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, unsigned int index, const DynamicsBody& anchorBody, bool collisions )
280 typedef MessageValue3< DynamicsBody, unsigned int, const DynamicsBody*, bool > LocalType;
282 // Reserve some memory inside the message queue
283 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
289 inline void ConserveVolumeMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
291 typedef MessageValue1< DynamicsBody, bool > LocalType;
293 // Reserve some memory inside the message queue
294 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
300 inline void ConserveShapeMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, bool flag )
302 typedef MessageValue1< DynamicsBody, bool > LocalType;
304 // Reserve some memory inside the message queue
305 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
311 inline void SetCollisionGroupMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, short int collisionGroup )
313 typedef MessageValue1< DynamicsBody, short int > LocalType;
315 // Reserve some memory inside the message queue
316 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
322 inline void SetCollisionMaskMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body, short int collisionMask )
324 typedef MessageValue1< DynamicsBody, short int > LocalType;
326 // Reserve some memory inside the message queue
327 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
333 inline void ConnectMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
335 typedef Message< DynamicsBody > LocalType;
337 // Reserve some memory inside the message queue
338 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
344 inline void DisconnectMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
346 typedef Message< DynamicsBody > LocalType;
348 // Reserve some memory inside the message queue
349 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
355 inline void DeleteBodyMessage( EventToUpdate& eventToUpdate, const DynamicsBody& body )
357 typedef Message< DynamicsBody > LocalType;
359 // Reserve some memory inside the message queue
360 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
366 } // namespace SceneGraph
368 } // namespace Internal
372 #endif // __SCENE_GRAPH_DYNAMICS_BODY_H__