1 #ifndef DALI_TOOLKIT_PHYSICS_INTERNAL_PHYSICS_WORLD_H
2 #define DALI_TOOLKIT_PHYSICS_INTERNAL_PHYSICS_WORLD_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.
20 #include <dali/dali.h>
21 #include <dali/devel-api/update/frame-callback-interface.h>
22 #include <dali/devel-api/update/update-proxy.h>
24 #include <dali-physics/public-api/physics-adaptor.h>
30 namespace Dali::Toolkit::Physics::Internal
36 * Abstract class that handles the update frame callback, queuing and calling
37 * functions before the integration step ; calling the integration step,
38 * and owning the mutex for the update callback.
40 * Implementing classes should also hold the physics world.
46 * Create a new physics world.
48 static std::unique_ptr<PhysicsWorld> New(Dali::Actor rootActor, Dali::CallbackBase* updateCallback);
51 * Constructor which takes the root actor and a callback from the PhysicsAdaptor
52 * @param[in] rootActor The root actor that physics actors will be added to
53 * @param[in] updateCallback A callback from the PhysicsAdaptor which updates the physics actors after the integration step
55 PhysicsWorld(Dali::Actor rootActor, Dali::CallbackBase* updateCallback);
59 * Note, removes the frame callback.
61 virtual ~PhysicsWorld();
64 * Initialize derived classes and creates the frame callback
69 * Initialize the derived class
71 virtual void OnInitialize() = 0;
74 * Get the native physics world / space.
75 * @return A pointer to the physics world / space
77 virtual Dali::Any GetNative() = 0;
80 * Set how long the integration should take.
81 * @param[in] timestep The length of time that the physics integration should take.
83 void SetTimestep(float timestep);
86 * Get the current physics integration timestep
87 * @return the current physics integration timestep
104 ScopedLock(PhysicsWorld& world)
113 PhysicsWorld& mWorld;
117 * Queue a function for execution in the update thread, prior to the physics integration.
118 * Enables syncronization of DALi properties and physics controlled properties.
120 void Queue(std::function<void(void)> function);
123 * Create a sync point for queued functions.
125 * Ensures that any queued functions are processed after this sync
126 * point is seen in the Update::FrameCallback, which will be in the
127 * same frame as any other DALi properties set during this event
128 * handler invocation.
132 void CreateSyncPoint();
135 * Hit test the physics world and return the nearest body.
137 * @param[in] rayFromWorld The origin in physics world space
138 * @param[in] rayToWorld A point along the direction on the far side of the physics world
139 * @param[in] nativeFilter a native body / shape filter
140 * @param[out] localPivot The hit point local to the body
141 * @param[out] distanceFromCamera The distance of the pick point from the camera
142 * @return Empty value if no dynamic body found, otherwise a valid ptr to the hit body.
144 virtual Dali::Any HitTest(Dali::Vector3 rayFromWorld, Dali::Vector3 rayToWorld, Dali::Any nativeFilter, Dali::Vector3& localPivot, float& distanceFromCamera) = 0;
147 * @copydoc Dali::Toolkit::Physics::PhysicsAdaptor::SetIntegrationState
149 void SetIntegrationState(Physics::PhysicsAdaptor::IntegrationState state);
152 * @copydoc Dali::Toolkit::Physics::PhysicsAdaptor::GetIntegrationState
154 Physics::PhysicsAdaptor::IntegrationState GetIntegrationState();
157 * @copydoc Dali::Toolkit::Physics::PhysicsAdaptor::SetDebugState
159 void SetDebugState(Physics::PhysicsAdaptor::DebugState state);
162 * @copydoc Dali::Toolkit::Physics::PhysicsAdaptor::GetDebugState
164 Physics::PhysicsAdaptor::DebugState GetDebugState();
167 bool OnUpdate(Dali::UpdateProxy& updateProxy, float elapsedSeconds);
170 virtual void Integrate(float timestep) = 0;
174 std::queue<std::function<void(void)>> commandQueue;
175 Dali::UpdateProxy::NotifySyncPoint mNotifySyncPoint{Dali::UpdateProxy::INVALID_SYNC};
176 Dali::CallbackBase* mUpdateCallback{nullptr};
177 std::unique_ptr<FrameCallback> mFrameCallback;
178 Dali::Actor mRootActor;
180 float mPhysicsTimeStep{1.0 / 180.0};
181 Physics::PhysicsAdaptor::IntegrationState mPhysicsIntegrateState{Physics::PhysicsAdaptor::IntegrationState::ON};
182 Physics::PhysicsAdaptor::DebugState mPhysicsDebugState{Physics::PhysicsAdaptor::DebugState::OFF};
185 } // namespace Dali::Toolkit::Physics::Internal
187 #endif //DALI_TOOLKIT_PHYSICS_INTERNAL_PHYSICS_WORLD_H