[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / internal / chipmunk-impl / chipmunk-physics-actor-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/physics-actor-impl.h>
19 #include <dali-physics/internal/physics-adaptor-impl.h>
20
21 #include <chipmunk/chipmunk.h>
22
23 namespace
24 {
25 inline cpVect fromVec3(Dali::Vector3 vec3)
26 {
27   return cpv(vec3.x, vec3.y);
28 }
29
30 inline Dali::Vector3 toVec3(cpVect vec)
31 {
32   return Dali::Vector3(vec.x, vec.y, 0.0f);
33 }
34
35 } //Anonymous namespace
36
37 namespace Dali::Toolkit::Physics::Internal
38 {
39 PhysicsActorPtr PhysicsActor::New(Dali::Actor actor, Dali::Any body, Dali::Toolkit::Physics::Internal::PhysicsAdaptor& adaptor)
40 {
41   PhysicsActorPtr physicsActor(new Internal::PhysicsActor(actor, body, adaptor));
42   physicsActor->Initialize();
43   return physicsActor;
44 }
45
46 PhysicsActor::PhysicsActor(Dali::Actor actor, Dali::Any body, PhysicsAdaptor& adaptor)
47 : mAdaptor(adaptor),
48   mActorId(actor.GetProperty<int>(Dali::Actor::Property::ID)),
49   mBody(body)
50 {
51 }
52
53 PhysicsActor::~PhysicsActor() = default;
54
55 void PhysicsActor::Initialize(void)
56 {
57   cpBodySetUserData2(mBody.Get<cpBody*>(), this);
58
59   // RegisterObject?
60 }
61
62 void PhysicsActor::AsyncSetPhysicsPosition(Dali::Vector3 actorPosition)
63 {
64   // Queue task
65   cpBody* body = mBody.Get<cpBody*>();
66   cpVect  pos  = fromVec3(mAdaptor.TranslateToPhysicsSpace(actorPosition));
67   mAdaptor.Queue([body, pos] { cpBodySetPosition(body, pos); });
68 }
69
70 void PhysicsActor::AsyncSetPhysicsRotation(Dali::Quaternion rotation)
71 {
72   // Queue task
73   cpBody* body = mBody.Get<cpBody*>();
74   auto    q    = mAdaptor.TranslateToPhysicsSpace(rotation);
75   Vector3 axis;
76   Radian  angle;
77   q.ToAxisAngle(axis, angle);
78   mAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle); });
79 }
80
81 Dali::Vector3 PhysicsActor::GetPhysicsPosition() const
82 {
83   cpBody* body = mBody.Get<cpBody*>();
84   return toVec3(cpBodyGetPosition(body));
85 }
86
87 Dali::Quaternion PhysicsActor::GetPhysicsRotation() const
88 {
89   cpBody* body  = mBody.Get<cpBody*>();
90   cpFloat angle = cpBodyGetAngle(body);
91   return Quaternion(Radian(angle), Vector3::ZAXIS);
92 }
93
94 Dali::Vector3 PhysicsActor::GetActorPosition() const
95 {
96   cpBody* body       = mBody.Get<cpBody*>();
97   cpVect  cpPosition = cpBodyGetPosition(body);
98   return mAdaptor.TranslateFromPhysicsSpace(Vector3(cpPosition.x, cpPosition.y, 0.0f));
99 }
100
101 Dali::Quaternion PhysicsActor::GetActorRotation() const
102 {
103   cpBody* body  = mBody.Get<cpBody*>();
104   cpFloat angle = cpBodyGetAngle(body);
105   return mAdaptor.TranslateFromPhysicsSpace(Quaternion(Radian(angle), Vector3::ZAXIS));
106 }
107
108 } // namespace Dali::Toolkit::Physics::Internal