Commit 86c6cab5e9fcb2e798ccd606cd3e547c422f5336

Authored by David Steele
1 parent 0fb7eb98

Updated chipmunk demo to use Dali physics adaptor

Added dependency on dali-physics-3d for bullet-example

Change-Id: Ifaf5dd7dce079549012f25a094473aef2fe0f08a
build/tizen/CMakeLists.txt
@@ -327,6 +327,7 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc. @@ -327,6 +327,7 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc.
327 327
328 FIND_PACKAGE( chipmunk ) 328 FIND_PACKAGE( chipmunk )
329 FIND_PACKAGE( bullet3 ) 329 FIND_PACKAGE( bullet3 )
  330 + FIND_PACKAGE( dali2-physics-2d )
330 FIND_PACKAGE( dali2-physics-3d ) 331 FIND_PACKAGE( dali2-physics-3d )
331 332
332 # Set up the include dir 333 # Set up the include dir
@@ -394,6 +395,10 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc. @@ -394,6 +395,10 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc.
394 SET(ENABLE_PHYSICS_3D "ON" ) 395 SET(ENABLE_PHYSICS_3D "ON" )
395 ENDIF() 396 ENDIF()
396 397
  398 + IF (dali2-physics-2d_FOUND)
  399 + SET(DALI_PHYSICS_2D_LDFLAGS ${DALI_PHYSICS_2D_LDFLAGS} dali2-physics-2d::dali2-physics-2d)
  400 + ENDIF()
  401 +
397 IF (dali2-physics-3d_FOUND) 402 IF (dali2-physics-3d_FOUND)
398 SET(DALI_PHYSICS_3D_LDFLAGS ${DALI_PHYSICS_3D_LDFLAGS} dali2-physics-3d::dali2-physics-3d) 403 SET(DALI_PHYSICS_3D_LDFLAGS ${DALI_PHYSICS_3D_LDFLAGS} dali2-physics-3d::dali2-physics-3d)
399 ENDIF() 404 ENDIF()
examples/chipmunk-physics/README.md
1 # Chipmunk Physics Example 1 # Chipmunk Physics Example
2 2
3 This is an example showing how to use Chipmunk2D physics library to create and control physics objects in DALi. 3 This is an example showing how to use Chipmunk2D physics library to create and control physics objects in DALi.
4 -It creates a ball and a pyramid brick wall which can be moved using touch and key events. 4 +It creates a set of balls which act under gravity
5 5
6 ![](./chipmunk.gif) 6 ![](./chipmunk.gif)
  7 +
  8 +"wasd" keys move the last touched actor up/down/left/right.
  9 +"qe" keys rotate the last touched actor in Z axis
  10 +"p" key resets the position/forces on the last touched actor to the origin
  11 +Space key toggles the integration state.
