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-actor-impl.h>
19 #include <dali-physics/internal/physics-adaptor-impl.h>
21 #include <chipmunk/chipmunk.h>
25 inline cpVect fromVec3(Dali::Vector3 vec3)
27 return cpv(vec3.x, vec3.y);
30 inline Dali::Vector3 toVec3(cpVect vec)
32 return Dali::Vector3(vec.x, vec.y, 0.0f);
35 } //Anonymous namespace
37 namespace Dali::Toolkit::Physics::Internal
39 PhysicsActorPtr PhysicsActor::New(Dali::Actor actor, Dali::Any body, Dali::Toolkit::Physics::Internal::PhysicsAdaptor& adaptor)
41 PhysicsActorPtr physicsActor(new Internal::PhysicsActor(actor, body, adaptor));
42 physicsActor->Initialize();
46 PhysicsActor::PhysicsActor(Dali::Actor actor, Dali::Any body, PhysicsAdaptor& adaptor)
48 mActorId(actor.GetProperty<int>(Dali::Actor::Property::ID)),
53 PhysicsActor::~PhysicsActor() = default;
55 void PhysicsActor::Initialize(void)
57 cpBodySetUserData2(mBody.Get<cpBody*>(), this);
62 void PhysicsActor::AsyncSetPhysicsPosition(Dali::Vector3 actorPosition)
65 cpBody* body = mBody.Get<cpBody*>();
66 cpVect pos = fromVec3(mAdaptor.TranslateToPhysicsSpace(actorPosition));
67 mAdaptor.Queue([body, pos] { cpBodySetPosition(body, pos); });
70 void PhysicsActor::AsyncSetPhysicsRotation(Dali::Quaternion rotation)
73 cpBody* body = mBody.Get<cpBody*>();
74 auto q = mAdaptor.TranslateToPhysicsSpace(rotation);
77 q.ToAxisAngle(axis, angle);
78 mAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle); });
81 Dali::Vector3 PhysicsActor::GetPhysicsPosition() const
83 cpBody* body = mBody.Get<cpBody*>();
84 return toVec3(cpBodyGetPosition(body));
87 Dali::Quaternion PhysicsActor::GetPhysicsRotation() const
89 cpBody* body = mBody.Get<cpBody*>();
90 cpFloat angle = cpBodyGetAngle(body);
91 return Quaternion(Radian(angle), Vector3::ZAXIS);
94 Dali::Vector3 PhysicsActor::GetActorPosition() const
96 cpBody* body = mBody.Get<cpBody*>();
97 cpVect cpPosition = cpBodyGetPosition(body);
98 return mAdaptor.TranslateFromPhysicsSpace(Vector3(cpPosition.x, cpPosition.y, 0.0f));
101 Dali::Quaternion PhysicsActor::GetActorRotation() const
103 cpBody* body = mBody.Get<cpBody*>();
104 cpFloat angle = cpBodyGetAngle(body);
105 return mAdaptor.TranslateFromPhysicsSpace(Quaternion(Radian(angle), Vector3::ZAXIS));
108 } // namespace Dali::Toolkit::Physics::Internal