1 #ifndef __DALI_DYNAMICS_WORLD_H__
2 #define __DALI_DYNAMICS_WORLD_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
24 // BASE CLASS INCLUDES
25 #include <dali/public-api/object/base-handle.h>
28 #include <dali/devel-api/dynamics/dynamics-world-config.h>
29 #include <dali/public-api/signals/dali-signal.h>
34 namespace Internal DALI_INTERNAL
37 } // namespace Internal
41 class DynamicsCollision;
46 * @brief DynamicsWorld gives the application developer an alternative method of moving and rotating.
47 * actors in the DALi scene.
49 * Actors are represented by DynamicsBody objects in the dynamics simulation and are moved by
50 * forces (eg gravity). Dynamics also allows collisions between objects to be
51 * detected and responded to in signal handlers.
53 * DALi will update the physics simulation after animations and constraints, thus dynamics forces
54 * will override positions and orientations applied by animations and constrints.
56 * Here is an example illustrating the basic steps to initialise the simulation and
60 * // Initialize and get a handle to the Dali::DynamicsWorld
61 * Dali::DynamicsWorldConfig worldConfig( Dali::DynamicsWorldConfig::New() );
62 * Dali::DynamicsWorld dynamicsWorld( Dali::Stage::GetCurrent().InitializeDynamics( worldConfig ) );
64 * // Create an actor to represent the world and act as a parent to DynamicsBody instances
65 * Dali::Actor dynamicsRootActor( Dali::Actor::New() );
66 * dynamicsRootActor.SetParentOrigin( Dali::ParentOrigin::CENTER );
67 * dynamicsWorld.SetRootActor( dynamicsRootActor );
68 * Dali::Stage::GetCurrent().Add( dynamicsRootActor );
70 * // create an actor to represent a rigid body
71 * Dali::Actor actor( Dali::Actor::New() );
72 * actor.SetParentOrigin( Dali::ParentOrigin::CENTER );
74 * // Enable dynamics for the actor, creating a rigid body with default configuration
75 * actor.EnableDynamics( Dali::DynamicsBodyConfig::New() );
77 * // Add the actor to the scene
78 * dynamicsRootActor.Add( actor );
82 * | %Signal Name | Method |
83 * |---------------------------|----------------------------|
84 * | collision | @ref CollisionSignal() |
86 class DALI_IMPORT_API DynamicsWorld : public BaseHandle
91 typedef Signal< void ( DynamicsWorld, const DynamicsCollision ) > CollisionSignalType; ///< Type of collision signal
100 DEBUG_MODE_WIREFRAME = (1<<0),
101 DEBUG_MODE_FAST_WIREFRAME = (1<<1),
102 DEBUG_MODE_AABB = (1<<2),
103 DEBUG_MODE_CONTACT_POINTS = (1<<3),
104 DEBUG_MODE_NO_DEACTIVATION = (1<<4),
105 DEBUG_MODE_CONSTRAINTS = (1<<5),
106 DEBUG_MODE_CONSTRAINTS_LIMITS = (1<<6),
107 DEBUG_MODES_NORMALS = (1<<7),
112 * @brief Create an uninitialized DynamicsWorld handle.
119 * This is non-virtual since derived Handle types must not contain data or virtual methods.
124 * @brief This copy constructor is required for (smart) pointer semantics.
126 * @param [in] handle A reference to the copied handle
128 DynamicsWorld(const DynamicsWorld& handle);
131 * @brief This assignment operator is required for (smart) pointer semantics.
133 * @param [in] rhs A reference to the copied handle
134 * @return A reference to this
136 DynamicsWorld& operator=(const DynamicsWorld& rhs);
139 * @brief Static / singleton creator and getter.
140 * Initialise the Dynamics simulation and create a DynamicsWorld object.
142 * Only one instance of DynamicsWorld will be created, so calling this method multiple times
143 * will return the same DynamicsWorld object.
145 * If an instance already exists, it is returned regardless of configuration being passed in.
147 * @param[in] configuration A DynamicsWorldConfig object describing the required capabilities of the dynamics world.
148 * @return A handle to the world object of the dynamics simulation, or an empty handle if Dynamics is not capable
149 * of supporting a requirement in the configuration as it is not available on the platform.
151 static DynamicsWorld GetInstance( DynamicsWorldConfig configuration );
154 * @brief Static / singleton getter.
155 * Get a handle to the world object of the dynamics simulation.
157 * Does not create an instance, use only if the instance is known to exist.
159 * @return A pointer to the dynamics world if it is installed.
161 static DynamicsWorld Get();
164 * @brief Static instance cleanup.
165 * Terminate the dynamics simulation.
167 * Calls Actor::DisableDynamics on all dynamics enabled actors,
168 * all handles to any DynamicsBody or DynamicsJoint objects held by applications
169 * will become detached from their actors and the simulation therefore should be discarded.
171 static void DestroyInstance();
173 // Methods that modify the simulation
177 * @brief Set the gravity for the world.
179 * @param[in] gravity a Vector3 specifying the applicable gravity for the world.
181 void SetGravity( const Vector3& gravity );
184 * @brief Get the gravity for the world.
186 * @return A Vector3 specifying the gravity that will be applied in the world.
188 const Vector3& GetGravity() const;
191 * @brief Get the current debug drawmode.
193 * @return A combination of the flags in DEBUG_MODES or 0 if debug drawing is currently disabled.
195 int GetDebugDrawMode() const;
198 * @brief Set the debug drawmode for the simulation.
200 * @param[in] mode A combination of the flags in DEBUG_MODES or 0 to disable debug drawing
202 void SetDebugDrawMode(int mode);
205 * @brief Set the actor which will represent the dynamics world.
207 * All actors that will participate in the dynamics simulation must be direct children of this actor
208 * @param[in] actor The root actor for the dynamics simulation
210 void SetRootActor(Actor actor);
213 * @brief Get the root actor for the simulation.
215 * @return The root actor for the dynamics simulation
217 Actor GetRootActor() const;
222 * @brief This signal is emitted when a collision is detected between two DynamicsBodies.
224 * A callback of the following type may be connected:
226 * void YourCollisionHandler(Dali::DynamicsWorld world, const Dali::DynamicsCollision collisionData);
229 * @return The signal to connect to.
231 CollisionSignalType& CollisionSignal();
233 public: // Not intended for application developers
236 * @brief This constructor is used internally by Dali.
238 * @param [in] internal A pointer to a newly allocated Dali resource
240 explicit DALI_INTERNAL DynamicsWorld( Internal::DynamicsWorld* internal );
245 #endif // __DALI_DYNAMICS_WORLD_H__