examples/chipmunk-physics/frame-callback.cpp deleted
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 -#include "frame-callback.h"  
18 -#include <dali/devel-api/threading/mutex.h>  
19 -#include <dali/devel-api/update/update-proxy.h>  
20 -#include <dali/integration-api/debug.h>  
21 -#include <dali/public-api/math/vector3.h>  
22 -#include "physics-impl.h"  
23 -  
24 -using Dali::Quaternion;  
25 -using Dali::Vector3;  
26 -  
27 -#if defined(DEBUG_ENABLED)  
28 -extern Debug::Filter* gPhysicsDemo;  
29 -#endif  
30 -  
31 -FrameCallback::FrameCallback(PhysicsImpl& physicsImpl)  
32 -: mPhysicsImpl(physicsImpl)  
33 -{  
34 -}  
35 -  
36 -bool FrameCallback::Update(Dali::UpdateProxy& updateProxy, float elapsedSeconds)  
37 -{  
38 - static int numCalls = 0;  
39 -  
40 - numCalls++;  
41 - if(numCalls % 30 == 0)  
42 - {  
43 - DALI_LOG_INFO(gPhysicsDemo, Debug::Concise, "Physics frame update\n");  
44 - }  
45 -  
46 - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex);  
47 - static float frameTime = 0;  
48 - frameTime += elapsedSeconds;  
49 - do  
50 - {  
51 - mPhysicsImpl.Integrate(mPhysicsTimeStep);  
52 - frameTime -= mPhysicsTimeStep;  
53 - } while(frameTime > 0);  
54 -  
55 - for(auto&& actor : mPhysicsImpl.mPhysicsActors)  
56 - {  
57 - // Get position, orientation from physics world.  
58 - Vector3 position = actor.second.GetActorPosition();  
59 - updateProxy.BakePosition(actor.first, position);  
60 - Quaternion rotation = actor.second.GetActorRotation();  
61 - updateProxy.BakeOrientation(actor.first, rotation);  
62 - }  
63 -  
64 - return true;  
65 -}  
examples/chipmunk-physics/frame-callback.h deleted
1 -#ifndef PHYSICS_DEMO_FRAME_CALLBACK_H  
2 -#define PHYSICS_DEMO_FRAME_CALLBACK_H  
3 -  
4 -/*  
5 - * Copyright (c) 2023 Samsung Electronics Co., Ltd.  
6 - *  
7 - * Licensed under the Apache License, Version 2.0 (the "License");  
8 - * you may not use this file except in compliance with the License.  
9 - * You may obtain a copy of the License at  
10 - *  
11 - * http://www.apache.org/licenses/LICENSE-2.0  
12 - *  
13 - * Unless required by applicable law or agreed to in writing, software  
14 - * distributed under the License is distributed on an "AS IS" BASIS,  
15 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
16 - * See the License for the specific language governing permissions and  
17 - * limitations under the License.  
18 - */  
19 -  
20 -#include <dali/public-api/adaptor-framework/window.h>  
21 -#include <dali/devel-api/update/frame-callback-interface.h>  
22 -#include <map>  
23 -#include <chrono>  
24 -  
25 -class PhysicsImpl;  
26 -  
27 -class FrameCallback : public Dali::FrameCallbackInterface  
28 -{  
29 -public:  
30 - /**  
31 - * Constructor  
32 - */  
33 - explicit FrameCallback(PhysicsImpl& physicsImpl);  
34 -  
35 - /**  
36 - * Set the physics time step  
37 - * @param timeStep (in seconds)  
38 - */  
39 - void SetPhysicsTimeStep(float timeStep)  
40 - {  
41 - mPhysicsTimeStep = timeStep;  
42 - }  
43 -  
44 -private:  
45 - /**  
46 - * Called each frame.  
47 - * @param[in] updateProxy Used to set world matrix and size  
48 - * @param[in] elapsedSeconds Time since last frame  
49 - * @return Whether we should keep rendering.  
50 - */  
51 - bool Update(Dali::UpdateProxy& updateProxy, float elapsedSeconds) override;  
52 -  
53 -private: // Member variables  
54 - PhysicsImpl& mPhysicsImpl;  
55 - float mPhysicsTimeStep{1.0/180.0};  
56 -};  
57 -  
58 -#endif //PHYSICS_DEMO_FRAME_CALLBACK_H  
examples/chipmunk-physics/physics-actor.cpp deleted
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 -}  
examples/chipmunk-physics/physics-actor.h deleted
1 -#ifndef DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H  
2 -#define DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H  
3 -/*  
4 - * Copyright (c) 2023 Samsung Electronics Co., Ltd.  
5 - *  
6 - * Licensed under the Apache License, Version 2.0 (the "License");  
7 - * you may not use this file except in compliance with the License.  
8 - * You may obtain a copy of the License at  
9 - *  
10 - * http://www.apache.org/licenses/LICENSE-2.0  
11 - *  
12 - * Unless required by applicable law or agreed to in writing, software  
13 - * distributed under the License is distributed on an "AS IS" BASIS,  
14 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
15 - * See the License for the specific language governing permissions and  
16 - * limitations under the License.  
17 - *  
18 - */  
19 -  
20 -#include <chipmunk/chipmunk.h>  
21 -#include <cstdint>  
22 -#include <dali/dali.h>  
23 -#include <iosfwd>  
24 -  
25 -// Forward declarations  
26 -class PhysicsImpl;  
27 -  
28 -/**  
29 - * Class that associates an actor with a physics body. (Initially, rigid body)  
30 - */  
31 -class PhysicsActor  
32 -{  
33 -public:  
34 - PhysicsActor() = default;  
35 - PhysicsActor(Dali::Actor& actor, cpBody* body, PhysicsImpl* impl, Dali::Property::Index brightnessId)  
36 - : mImpl(impl),  
37 - mActorId(actor.GetProperty<int>(Dali::Actor::Property::ID)),  
38 - mBody(body),  
39 - mBrightnessIndex(brightnessId)  
40 - {  
41 - cpBodySetUserData(mBody, this);  
42 - }  
43 -  
44 - PhysicsActor(const PhysicsActor& rhs)=delete;  
45 - PhysicsActor& operator=(const PhysicsActor& rhs)=delete;  
46 -  
47 - PhysicsActor(const PhysicsActor&& rhs)  
48 - {  
49 - if(this != &rhs)  
50 - {  
51 - mImpl = rhs.mImpl;  
52 - mActorId = rhs.mActorId;  
53 - mBody = rhs.mBody;  
54 - cpBodySetUserData(mBody, this);  
55 - mBrightnessIndex = rhs.mBrightnessIndex;  
56 - }  
57 - }  
58 -  
59 - PhysicsActor& operator=(const PhysicsActor&& rhs)  
60 - {  
61 - if(this != &rhs)  
62 - {  
63 - mActorId = rhs.mActorId;  
64 - mBody = rhs.mBody;  
65 - mImpl = rhs.mImpl;  
66 - mBrightnessIndex = rhs.mBrightnessIndex;  
67 - cpBodySetUserData(mBody, this);  
68 - }  
69 - return *this;  
70 - }  
71 -  
72 - uint32_t GetId()  
73 - {  
74 - return mActorId;  
75 - }  
76 -  
77 - cpBody* GetBody()  
78 - {  
79 - return mBody;  
80 - }  
81 -  
82 - Dali::Property::Index GetBrightnessIndex()  
83 - {  
84 - return mBrightnessIndex;  
85 - }  
86 -  
87 - Dali::Vector3 GetPhysicsPosition();  
88 - Dali::Quaternion GetPhysicsRotation();  
89 -  
90 - void SetPhysicsPosition(Dali::Vector3 actorPosition);  
91 - void SetPhysicsVelocity(Dali::Vector3 actorVelocity);  
92 - void SetPhysicsAngularVelocity(Dali::Vector3 actorVelocity);  
93 - void SetPhysicsRotation(Dali::Quaternion actorRotation);  
94 - Dali::Vector3 GetActorPosition();  
95 - Dali::Vector3 GetActorVelocity();  
96 - Dali::Quaternion GetActorRotation();  
97 - void ClearForces();  
98 -  
99 -private:  
100 - PhysicsImpl* mImpl{nullptr};  
101 - uint32_t mActorId{0};  
102 - cpBody* mBody{nullptr};  
103 - Dali::Property::Index mBrightnessIndex{Dali::Property::INVALID_INDEX};  
104 -};  
105 -  
106 -#endif // DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H  
examples/chipmunk-physics/physics-demo-controller.cpp
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 */ 15 */
16 16
17 #include <dali-toolkit/dali-toolkit.h> 17 #include <dali-toolkit/dali-toolkit.h>
18 -#include <dali/dali.h> 18 +#include <dali-physics/dali-physics.h>
19 19
20 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h> 20 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
21 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h> 21 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
@@ -25,18 +25,18 @@ @@ -25,18 +25,18 @@
25 25
26 #include <iostream> 26 #include <iostream>
27 #include <string> 27 #include <string>
28 -  
29 -#include "generated/rendering-textured-shape-frag.h"  
30 -#include "generated/rendering-textured-shape-vert.h"  
31 -#include "physics-actor.h"  
32 -#include "physics-impl.h" 28 +#include <chipmunk/chipmunk.h>
33 29
34 using namespace Dali; 30 using namespace Dali;
  31 +using namespace Dali::Toolkit::Physics;
35 32
36 #if defined(DEBUG_ENABLED) 33 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gPhysicsDemo = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS_EXAMPLE"); 34 Debug::Filter* gPhysicsDemo = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS_EXAMPLE");
38 #endif 35 #endif
39 36
  37 +#define GRABBABLE_MASK_BIT (1u << 31)
  38 +cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT};
  39 +
40 namespace KeyModifier 40 namespace KeyModifier
41 { 41 {
42 enum Key 42 enum Key
@@ -84,25 +84,39 @@ public: @@ -84,25 +84,39 @@ public:
84 mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY); 84 mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY);
85 Window::WindowSize windowSize = mWindow.GetSize(); 85 Window::WindowSize windowSize = mWindow.GetSize();
86 86
87 - mPhysicsRoot = mPhysicsImpl.Initialize(mWindow); 87 + // Map Physics space (origin bottom left, +ve Y up)
  88 + // to DALi space (origin center, +ve Y down)
  89 + mPhysicsTransform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f));
  90 + mPhysicsTransform.SetTranslation(Vector3(windowSize.GetWidth() * 0.5f,
  91 + windowSize.GetHeight() * 0.5f,
  92 + 0.0f));
  93 +
  94 + mPhysicsAdaptor = PhysicsAdaptor::New(mPhysicsTransform, windowSize);
  95 + mPhysicsRoot = mPhysicsAdaptor.GetRootActor();
88 mPhysicsRoot.TouchedSignal().Connect(this, &PhysicsDemoController::OnTouched); 96 mPhysicsRoot.TouchedSignal().Connect(this, &PhysicsDemoController::OnTouched);
89 97
90 mWindow.Add(mPhysicsRoot); 98 mWindow.Add(mPhysicsRoot);
91 99
  100 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  101 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  102 +
  103 + CreateBounds(space, windowSize);
