07f14a32b08f79afda33cde7dc8b58ed13b9fa07
[platform/core/uifw/dali-demo.git] / examples / chipmunk-physics / physics-actor.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
18 #include "physics-actor.h"
19 #include "physics-impl.h"
20 #include <dali/public-api/common/constants.h>
21 #include <dali/public-api/math/vector3.h>
22 #include <dali/public-api/math/quaternion.h>
23
24 using Dali::Vector3;
25 using Dali::Quaternion;
26 using Dali::Radian;
27
28 void PhysicsActor::ClearForces()
29 {
30   printf("Not Implemented\n");
31   //mBody->clearForces();
32   // No similar API
33 }
34
35 Dali::Vector3 PhysicsActor::GetPhysicsPosition()
36 {
37   cpVect cpPosition = cpBodyGetPosition(mBody);
38   return Vector3(cpPosition.x, cpPosition.y, 0.0f);
39 }
40
41 void PhysicsActor::SetPhysicsPosition(Dali::Vector3 actorPosition)
42 {
43   Dali::Mutex::ScopedLock lock(mImpl->mMutex);
44   Vector3 physicsPosition = mImpl->TranslateToPhysicsSpace(actorPosition);
45   cpBodySetPosition(mBody, cpv(physicsPosition.x, physicsPosition.y));
46 }
47
48 void PhysicsActor::SetPhysicsVelocity(Dali::Vector3 actorVelocity)
49 {
50   Dali::Mutex::ScopedLock lock(mImpl->mMutex);
51   Vector3 physicsVelocity = mImpl->ConvertVectorToPhysicsSpace(actorVelocity);
52   cpBodySetVelocity(mBody, cpv(physicsVelocity.x, physicsVelocity.y));
53 }
54
55 void PhysicsActor::SetPhysicsAngularVelocity(Dali::Vector3 velocity)
56 {
57   Dali::Mutex::ScopedLock lock(mImpl->mMutex);
58   printf("Not Implemented\n");
59   //mBody->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
60 }
61
62 Quaternion PhysicsActor::GetPhysicsRotation()
63 {
64   return Quaternion{};
65 }
66
67 void PhysicsActor::SetPhysicsRotation(Dali::Quaternion rotation)
68 {
69   Dali::Mutex::ScopedLock lock(mImpl->mMutex);
70
71   Vector3 axis;
72   Radian angle;
73   rotation.ToAxisAngle(axis, angle);
74
75   //btQuaternion orn = btQuaternion(btVector3(axis.x, -axis.y, axis.z), btScalar(float(-angle)));
76   //btTransform& transform = mBody->getWorldTransform();
77   //transform.setRotation(orn);
78   printf("Not Implemented\n");
79 }
80
81
82 Vector3 PhysicsActor::GetActorPosition()
83 {
84   cpVect cpPosition = cpBodyGetPosition(mBody);
85   return mImpl->TranslateFromPhysicsSpace(Vector3(cpPosition.x, cpPosition.y, 0.0f));
86 }
87
88 Vector3 PhysicsActor::GetActorVelocity()
89 {
90   cpVect cpVelocity = cpBodyGetVelocity(mBody);
91   return mImpl->ConvertVectorFromPhysicsSpace(Vector3(cpVelocity.x, cpVelocity.y, 0.0f));
92 }
93
94 Quaternion PhysicsActor::GetActorRotation()
95 {
96   cpFloat angle = cpBodyGetAngle(mBody);
97   return Quaternion(Radian(angle), -Vector3::ZAXIS);
98 }