[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / internal / chipmunk-impl / chipmunk-physics-adaptor-impl.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // Class Header
18 #include <dali-physics/internal/chipmunk-impl/chipmunk-physics-adaptor-impl.h>
19
20 // External Headers
21 #include <utility>
22
23 // Internal Headers
24 #include <dali-physics/internal/chipmunk-impl/chipmunk-physics-world-impl.h>
25 #include <dali/dali.h>
26 #include <dali/integration-api/debug.h>
27
28 namespace
29 {
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS");
32 #endif
33
34 inline cpVect ConvertVector(Dali::Vector3 vector)
35 {
36   return cpv(vector.x, vector.y);
37 }
38
39 } // namespace
40
41 namespace Dali::Toolkit::Physics::Internal
42 {
43 PhysicsAdaptorPtr CreateNewPhysicsAdaptor(const Dali::Matrix& transform, Uint16Pair worldSize)
44 {
45   PhysicsAdaptorPtr adaptor(new ChipmunkPhysicsAdaptor());
46   adaptor->Initialize(transform, worldSize);
47   return adaptor;
48 }
49
50 ChipmunkPhysicsAdaptor::ChipmunkPhysicsAdaptor()
51 : PhysicsAdaptor()
52 {
53 }
54
55 ChipmunkPhysicsAdaptor::~ChipmunkPhysicsAdaptor()
56 {
57   // @todo Ensure physics bodies don't leak
58 }
59
60 void ChipmunkPhysicsAdaptor::OnInitialize(const Dali::Matrix& transform, Uint16Pair worldSize)
61 {
62   mTransform        = transform;
63   mInverseTransform = transform;
64   mInverseTransform.Invert();
65   mSize = worldSize;
66
67   mPhysicsWorld = ChipmunkPhysicsWorld::New(mRootActor,
68                                             Dali::MakeCallback(mSlotDelegate.GetSlot(),
69                                                                &PhysicsAdaptor::OnUpdateActors));
70 }
71
72 Layer ChipmunkPhysicsAdaptor::CreateDebugLayer(Dali::Window window)
73 {
74   Layer debugLayer;
75
76   auto renderTaskList = window.GetRenderTaskList();
77   auto renderTask     = renderTaskList.GetTask(0);
78   auto windowSize     = window.GetSize();
79
80   debugLayer                                 = Layer::New();
81   debugLayer[Actor::Property::NAME]          = "PhysicsDebugLayer";
82   debugLayer[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;
83   debugLayer[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
84
85   Constraint positionConstraint = Constraint::New<Vector3>(debugLayer, Actor::Property::POSITION, EqualToConstraint());
86   positionConstraint.AddSource(Source(mRootActor, Actor::Property::POSITION));
87   positionConstraint.Apply();
88   Constraint sizeConstraint = Constraint::New<Vector2>(debugLayer, Actor::Property::SIZE, EqualToConstraint());
89   sizeConstraint.AddSource(Source(mRootActor, Actor::Property::SIZE));
90   sizeConstraint.Apply();
91
92   auto world = static_cast<ChipmunkPhysicsWorld*>(mPhysicsWorld.get());
93
94   std::unique_ptr<PhysicsDebugRenderer> debugRenderer = PhysicsDebugRenderer::New(windowSize.GetWidth(), windowSize.GetHeight(), renderTask.GetCameraActor(), this);
95
96   mDebugActor = DrawableActor::New(*(debugRenderer->GetCallback().get()));
97   world->SetDebugRenderer(debugRenderer.release());
98
99   mDebugActor[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;
100   mDebugActor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
101
102   Constraint sizeConstraint2 = Constraint::New<Vector2>(mDebugActor, Actor::Property::SIZE, EqualToConstraint());
103   sizeConstraint2.AddSource(ParentSource(Actor::Property::SIZE));
104   sizeConstraint2.Apply();
105
106   debugLayer.Add(mDebugActor);
107
108   window.Add(debugLayer);
109   return debugLayer;
110 }
111
112 void ChipmunkPhysicsAdaptor::SetTransformAndSize(const Dali::Matrix& transform, Uint16Pair worldSize)
113 {
114   mTransform        = transform;
115   mInverseTransform = transform;
116   mInverseTransform.Invert();
117   mSize = worldSize;
118
119   GetRootActor()[Actor::Property::SIZE] = Vector3(worldSize.GetWidth(), worldSize.GetHeight(), 0);
120
121   auto world = static_cast<ChipmunkPhysicsWorld*>(mPhysicsWorld.get());
122   if(world->HasDebugRenderer())
123   {
124     Actor layer                  = mDebugActor.GetParent();
125     layer[Actor::Property::SIZE] = Vector3(worldSize);
126     world->GetDebugRenderer().UpdateWindowSize(worldSize);
127   }
128 }
129
130 PhysicsActorPtr ChipmunkPhysicsAdaptor::AddActorBody(Dali::Actor actor, Dali::Any body)
131 {
132   uint32_t id    = static_cast<uint32_t>(actor.GetProperty<int>(Actor::Property::ID));
133   cpBody*  cBody = body.Get<cpBody*>();
134   cpBodySetUserData2(cBody, this);
135
136   mPhysicsActors.insert(std::make_pair(id, PhysicsActor::New(actor, body, *this)));
137   actor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
138   actor[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;
139   mRootActor.Add(actor);
140   return mPhysicsActors.at(id);
141 }
142
143 void ChipmunkPhysicsAdaptor::RemoveActorBody(PhysicsActor& physicsActor)
144 {
145   auto iter = mPhysicsActors.find(physicsActor.GetId());
146   if(iter != mPhysicsActors.end())
147   {
148     mPhysicsActors.erase(iter);
149   }
150   Dali::Actor actor = mRootActor.FindChildById(physicsActor.GetId());
151   if(actor)
152   {
153     actor.Unparent();
154   }
155   auto    body  = physicsActor.GetBody();
156   cpBody* cBody = body.Get<cpBody*>();
157   if(cBody)
158   {
159     cpBodySetUserData2(cBody, nullptr);
160   }
161 }
162
163 PhysicsActorPtr ChipmunkPhysicsAdaptor::GetPhysicsActor(Dali::Any body) const
164 {
165   cpBody* cBody = body.Get<cpBody*>();
166   if(cBody)
167   {
168     return reinterpret_cast<PhysicsActor*>(cpBodyGetUserData2(cBody));
169   }
170   DALI_LOG_ERROR("Body not found in physics actors");
171   return nullptr;
172 }
173
174 // Convert a position from root actor local space to physics space
175 Vector3 ChipmunkPhysicsAdaptor::TranslateToPhysicsSpace(Vector3 vector) const
176 {
177   Vector4 position = mTransform * Vector4(vector.x, vector.y, vector.z, 1.0f);
178   return Vector3(position);
179 }
180
181 // Convert a position from physics space to root actor local space
182 Vector3 ChipmunkPhysicsAdaptor::TranslateFromPhysicsSpace(Vector3 vector) const
183 {
184   Vector4 position = mInverseTransform * Vector4(vector.x, vector.y, vector.z, 1.0f);
185   return Vector3(position);
186 }
187
188 Quaternion ChipmunkPhysicsAdaptor::TranslateToPhysicsSpace(Quaternion orientation) const
189 {
190   // Actors face outwards (+ve Z)
191   // In DALi world, +ve angle about +ve Z is clockwise.
192   // But, if physics is mirrored in Y axis, so +ve angle is anti-clockwise.
193
194   // Compute angle about Z axis
195   Vector3 axis;
196   Radian  angle;
197   orientation.ToAxisAngle(axis, angle);
198
199   // Check if Transform matrix is mirrored in X xor Y
200   if(std::signbit(mTransform.AsFloat()[0]) ^ std::signbit(mTransform.AsFloat()[5]))
201   {
202     return Quaternion(-angle, axis);
203   }
204
205   return Quaternion(angle, axis);
206 }
207
208 Quaternion ChipmunkPhysicsAdaptor::TranslateFromPhysicsSpace(Quaternion orientation) const
209 {
210   // Mirroring conversion is identical in both transforms
211   return TranslateToPhysicsSpace(orientation);
212 }
213
214 // Convert a vector from dali space to physics space
215 Vector3 ChipmunkPhysicsAdaptor::ConvertVectorToPhysicsSpace(Vector3 vector) const
216 {
217   Vector4 otherVector(mTransform * Vector4(vector.x, vector.y, vector.z, 0.0f));
218   return Vector3(otherVector);
219 }
220
221 // Convert a vector from physics space to root actor local space
222 Vector3 ChipmunkPhysicsAdaptor::ConvertVectorFromPhysicsSpace(Vector3 vector) const
223 {
224   Vector4 otherVector(mInverseTransform * Vector4(vector.x, vector.y, vector.z, 0.0f));
225   return Vector3(otherVector);
226 }
227
228 void ChipmunkPhysicsAdaptor::BuildPickingRay(Vector3 origin, Vector3 direction, Dali::Vector3& rayFromWorld, Dali::Vector3& rayToWorld)
229 {
230   rayFromWorld = TranslateToPhysicsSpace(origin);
231   rayToWorld   = TranslateToPhysicsSpace(origin); // rayToWorld is identical - there's no depth
232 }
233
234 Vector3 ChipmunkPhysicsAdaptor::ProjectPoint(Vector3 origin, Vector3 direction, float distance)
235 {
236   // Ignore direction & distance.
237   return TranslateToPhysicsSpace(origin);
238 }
239
240 } // namespace Dali::Toolkit::Physics::Internal