92 // Ball area = 2*PI*26^2 ~= 6.28*26*26 ~= 5400 104 // Ball area = 2*PI*26^2 ~= 6.28*26*26 ~= 5400
93 // Fill quarter of the screen... 105 // Fill quarter of the screen...
94 int numBalls = 10 + windowSize.GetWidth() * windowSize.GetHeight() / 20000; 106 int numBalls = 10 + windowSize.GetWidth() * windowSize.GetHeight() / 20000;
95 -  
96 for(int i = 0; i < numBalls; ++i) 107 for(int i = 0; i < numBalls; ++i)
97 { 108 {
98 - CreateBall(); 109 + CreateBall(space);
99 } 110 }
100 111
101 // For funky mouse drag 112 // For funky mouse drag
102 - mMouseBody = mPhysicsImpl.AddMouseBody(); 113 + mMouseBody = cpBodyNewKinematic(); // Mouse actor is a kinematic body that is not integrated
  114 +
  115 + // Process any async queued methods next frame
  116 + mPhysicsAdaptor.CreateSyncPoint();
103 } 117 }
104 118
105 - void CreateBall() 119 + void CreateBall(cpSpace* space)
106 { 120 {
107 const float BALL_MASS = 10.0f; 121 const float BALL_MASS = 10.0f;
108 const float BALL_RADIUS = 26.0f; 122 const float BALL_RADIUS = 26.0f;
@@ -111,13 +125,81 @@ public: @@ -111,13 +125,81 @@ public:
111 125
112 auto ball = Toolkit::ImageView::New(BALL_IMAGE); 126 auto ball = Toolkit::ImageView::New(BALL_IMAGE);
113 127
114 - auto& physicsBall = mPhysicsImpl.AddBall(ball, BALL_MASS, BALL_RADIUS, BALL_ELASTICITY, BALL_FRICTION); 128 + cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero)));
  129 +
  130 + cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero));
  131 + cpShapeSetElasticity(shape, BALL_ELASTICITY);
  132 + cpShapeSetFriction(shape, BALL_FRICTION);
  133 +
  134 + ball.RegisterProperty("uBrightness", 0.0f);
  135 +
  136 + PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body);
  137 +
