1 #ifndef DALI_TOOLKIT_PHYSICS_ACTOR_H
2 #define DALI_TOOLKIT_PHYSICS_ACTOR_H
5 * Copyright (c) 2023 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.
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/object/any.h>
23 #include <dali/public-api/object/base-handle.h>
26 #include <dali-toolkit/public-api/dali-toolkit-common.h>
28 namespace Dali::Toolkit::Physics
38 * Class to associate a physics body (such as btRigidBody*) with a DALi
39 * actor for rendering.
41 * This object offers methods to modify basic physics properties
42 * asynchronously, that is, on the Update thread. It is also possible
43 * for the developer to queue other physics functions on the body by
44 * using PhysicsWorld::Queue() and passing in a lambda that captures the
48 * btRigidBody* body = physicsActor.GetBody().Get<btRigidBody*>();
49 * mPhysicsImpl.Queue([body](){ body->clearForces(); });
50 * This enables the developer to synchronize setting physics properties
51 * and setting DALi actor properties.
53 class DALI_TOOLKIT_API PhysicsActor : public Dali::BaseHandle
67 * This is non-virtual since derived Handle types must not contain data or virtual methods
72 * @brief Copy Constructor.
75 * @param[in] handle The handle to copy
76 * @note This creates a new handle, but does not create a new implementation object.
78 PhysicsActor(const PhysicsActor& handle);
81 * @brief Move Constructor.
84 * @param[in] handle A reference to the handle to move
86 PhysicsActor(PhysicsActor&& rhs) noexcept;
89 * @brief Assignment operator.
92 * @param[in] handle A reference to the handle to move
93 * @return a reference to this handle
95 PhysicsActor& operator=(const PhysicsActor& handle);
98 * @brief Move Assignment operator.
101 * @param[in] handle A reference to the handle to move
102 * @return a reference to this handle
104 PhysicsActor& operator=(PhysicsActor&& handle) noexcept;
110 * Binds the actor to the given body. This should be a body that has
111 * been added to the physics world, and has physical postion and
112 * rotation in that space. The Actor is used to render that object in
115 * @note This object only stores the actor ID, which is used
116 * internally in the FrameCallback. It is up to the caller to ensure
117 * there is a reference to the actor (e.g. by parenting into the scene)
119 * @note This API is used internally, and is of little benefit to the developer.
121 * @param[in] actor The DALi actor used to render this object
122 * @param[in] body The physics body used in the physics simulation
123 * @param[in] adaptor The physics adaptor to use. @todo remove?
125 static PhysicsActor New(Dali::Actor actor, Dali::Any body, PhysicsAdaptor adaptor);
128 * @brief Downcasts a handle to PhysicsActor handle.
130 * If handle points to an PhysicsActor object, the downcast produces valid handle.
131 * If not, the returned handle is left uninitialized.
134 * @param[in] handle to an object
135 * @return handle to a PhysicsActor object or an uninitialized handle
137 static PhysicsActor DownCast(BaseHandle handle);
140 * @brief Get the actor ID of the associated actor.
144 uint32_t GetId() const;
147 * @brief Get the actual physics body of this object.
150 * Using ANY wrapper to enable this interface to be used for any
151 * types of physics bodies from either 2d or 3d physics.
152 * @return The physics body. It can be cast to an appropriate type,
154 * btRigidBody* body = actor.GetBody().Get<btRigidBody*>();
156 * @note Please ensure to get a scoped accessor first to avoid modifying
157 * physics properties during integration step, or use the Async methods
160 Dali::Any GetBody() const;
163 * @brief Queue a method to set the position on the associated physics body
164 * in the update thread before the next integration.
167 * @param[in] actorPosition The position of the actor in DALi space
169 void AsyncSetPhysicsPosition(Dali::Vector3 actorPosition);
172 * @brief Queue a method to set the rotation of the associated physics body
173 * in the update thread before the next integration.
176 * @param[in] actorRotation The orientation of the actor in DALi space
178 void AsyncSetPhysicsRotation(Dali::Quaternion actorRotation);
181 * @brief Get the current position of the physics body in Physics space.
184 * @return the current position of the physics body in Physics space.
186 Dali::Vector3 GetPhysicsPosition() const;
189 * @brief Get the current rotation of the physics body in Physics space.
192 * @return the current rotation of the physics body in Physics space.
194 Dali::Quaternion GetPhysicsRotation() const;
197 * @brief Get the current position of the physics body in DALi space.
200 * @return the current position of the physics body in DALi space.
202 Dali::Vector3 GetActorPosition() const;
205 * @brief Get the current rotation of the physics body in DALi space.
208 * @return the current rotation of the physics body in DALi space.
210 Dali::Quaternion GetActorRotation() const;
212 public: // Not intended for developer use
215 * @brief This constructor is used by PhysicsActor::New() methods.
218 * @param[in] impl A pointer to a newly allocated Dali resource.
219 * @note Not intended for application developers
221 explicit DALI_INTERNAL PhysicsActor(Internal::PhysicsActor* impl);
225 } // namespace Dali::Toolkit::Physics
227 #endif //DALI_TOOLKIT_PHYSICS_ACTOR_H