2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-physics/internal/physics-adaptor-impl.h>
25 #include <dali-physics/internal/physics-world-impl.h>
26 #include <dali/dali.h>
27 #include <dali/devel-api/threading/mutex.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/public-api/actors/drawable-actor.h>
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS");
39 namespace Dali::Toolkit::Physics::Internal
41 PhysicsAdaptor::PhysicsAdaptor()
46 PhysicsAdaptor::~PhysicsAdaptor()
50 void PhysicsAdaptor::Initialize(const Dali::Matrix& transform, Uint16Pair worldSize)
52 // Create an actor that can handle mouse events.
53 // @todo Enable this to be fully configured / provided
54 mRootActor = Layer::New();
55 mRootActor[Actor::Property::NAME] = "PhysicsRootLayer";
56 mRootActor[Layer::Property::BEHAVIOR] = Layer::LAYER_3D;
57 mRootActor[Layer::Property::DEPTH_TEST] = true;
58 mRootActor[Actor::Property::SIZE] = Vector2(worldSize.GetWidth(), worldSize.GetHeight());
59 mRootActor[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER;
60 mRootActor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
62 // Initialize derived adaptor (and world)
63 OnInitialize(transform, worldSize);
66 void PhysicsAdaptor::SetTimestep(float timestep)
68 mPhysicsWorld->SetTimestep(timestep);
71 float PhysicsAdaptor::GetTimestep() const
73 return mPhysicsWorld->GetTimestep();
76 Physics::PhysicsAdaptor::ScopedPhysicsAccessorPtr PhysicsAdaptor::GetPhysicsAccessor()
78 return std::unique_ptr<Physics::PhysicsAdaptor::ScopedPhysicsAccessor>(new Physics::PhysicsAdaptor::ScopedPhysicsAccessor(*mPhysicsWorld.get()));
81 void PhysicsAdaptor::SetIntegrationState(Physics::PhysicsAdaptor::IntegrationState state)
83 mPhysicsWorld->SetIntegrationState(state);
86 Physics::PhysicsAdaptor::IntegrationState PhysicsAdaptor::GetIntegrationState() const
88 return mPhysicsWorld->GetIntegrationState();
91 void PhysicsAdaptor::SetDebugState(Physics::PhysicsAdaptor::DebugState state)
93 mPhysicsWorld->SetDebugState(state);
96 Physics::PhysicsAdaptor::DebugState PhysicsAdaptor::GetDebugState() const
98 return mPhysicsWorld->GetDebugState();
101 Dali::Actor PhysicsAdaptor::GetRootActor() const
106 void PhysicsAdaptor::OnUpdateActors(Dali::UpdateProxy* updateProxy)
108 for(auto&& actor : mPhysicsActors)
110 // Get position, orientation from physics world.
111 Vector3 position = actor.second->GetActorPosition();
112 updateProxy->BakePosition(actor.first, position);
113 Quaternion rotation = actor.second->GetActorRotation();
114 updateProxy->BakeOrientation(actor.first, rotation);
118 void PhysicsAdaptor::Queue(std::function<void()> function)
120 mPhysicsWorld->Queue(function);
123 void PhysicsAdaptor::CreateSyncPoint()
125 mPhysicsWorld->CreateSyncPoint();
128 std::unique_ptr<PhysicsWorld>& PhysicsAdaptor::GetPhysicsWorld()
130 return mPhysicsWorld;
133 } // namespace Dali::Toolkit::Physics::Internal