115 Window::WindowSize windowSize = mWindow.GetSize(); 138 Window::WindowSize windowSize = mWindow.GetSize();
116 - const float s = BALL_RADIUS;  
117 - const float fw = windowSize.GetWidth() - BALL_RADIUS;  
118 - const float fh = windowSize.GetHeight() - BALL_RADIUS;  
119 - physicsBall.SetPhysicsPosition(Vector3(Random::Range(s, fw), Random::Range(s, fh), 0.0f));  
120 - physicsBall.SetPhysicsVelocity(Vector3(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0), 0.0f)); 139 +
  140 + const float fw = 0.5f*(windowSize.GetWidth() - BALL_RADIUS);
  141 + const float fh = 0.5f*(windowSize.GetHeight() - BALL_RADIUS);
  142 +
  143 + // Example of setting physics property on update thread
  144 + physicsBall.AsyncSetPhysicsPosition(Vector3(Random::Range(-fw, fw), Random::Range(-fh, fh), 0.0f));
  145 +
  146 + // Example of queuing a chipmunk method to run on the update thread
  147 + mPhysicsAdaptor.Queue([body](){
  148 + cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0)));
  149 + });
  150 + }
  151 +
  152 + void CreateBounds(cpSpace* space, Window::WindowSize size)
  153 + {
  154 + // We're working in physics space here - coords are: origin: bottom left, +ve Y: up
  155 + int xBound = size.GetWidth();
  156 + int yBound = size.GetHeight();
  157 +
  158 + cpBody* staticBody = cpSpaceGetStaticBody(space);
  159 +
  160 + if(mLeftBound)
  161 + {
  162 + cpSpaceRemoveShape(space, mLeftBound);
  163 + cpSpaceRemoveShape(space, mRightBound);
  164 + cpSpaceRemoveShape(space, mTopBound);
  165 + cpSpaceRemoveShape(space, mBottomBound);
  166 + cpShapeFree(mLeftBound);
  167 + cpShapeFree(mRightBound);
  168 + cpShapeFree(mTopBound);
  169 + cpShapeFree(mBottomBound);
  170 + }
  171 + mLeftBound = AddBound(space, staticBody, cpv(0, 0), cpv(0, yBound));
  172 + mRightBound = AddBound(space, staticBody, cpv(xBound, 0), cpv(xBound, yBound));
  173 + mTopBound = AddBound(space, staticBody, cpv(0, 0), cpv(xBound, 0));
  174 + mBottomBound = AddBound(space, staticBody, cpv(0, yBound), cpv(xBound, yBound));
  175 + }
  176 +
  177 + cpShape* AddBound(cpSpace* space, cpBody* staticBody, cpVect start, cpVect end)
  178 + {
  179 + cpShape* shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, start, end, 0.0f));
  180 + cpShapeSetElasticity(shape, 1.0f);
  181 + cpShapeSetFriction(shape, 1.0f);
  182 +
  183 + cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
  184 + return shape;
  185 + }
  186 +
  187 + void MoveMouseBody(cpBody* mouseBody, Vector3 position)
  188 + {
  189 + cpVect cpPosition = cpv(position.x, position.y);
  190 + cpVect newPoint = cpvlerp(cpBodyGetPosition(mouseBody), cpPosition, 0.25f);
  191 + cpBodySetVelocity(mouseBody, cpvmult(cpvsub(newPoint, cpBodyGetPosition(mouseBody)), 60.0f));
  192 + cpBodySetPosition(mouseBody, newPoint);
  193 + }
  194 +
  195 + cpConstraint* AddPivotJoint(cpSpace* space, cpBody* body1, cpBody* body2, Vector3 localPivot)
  196 + {
  197 + cpVect pivot{localPivot.x, localPivot.y};
  198 + cpConstraint* joint = cpPivotJointNew2(body2, body1, cpvzero, pivot);
  199 + cpConstraintSetMaxForce(joint, 50000.0f); // Magic numbers for mouse feedback.
  200 + cpConstraintSetErrorBias(joint, cpfpow(1.0f - 0.15f, 60.0f));
  201 + cpConstraint* constraint = cpSpaceAddConstraint(space, joint);
  202 + return constraint; // Constraint & joint are the same...
121 } 203 }
122 204
123 void OnTerminate(Application& application) 205 void OnTerminate(Application& application)
@@ -127,21 +209,24 @@ public: @@ -127,21 +209,24 @@ public:
127 209
128 void OnWindowResize(Window window, Window::WindowSize newSize) 210 void OnWindowResize(Window window, Window::WindowSize newSize)
129 { 211 {
130 - mPhysicsImpl.CreateWorldBounds(newSize); 212 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  213 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  214 +
  215 + CreateBounds(space, newSize);
131 } 216 }
132 217
133 bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) 218 bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch)
134 { 219 {
135 static enum { 220 static enum {
136 None, 221 None,
137 - MoveCameraXZ,  
138 MovePivot, 222 MovePivot,
139 } state = None; 223 } state = None;
140 224
141 - auto renderTask = mWindow.GetRenderTaskList().GetTask(0);  
142 - auto screenCoords = touch.GetScreenPosition(0);  
143 - Vector3 origin, direction;  
144 - Dali::HitTestAlgorithm::BuildPickingRay(renderTask, screenCoords, origin, direction); 225 + auto renderTask = mWindow.GetRenderTaskList().GetTask(0);
  226 + auto screenCoords = touch.GetScreenPosition(0);
  227 + // In this demo, physics space is equivalent to screen space with y inverted
  228 + auto windowSize = mWindow.GetSize();
  229 + Vector3 rayPhysicsOrigin(screenCoords.x, windowSize.GetHeight() - screenCoords.y, 0.0f);
145 230
146 switch(state) 231 switch(state)
147 { 232 {
@@ -149,30 +234,23 @@ public: @@ -149,30 +234,23 @@ public:
149 { 234 {
150 if(touch.GetState(0) == Dali::PointState::STARTED) 235 if(touch.GetState(0) == Dali::PointState::STARTED)
151 { 236 {
152 - if(mCtrlDown)  
153 - {  
154 - state = MoveCameraXZ;  
155 - // local to top left  
156 - //cameraY = touch.GetLocalPosition(0).y;  
157 - // Could move on fixed plane, e.g. y=0.  
158 - // position.Y corresponds to a z value depending on perspective  
159 - // position.X scales to an x value depending on perspective  
160 - }  
161 - else 237 + state = MovePivot;
  238 +
  239 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  240 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  241 +
  242 + Vector3 localPivot;
  243 + float pickingDistance;
  244 +
  245 + auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsOrigin, localPivot, pickingDistance);
  246 + if(!body.Empty())
162 { 247 {
163 - state = MovePivot;  
164 - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); 248 + mPickedBody = body.Get<cpBody*>();
  249 + mSelectedActor = mPhysicsAdaptor.GetPhysicsActor(mPickedBody);
165 250
166 - Vector3 localPivot;  
167 - float pickingDistance;  
168 - auto body = mPhysicsImpl.HitTest(screenCoords, origin, direction, localPivot, pickingDistance);  
169 - if(body)  
170 - {  
171 - mPickedBody = body;  
172 - mPhysicsImpl.HighlightBody(mPickedBody, true);  
173 - mPickedSavedState = mPhysicsImpl.ActivateBody(mPickedBody);  
174 - mPickedConstraint = mPhysicsImpl.AddPivotJoint(mPickedBody, mMouseBody, localPivot);  
175 - } 251 + mPickedSavedState = cpBodyIsSleeping(mPickedBody);
  252 + cpBodyActivate(mPickedBody);
  253 + mPickedConstraint = AddPivotJoint(space, mPickedBody, mMouseBody, localPivot);
176 } 254 }
177 } 255 }
178 break; 256 break;
@@ -183,23 +261,11 @@ public: @@ -183,23 +261,11 @@ public:
183 { 261 {
184 if(mPickedBody && mPickedConstraint) 262 if(mPickedBody && mPickedConstraint)
185 { 263 {
186 - if(!mShiftDown)  
187 - {  
188 - // Move point in XY plane, projected into scene  
189 - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); 264 + // Ensure we get a lock before altering constraints
  265 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
190 266
191 - Vector3 position = mPhysicsImpl.TranslateToPhysicsSpace(Vector3(screenCoords));  
192 - mPhysicsImpl.MoveMouseBody(mMouseBody, position);  
193 - }  
194 - else  
195 - {  
196 - // Move point in XZ plane  
197 - // Above vanishing pt, it's on top plane of frustum; below vanishing pt it's on bottom plane.  
198 - // Kind of want to project onto the plane using initial touch xy, rather than top/bottom.  
199 - // Whole new projection code needed.  
200 -  
201 - // Cheat!  
202 - } 267 + // Move point in physics coords
  268 + MoveMouseBody(mMouseBody, rayPhysicsOrigin);
203 } 269 }
204 } 270 }
205 else if(touch.GetState(0) == Dali::PointState::FINISHED || 271 else if(touch.GetState(0) == Dali::PointState::FINISHED ||
@@ -207,11 +273,20 @@ public: @@ -207,11 +273,20 @@ public:
207 { 273 {
208 if(mPickedConstraint) 274 if(mPickedConstraint)
209 { 275 {
210 - mPhysicsImpl.HighlightBody(mPickedBody, false); 276 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  277 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
211 278
212 - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex);  
213 - mPhysicsImpl.RestoreBodyState(mPickedBody, mPickedSavedState);  
214 - mPhysicsImpl.ReleaseConstraint(mPickedConstraint); 279 + if(mPickedSavedState)
  280 + {
  281 + cpBodyActivate(mPickedBody);
  282 + }
  283 + else
  284 + {
  285 + cpBodySleep(mPickedBody);
  286 + }
  287 +
  288 + cpSpaceRemoveConstraint(space, mPickedConstraint);
  289 + cpConstraintFree(mPickedConstraint);
215 mPickedConstraint = nullptr; 290 mPickedConstraint = nullptr;
216 mPickedBody = nullptr; 291 mPickedBody = nullptr;
217 } 292 }
@@ -219,23 +294,9 @@ public: @@ -219,23 +294,9 @@ public:
219 } 294 }
220 break; 295 break;
221 } 296 }
222 - case MoveCameraXZ:  
223 - {  
224 - if(touch.GetState(0) == Dali::PointState::MOTION)  
225 - {  
226 - // Move camera in XZ plane  
227 - //float y = cameraY; // touch point in Y. Move camera in an XZ plane on this point.  
228 - }  
229 - else if(touch.GetState(0) == Dali::PointState::FINISHED ||  
230 - touch.GetState(0) == Dali::PointState::INTERRUPTED)  
231 - {  
232 - state = None;  
233 - }  
234 - break;  
235 - }  
236 } 297 }
237 298
238 - //std::cout<<"Touch State: "<<state<<std::endl; 299 +
239 Stage::GetCurrent().KeepRendering(30.0f); 300 Stage::GetCurrent().KeepRendering(30.0f);
240 301
241 return true; 302 return true;
@@ -243,6 +304,8 @@ public: @@ -243,6 +304,8 @@ public:
243 304
244 void OnKeyEv(const Dali::KeyEvent& event) 305 void OnKeyEv(const Dali::KeyEvent& event)
245 { 306 {
  307 + static bool integrateState{true};
  308 +
246 if(event.GetState() == KeyEvent::DOWN) 309 if(event.GetState() == KeyEvent::DOWN)
247 { 310 {
248 switch(event.GetKeyCode()) 311 switch(event.GetKeyCode())
@@ -273,11 +336,80 @@ public: @@ -273,11 +336,80 @@ public:
273 } 336 }
274 else if(!event.GetKeyString().compare(" ")) 337 else if(!event.GetKeyString().compare(" "))
275 { 338 {
276 - mPhysicsImpl.ToggleIntegrateState(); 339 + integrateState = true^integrateState;
  340 + mPhysicsAdaptor.SetIntegrationState(integrateState?
  341 + PhysicsAdaptor::IntegrationState::ON:
  342 + PhysicsAdaptor::IntegrationState::OFF);
  343 +
277 } 344 }
278 - else if(!event.GetKeyString().compare("m")) 345 + else if(!event.GetKeyString().compare("w"))
279 { 346 {
280 - mPhysicsImpl.ToggleDebugState(); 347 + if(mSelectedActor)
  348 + {
  349 + Vector3 pos = mSelectedActor.GetActorPosition();
  350 + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(0, -10, 0));
  351 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  352 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  353 + mPhysicsAdaptor.CreateSyncPoint();
  354 + }
  355 + }
  356 + else if(!event.GetKeyString().compare("s"))
  357 + {
  358 + if(mSelectedActor)
  359 + {
  360 + Vector3 pos = mSelectedActor.GetActorPosition();
  361 + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(0, 10, 0));
  362 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  363 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  364 + mPhysicsAdaptor.CreateSyncPoint();
  365 + }
  366 + }
  367 + else if(!event.GetKeyString().compare("a"))
  368 + {
  369 + if(mSelectedActor)
  370 + {
  371 + Vector3 pos = mSelectedActor.GetActorPosition();
  372 + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(-10, 0, 0));
  373 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  374 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  375 + mPhysicsAdaptor.CreateSyncPoint();
  376 + }
  377 + }
  378 + else if(!event.GetKeyString().compare("d"))
  379 + {
  380 + if(mSelectedActor)
  381 + {
  382 + Vector3 pos = mSelectedActor.GetActorPosition();
  383 + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(10, 0, 0));
  384 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  385 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  386 + mPhysicsAdaptor.CreateSyncPoint();
  387 + }
  388 + }
  389 + else if(!event.GetKeyString().compare("q"))
  390 + {
  391 + // Rotate anti-clockwise
  392 + if(mSelectedActor)
  393 + {
  394 + Quaternion quaternion = mSelectedActor.GetActorRotation();
  395 + quaternion *= Quaternion(Degree(-15.0f), Vector3::ZAXIS);
  396 + mSelectedActor.AsyncSetPhysicsRotation(quaternion);
  397 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  398 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  399 + mPhysicsAdaptor.CreateSyncPoint();
  400 + }
  401 + }
  402 + else if(!event.GetKeyString().compare("e"))
  403 + {
  404 + // Rotate clockwise using native physics APIs
  405 + if(mSelectedActor)
  406 + {
  407 + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>();
  408 + float angle = cpBodyGetAngle(body);
  409 + mPhysicsAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle-Math::PI/12.0f); });
  410 + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); });
  411 + mPhysicsAdaptor.CreateSyncPoint();
  412 + }
281 } 413 }
282 break; 414 break;
283 } 415 }
@@ -313,13 +445,20 @@ private: @@ -313,13 +445,20 @@ private:
313 Application& mApplication; 445 Application& mApplication;
314 Window mWindow; 446 Window mWindow;
315 447
316 - PhysicsImpl mPhysicsImpl; 448 + PhysicsAdaptor mPhysicsAdaptor;
  449 + PhysicsActor mSelectedActor;
  450 + Matrix mPhysicsTransform;
317 Actor mPhysicsRoot; 451 Actor mPhysicsRoot;
318 cpBody* mMouseBody{nullptr}; 452 cpBody* mMouseBody{nullptr};
319 cpBody* mPickedBody{nullptr}; 453 cpBody* mPickedBody{nullptr};
320 cpConstraint* mPickedConstraint{nullptr}; 454 cpConstraint* mPickedConstraint{nullptr};
321 int mPickedSavedState = -1; /// 0 : Active, 1 : Sleeping 455 int mPickedSavedState = -1; /// 0 : Active, 1 : Sleeping
322 456
  457 + cpShape* mLeftBound{nullptr};
  458 + cpShape* mRightBound{nullptr};
  459 + cpShape* mTopBound{nullptr};
  460 + cpShape* mBottomBound{nullptr};
  461 +
323 bool mCtrlDown{false}; 462 bool mCtrlDown{false};
324 bool mAltDown{false}; 463 bool mAltDown{false};
325 bool mShiftDown{false}; 464 bool mShiftDown{false};
examples/chipmunk-physics/physics-impl.cpp deleted
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-impl.h"  
19 -#include "physics-actor.h"  
20 -  
21 -#include <devel-api/common/stage.h>  
22 -#include <iostream>  
23 -#include <map>  
24 -#include <utility>  
25 -  
26 -using Dali::Actor;  
27 -using Dali::Layer;  
28 -using Dali::Stage;  
29 -using Dali::Vector2;  
30 -using Dali::Vector3;  
31 -using Dali::Window;  
32 -using namespace Dali::DevelStage;  
33 -  
34 -#define GRABBABLE_MASK_BIT (1u << 31)  
35 -cpShapeFilter GRAB_FILTER = {CP_NO_GROUP, GRABBABLE_MASK_BIT, GRABBABLE_MASK_BIT};  
36 -cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT};  
37 -  
38 -Actor PhysicsImpl::Initialize(Window window)  
39 -{  
40 - mWindow = window;  
41 - mSpace = cpSpaceNew();  
42 - cpSpaceSetIterations(mSpace, 30);  
43 - cpSpaceSetSleepTimeThreshold(mSpace, 0.5f);  
44 - cpSpaceSetGravity(mSpace, cpv(0, -200));  
45 -  
46 - auto windowSize = window.GetSize();  
47 - CreateWorldBounds(windowSize);  
48 -  
49 - // Create an actor that can handle mouse events.  
50 - mPhysicsRoot = Layer::New();  
51 - mPhysicsRoot[Actor::Property::SIZE] = Vector2(windowSize.GetWidth(), windowSize.GetHeight());  
52 - mPhysicsRoot[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER;  
53 - mPhysicsRoot[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;  
54 -  
55 - mFrameCallback = new FrameCallback(*this);  
56 - AddFrameCallback(Stage::GetCurrent(), *mFrameCallback, window.GetRootLayer());  
57 - Stage::GetCurrent().KeepRendering(30);  
58 -  
59 - return mPhysicsRoot;  
60 -}  
61 -  
62 -Layer PhysicsImpl::CreateDebug(Vector2 windowSize)  
63 -{  
64 - return Layer();  
65 -}  
66 -  
67 -void PhysicsImpl::CreateWorldBounds(Window::WindowSize size)  
68 -{  
69 - // Physics origin is 0,0,0 in DALi coords.  
70 - // But, Y is inverted, so bottom is -ve, top is +ve.  
71 - // Perform this correction when applying position to actor.  
72 - // But, can't use actors in update, so cache transform.  
73 - SetTransform(Vector2(size.GetWidth(), size.GetHeight()));  
74 -  
75 - int xBound = size.GetWidth() / 2;  
76 - int yBound = size.GetHeight() / 2;  
77 -  
78 - cpBody* staticBody = cpSpaceGetStaticBody(mSpace);  
79 -  
80 - if(mLeftBound)  
81 - {  
82 - cpSpaceRemoveShape(mSpace, mLeftBound);  
83 - cpSpaceRemoveShape(mSpace, mRightBound);  
84 - cpSpaceRemoveShape(mSpace, mTopBound);  
85 - cpSpaceRemoveShape(mSpace, mBottomBound);  
86 - cpShapeFree(mLeftBound);  
87 - cpShapeFree(mRightBound);  
88 - cpShapeFree(mTopBound);  
89 - cpShapeFree(mBottomBound);  
90 - }  
91 - mLeftBound = AddBound(staticBody, cpv(-xBound, -yBound), cpv(-xBound, yBound));  
92 - mRightBound = AddBound(staticBody, cpv(xBound, -yBound), cpv(xBound, yBound));  
93 - mTopBound = AddBound(staticBody, cpv(-xBound, -yBound), cpv(xBound, -yBound));  
94 - mBottomBound = AddBound(staticBody, cpv(-xBound, yBound), cpv(xBound, yBound));  
95 -}  
96 -  
97 -void PhysicsImpl::SetTransform(Vector2 worldSize)  
98 -{  
99 - mWorldOffset.x = worldSize.x * 0.5f;  
100 - mWorldOffset.y = worldSize.y * 0.5f;  
101 - // y is always inverted.  
102 -}  
103 -  
104 -cpShape* PhysicsImpl::AddBound(cpBody* staticBody, cpVect start, cpVect end)  
105 -{  
106 - cpShape* shape = cpSpaceAddShape(mSpace, cpSegmentShapeNew(staticBody, start, end, 0.0f));  
107 - cpShapeSetElasticity(shape, 1.0f);  
108 - cpShapeSetFriction(shape, 1.0f);  
109 - cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);  
110 - return shape;  
111 -}  
112 -  
113 -PhysicsActor& PhysicsImpl::AddBall(::Actor actor, float mass, float radius, float elasticity, float friction)  
114 -{  
115 - Dali::Mutex::ScopedLock lock(mMutex);  
116 - cpBody* body = cpSpaceAddBody(mSpace, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));  
117 - cpBodySetPosition(body, cpv(0, 0));  
118 - cpBodySetVelocity(body, cpv(0, 0));  
119 -  
120 - cpShape* shape = cpSpaceAddShape(mSpace, cpCircleShapeNew(body, radius, cpvzero));  
121 - cpShapeSetElasticity(shape, elasticity);  
122 - cpShapeSetFriction(shape, friction);  
123 -  
124 - int id = actor[Actor::Property::ID];  
125 - Dali::Property::Index index = actor.RegisterProperty("uBrightness", 0.0f);  
126 - mPhysicsActors.insert(std::make_pair(id, PhysicsActor{actor, body, this, index}));  
127 - actor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::TOP_LEFT;  
128 - actor[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER;  
129 - mPhysicsRoot.Add(actor);  
130 - return mPhysicsActors.at(id);  
131 -}  
132 -  
133 -PhysicsActor& PhysicsImpl::AddBrick(Dali::Actor actor, float mass, float elasticity, float friction, Vector3 size)  
134 -{  
135 - Dali::Mutex::ScopedLock lock(mMutex);  
136 - cpBody* body = cpSpaceAddBody(mSpace, cpBodyNew(mass, cpMomentForBox(mass, size.width, size.height)));  
137 - cpBodySetPosition(body, cpv(0, 0));  
138 - cpBodySetVelocity(body, cpv(0, 0));  
139 -  
140 - cpShape* shape = cpSpaceAddShape(mSpace, cpBoxShapeNew(body, size.width, size.height, 0.0f));  
141 - cpShapeSetFriction(shape, friction);  
142 - cpShapeSetElasticity(shape, elasticity);  
143 -  
144 - int id = actor[Actor::Property::ID];  
145 - Dali::Property::Index index = actor.RegisterProperty("uBrightness", 0.0f);  
146 - mPhysicsActors.insert(std::make_pair(id, PhysicsActor{actor, body, this, index}));  
147 - actor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::TOP_LEFT;  
148 - actor[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER;  
149 - mPhysicsRoot.Add(actor);  
150 - return mPhysicsActors.at(id);  
151 -}  
152 -  
153 -cpBody* PhysicsImpl::AddMouseBody()  
154 -{  
155 - Dali::Mutex::ScopedLock lock(mMutex);  
156 - auto kinematicBody = cpBodyNewKinematic(); // Mouse actor is a kinematic body that is not integrated  
157 - return kinematicBody;  
158 -}  
159 -  
160 -PhysicsActor* PhysicsImpl::GetPhysicsActor(cpBody* body)  
161 -{  
162 - return reinterpret_cast<PhysicsActor*>(cpBodyGetUserData(body));  
163 -}  
164 -  
165 -void PhysicsImpl::HighlightBody(cpBody* body, bool highlight)  
166 -{  
167 - auto physicsActor = GetPhysicsActor(body);  
168 - if(physicsActor)  
169 - {  
170 - Actor actor = mPhysicsRoot.FindChildById(physicsActor->GetId());  
171 - if(actor)  
172 - {  
173 - actor[physicsActor->GetBrightnessIndex()] = highlight ? 1.0f : 0.0f;  
174 - }  
175 - }  
176 -}  
177 -  
178 -// Convert from root actor local space to physics space  
179 -Vector3 PhysicsImpl::TranslateToPhysicsSpace(Vector3 vector)  
180 -{  
181 - // root actor origin is top left, DALi Y is inverted.  
182 - // Physics origin is center. Y: 0->1 => 0.5=>-0.5  
183 - return Vector3(vector.x - mWorldOffset.x, mWorldOffset.y - vector.y, vector.z);  
184 -}  
185 -  
186 -// Convert from physics space to root actor local space  
187 -Vector3 PhysicsImpl::TranslateFromPhysicsSpace(Vector3 vector)  
188 -{  
189 - return Vector3(vector.x + mWorldOffset.x, mWorldOffset.y - vector.y, vector.z);  
190 -}  
191 -  
192 -// Convert a vector from dali space to physics space  
193 -Vector3 PhysicsImpl::ConvertVectorToPhysicsSpace(Vector3 vector)  
194 -{  
195 - // root actor origin is top left, DALi Y is inverted.  
196 - // @todo Add space config scale.  
197 - return Vector3(vector.x, -vector.y, vector.z);  
198 -}  
199 -  
200 -// Convert a vector physics space to root actor local space  
201 -Vector3 PhysicsImpl::ConvertVectorFromPhysicsSpace(Vector3 vector)  
202 -{  
203 - return Vector3(vector.x, -vector.y, vector.z);  
204 -}  
205 -  
206 -void PhysicsImpl::Integrate(float timestep)  
207 -{  
208 - if(mPhysicsIntegrateState)  
209 - {  
210 - cpSpaceStep(mSpace, timestep);  
211 - }  
212 - // if(mDynamicsWorld->getDebugDrawer() && mPhysicsDebugState)  
213 - // {  
214 - // mDynamicsWorld->debugDrawWorld();  
215 - // }  
216 -}  
217 -  
218 -cpBody* PhysicsImpl::HitTest(Vector2 screenCoords, Vector3 origin, Vector3 direction, Vector3& localPivot, float& distanceFromCamera)  
219 -{  
220 - Vector3 spacePosition = TranslateToPhysicsSpace(Vector3{screenCoords});  
221 - cpVect mousePosition = cpv(spacePosition.x, spacePosition.y);  
222 - cpFloat radius = 5.0f;  
223 - cpPointQueryInfo info = {0};  
224 - cpShape* shape = cpSpacePointQueryNearest(mSpace, mousePosition, radius, GRAB_FILTER, &info);  
225 -  
226 - cpBody* body{nullptr};  
227 -  
228 - if(shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY)  
229 - {  
230 - // Use the closest point on the surface if the click is outside the shape.  
231 - cpVect nearest = (info.distance > 0.0f ? info.point : mousePosition);  
232 - body = cpShapeGetBody(shape);  
233 - cpVect local = cpBodyWorldToLocal(body, nearest);  
234 - localPivot.x = local.x;  
235 - localPivot.y = local.y;  
236 - localPivot.z = 0.0;  
237 - }  
238 - return body;  
239 -}  
240 -  
241 -cpConstraint* PhysicsImpl::AddPivotJoint(cpBody* body1, cpBody* body2, Vector3 localPivot)  
242 -{  
243 - cpVect pivot{localPivot.x, localPivot.y};  
244 - cpConstraint* joint = cpPivotJointNew2(body2, body1, cpvzero, pivot);  
245 - cpConstraintSetMaxForce(joint, 50000.0f); // Magic numbers for mouse feedback.  
246 - cpConstraintSetErrorBias(joint, cpfpow(1.0f - 0.15f, 60.0f));  
247 - cpConstraint* constraint = cpSpaceAddConstraint(mSpace, joint);  
248 - return constraint; // Constraint & joint are the same...  
249 -}  
250 -  
251 -void PhysicsImpl::MoveMouseBody(cpBody* mouseBody, Vector3 position)  
252 -{  
253 - cpVect cpPosition = cpv(position.x, position.y);  
254 - cpVect newPoint = cpvlerp(cpBodyGetPosition(mouseBody), cpPosition, 0.25f);  
255 - cpBodySetVelocity(mouseBody, cpvmult(cpvsub(newPoint, cpBodyGetPosition(mouseBody)), 60.0f));  
256 - // Normally, kinematic body's position would be calculated by engine.  
257 - // For mouse, though, we want to set it.  
258 - cpBodySetPosition(mouseBody, newPoint);  
259 -}  
260 -  
261 -void PhysicsImpl::MoveConstraint(cpConstraint* constraint, Vector3 newPosition)  
262 -{  
263 -}  
264 -  
265 -void PhysicsImpl::ReleaseConstraint(cpConstraint* constraint)  
266 -{  
267 - cpSpaceRemoveConstraint(mSpace, constraint);  
268 - cpConstraintFree(constraint);  
269 -}  
270 -  
271 -int PhysicsImpl::ActivateBody(cpBody* body)  
272 -{  
273 - int oldState = cpBodyIsSleeping(body);  
274 - cpBodyActivate(body);  
275 -  
276 - return oldState;  
277 -}  
278 -  
279 -void PhysicsImpl::RestoreBodyState(cpBody* body, int oldState)  
280 -{  
281 - if(oldState)  
282 - {  
283 - cpBodyActivate(body);  
284 - }  
285 - else  
286 - {  
287 - cpBodySleep(body);  
288 - }  
289 -}  
examples/chipmunk-physics/physics-impl.h deleted
1 -#ifndef DALI_PHYSICS_DEMO_PHYSICS_IMPL_H  
2 -#define DALI_PHYSICS_DEMO_PHYSICS_IMPL_H  
3 -/*  
4 - * Copyright (c) 2023 Samsung Electronics Co., Ltd.  
5 - *  
6 - * Licensed under the Apache License, Version 2.0 (the "License");  
7 - * you may not use this file except in compliance with the License.  
8 - * You may obtain a copy of the License at  
9 - *  
10 - * http://www.apache.org/licenses/LICENSE-2.0  
11 - *  
12 - * Unless required by applicable law or agreed to in writing, software  
13 - * distributed under the License is distributed on an "AS IS" BASIS,  
14 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
15 - * See the License for the specific language governing permissions and  
16 - * limitations under the License.  
17 - *  
18 - */  
19 -  
20 -#include <dali/dali.h>  
21 -#include <dali/devel-api/threading/mutex.h>  
22 -#include <dali/devel-api/common/stage-devel.h>  
23 -  
24 -#include <map>  
25 -#include <chipmunk/chipmunk.h>  
26 -  
27 -#include "physics-actor.h"  
28 -#include "frame-callback.h"  
29 -  
30 -  
31 -class PhysicsImpl : public Dali::ConnectionTracker  
32 -{  
33 -public:  
34 - Dali::Actor Initialize(Dali::Window window);  
35 -  
36 - /**  
37 - * Create a layer & debug renderer  
38 - */  
39 - Dali::Layer CreateDebug(Dali::Vector2 windowSize);  
40 -  
41 - /**  
42 - * Converts a point in RootActor local coords (e.g. gesture)  
43 - * into physics space coords.  
44 - * @param vector The point to convert  
45 - * @return The converted point  
46 - */  
47 - Dali::Vector3 TranslateToPhysicsSpace(Dali::Vector3 vector);  
48 -  
49 - /**  
50 - * Converts a point in physics space coords.  
51 - * into RootActor local coords  
52 - * @param vector The point to convert  
53 - * @return The converted point  
54 - */  
55 - Dali::Vector3 TranslateFromPhysicsSpace(Dali::Vector3 vector);  
56 -  
57 - /**  
58 - * Converts a vector in DALi space into physics space.  
59 - * @param vector The vector to convert  
60 - * @return The converted vector  
61 - */  
62 - Dali::Vector3 ConvertVectorToPhysicsSpace(Dali::Vector3 vector);  
63 -  
64 - /**  
65 - * Converts a vector in physics space to DALi space  
66 - * @param vector The vector to convert  
67 - * @return The converted vector  
68 - */  
69 - Dali::Vector3 ConvertVectorFromPhysicsSpace(Dali::Vector3 vector);  
70 -  
71 - /**  
72 - * Set up the transform from world space to physics space  
73 - * @param[in] worldSize The 2d bounding box of the world in screen space  
74 - */  
75 - void SetTransform(Dali::Vector2 worldSize);  
76 -  
77 - /**  
78 - * Run the physics integration over the given timestep.  
79 - * @param timeStep  
80 - */  
81 - void Integrate(float timeStep);  
82 -  
83 - /**  
84 - * Toggle the integration state. If it's turned on, physics will run  
85 - * during the frame callback.  
86 - */  
87 - void ToggleIntegrateState()  
88 - {  
89 - mPhysicsIntegrateState ^= true;  
90 - }  
91 -  
92 - /**  
93 - * Toggle the debug state. If debug is turned on, use the physics engine  
94 - * debug to show wireframes.  
95 - */  
96 - void ToggleDebugState()  
97 - {  
98 - mPhysicsDebugState ^= true;  
99 - }  
100 -  
101 - void CreateWorldBounds(Dali::Window::WindowSize size);  
102 - cpShape* AddBound(cpBody* staticBody, cpVect start, cpVect end);  
103 - PhysicsActor& AddBall(Dali::Actor actor, float mass, float radius, float elasticity, float friction);  
104 - PhysicsActor& AddBrick(Dali::Actor actor,float mass, float elasticity, float friction, Dali::Vector3 size);  
105 -  
106 - cpBody* AddMouseBody();  
107 -  
108 - /**  
109 - * @param[in] screenCoords The touch point in screen coordinates  
110 - * @param[in] origin The camera origin in DALi world space  
111 - * @param[in] direction The ray direction in DALi world space  
112 - * @param[out] localPivot The hit point local to the body  
113 - * @param[out] distanceFromCamera The distance of the pick point from the camera  
114 - * @return nullptr if no dynamic body found, otherwise a valid ptr to the hit body.  
115 - */  
116 - cpBody* HitTest(Dali::Vector2 screenCoords, Dali::Vector3 origin, Dali::Vector3 direction,  
117 - Dali::Vector3& localPivot, float& distanceFromCamera);  
118 -  
119 - cpConstraint* AddPivotJoint(cpBody* body1, cpBody* body2, Dali::Vector3 localPivot);  
120 -  
121 - void MoveMouseBody(cpBody* mouseBody, Dali::Vector3 position);  
122 -  
123 - void MoveConstraint(cpConstraint* constraint, Dali::Vector3 newPosition);  
124 -  
125 - void ReleaseConstraint(cpConstraint* constraint);  
126 -  
127 - /**  
128 - * Ensure that the physics body does not go to sleep  
129 - * @param[in] body The physics body  
130 - * @return The old state  
131 - */  
132 - int ActivateBody(cpBody* body);  
133 -  
134 - /**  
135 - * Restore the state of the physics body  
136 - * @param[in] body The physics body  
137 - * @param[in] oldState The previous state to restore  
138 - */  
139 - void RestoreBodyState(cpBody* body, int oldState);  
140 -  
141 - /**  
142 - * Get the physics actor associated with the given body  
143 - * @param[in] body The physics body  
144 - * @return the associated physics actor  
145 - */  
146 - PhysicsActor* GetPhysicsActor(cpBody* body);  
147 -  
148 - /**  
149 - * Set the highlight state of the actor associated with the physics body  
150 - * @param[in] body The physics body  
151 - * @param[in] highlight Whether to turn the highlight on or off.  
152 - */  
153 - void HighlightBody(cpBody* body, bool highlight);  
154 -  
155 -  
156 -public:  
157 - std::map<uint32_t, PhysicsActor> mPhysicsActors;  
158 - bool mPhysicsIntegrateState{true};  
159 - bool mPhysicsDebugState{true};  
160 -  
161 - cpSpace* mSpace{nullptr};  
162 - cpShape* mLeftBound{nullptr};  
163 - cpShape* mRightBound{nullptr};  
164 - cpShape* mTopBound{nullptr};  
165 - cpShape* mBottomBound{nullptr};  
166 -  
167 - Dali::Window mWindow;  
168 - Dali::Mutex mMutex;  
169 -  
170 - Dali::Actor mPhysicsRoot;  
171 - Dali::Vector2 mWorldOffset;  
172 - FrameCallback* mFrameCallback{nullptr};  
173 -};  
174 -  
175 -#endif // DALI_PHYSICS_DEMO_PHYSICS_IMPL_H  
examples/chipmunk-physics/shaders/rendering-textured-shape.frag deleted
1 -uniform sampler2D uTexture;  
2 -uniform mediump float uBrightness;  
3 -varying mediump vec2 vTexCoord;  
4 -varying mediump vec3 vIllumination;  
5 -  
6 -mediump vec3 redistribute_rgb(mediump vec3 color)  
7 -{  
8 - mediump float threshold = 0.9999999;  
9 - mediump float m = max(max(color.r, color.g), color.b);  
10 - if(m <= threshold)  
11 - {  
12 - return color;  
13 - }  
14 - mediump float total = color.r + color.g + color.b;  
15 - if( total >= 3.0 * threshold)  
16 - {  
17 - return vec3(threshold);  
18 - }  
19 - mediump float x = (3.0 * threshold - total) / (3.0 * m - total);  
20 - mediump float gray = threshold - x * m;  
21 - return vec3(gray) + vec3(x)*color;  
22 -}  
23 -  
24 -void main()  
25 -{  
26 - mediump vec4 texColor = texture2D( uTexture, vTexCoord );  
27 - mediump vec3 pcol=texColor.rgb*(1.0+uBrightness);  
28 - gl_FragColor = vec4( redistribute_rgb(pcol), texColor.a);  
29 -}  
30 \ No newline at end of file 0 \ No newline at end of file
examples/chipmunk-physics/shaders/rendering-textured-shape.vert deleted
1 -attribute mediump vec3 aPosition; // DALi shader builtin  
2 -//attribute mediump vec2 aTexCoord; // DALi shader builtin  
3 -uniform mediump mat4 uMvpMatrix; // DALi shader builtin  
4 -uniform mediump mat4 uViewMatrix; // DALi shader builtin  
5 -uniform mediump mat4 uModelView; // DALi shader builtin  
6 -uniform mediump vec3 uSize; // DALi shader builtin  
7 -varying mediump vec3 vIllumination;  
8 -varying mediump vec2 vTexCoord;  
9 -  
10 -void main()  
11 -{  
12 - mediump vec4 vertexPosition = vec4(aPosition, 1.0);  
13 - mediump vec3 normal = normalize(vertexPosition.xyz);  
14 -  
15 - vertexPosition.xyz *= uSize;  
16 - vec4 pos = uModelView * vertexPosition;  
17 -  
18 - vec4 lightPosition = vec4(400.0, 0.0, 100.0, 1.0);  
19 - vec4 mvLightPos = uViewMatrix * lightPosition;  
20 - vec3 vectorToLight = normalize(mvLightPos.xyz - pos.xyz);  
21 - float lightDiffuse = max(dot(vectorToLight, normal), 0.0);  
22 -  
23 - vIllumination = vec3(lightDiffuse * 0.5 + 0.5);  
24 - vTexCoord = aPosition.xy*2.0;  
25 - gl_Position = uMvpMatrix * vertexPosition;  
26 -}  
27 \ No newline at end of file 0 \ No newline at end of file
packaging/com.samsung.dali-demo.spec
@@ -24,6 +24,7 @@ BuildRequires: pkgconfig(dali2-adaptor) @@ -24,6 +24,7 @@ BuildRequires: pkgconfig(dali2-adaptor)
24 BuildRequires: pkgconfig(dali2-toolkit) 24 BuildRequires: pkgconfig(dali2-toolkit)
25 BuildRequires: pkgconfig(dali2-scene3d) 25 BuildRequires: pkgconfig(dali2-scene3d)
26 BuildRequires: pkgconfig(dali2-physics-2d) 26 BuildRequires: pkgconfig(dali2-physics-2d)
  27 +BuildRequires: pkgconfig(dali2-physics-3d)
27 BuildRequires: pkgconfig(libtzplatform-config) 28 BuildRequires: pkgconfig(libtzplatform-config)
28 BuildRequires: pkgconfig(gles20) 29 BuildRequires: pkgconfig(gles20)
29 BuildRequires: pkgconfig(glesv2) 30 BuildRequires: pkgconfig(glesv2)