diff options
author | David Steele <david.steele@samsung.com> | 2023-08-23 18:50:42 +0100 |
---|---|---|
committer | David Steele <david.steele@samsung.com> | 2023-08-31 19:06:22 +0100 |
commit | 86c6cab5e9fcb2e798ccd606cd3e547c422f5336 (patch) | |
tree | fd9a5b620d0f512dc3603f0ca74c8887221b7b19 | |
parent | 0fb7eb9844f083c4edb33d1fe1ae3585cbb58659 (diff) | |
download | dali-demo-86c6cab5e9fcb2e798ccd606cd3e547c422f5336.tar.gz dali-demo-86c6cab5e9fcb2e798ccd606cd3e547c422f5336.tar.bz2 dali-demo-86c6cab5e9fcb2e798ccd606cd3e547c422f5336.zip |
Updated chipmunk demo to use Dali physics adaptor
Added dependency on dali-physics-3d for bullet-example
Change-Id: Ifaf5dd7dce079549012f25a094473aef2fe0f08a
-rw-r--r-- | build/tizen/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/chipmunk-physics/README.md | 7 | ||||
-rw-r--r-- | examples/chipmunk-physics/frame-callback.cpp | 65 | ||||
-rw-r--r-- | examples/chipmunk-physics/frame-callback.h | 58 | ||||
-rw-r--r-- | examples/chipmunk-physics/physics-actor.cpp | 98 | ||||
-rw-r--r-- | examples/chipmunk-physics/physics-actor.h | 106 | ||||
-rw-r--r-- | examples/chipmunk-physics/physics-demo-controller.cpp | 307 | ||||
-rw-r--r-- | examples/chipmunk-physics/physics-impl.cpp | 289 | ||||
-rw-r--r-- | examples/chipmunk-physics/physics-impl.h | 175 | ||||
-rw-r--r-- | examples/chipmunk-physics/shaders/rendering-textured-shape.frag | 29 | ||||
-rw-r--r-- | examples/chipmunk-physics/shaders/rendering-textured-shape.vert | 26 | ||||
-rwxr-xr-x | packaging/com.samsung.dali-demo.spec | 1 |
12 files changed, 235 insertions, 931 deletions
diff --git a/build/tizen/CMakeLists.txt b/build/tizen/CMakeLists.txt index 46c389c5..5270d570 100644 --- a/build/tizen/CMakeLists.txt +++ b/build/tizen/CMakeLists.txt @@ -327,6 +327,7 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc. FIND_PACKAGE( chipmunk ) FIND_PACKAGE( bullet3 ) + FIND_PACKAGE( dali2-physics-2d ) FIND_PACKAGE( dali2-physics-3d ) # Set up the include dir @@ -394,6 +395,10 @@ IF( WIN32 OR APPLE ) # WIN32 includes x64 as well according to the cmake doc. SET(ENABLE_PHYSICS_3D "ON" ) ENDIF() + IF (dali2-physics-2d_FOUND) + SET(DALI_PHYSICS_2D_LDFLAGS ${DALI_PHYSICS_2D_LDFLAGS} dali2-physics-2d::dali2-physics-2d) + ENDIF() + IF (dali2-physics-3d_FOUND) SET(DALI_PHYSICS_3D_LDFLAGS ${DALI_PHYSICS_3D_LDFLAGS} dali2-physics-3d::dali2-physics-3d) ENDIF() diff --git a/examples/chipmunk-physics/README.md b/examples/chipmunk-physics/README.md index 78adff1d..287fa274 100644 --- a/examples/chipmunk-physics/README.md +++ b/examples/chipmunk-physics/README.md @@ -1,6 +1,11 @@ # Chipmunk Physics Example This is an example showing how to use Chipmunk2D physics library to create and control physics objects in DALi. -It creates a ball and a pyramid brick wall which can be moved using touch and key events. +It creates a set of balls which act under gravity ![](./chipmunk.gif) + +"wasd" keys move the last touched actor up/down/left/right. +"qe" keys rotate the last touched actor in Z axis +"p" key resets the position/forces on the last touched actor to the origin +Space key toggles the integration state. diff --git a/examples/chipmunk-physics/frame-callback.cpp b/examples/chipmunk-physics/frame-callback.cpp deleted file mode 100644 index 129b6d75..00000000 --- a/examples/chipmunk-physics/frame-callback.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "frame-callback.h" -#include <dali/devel-api/threading/mutex.h> -#include <dali/devel-api/update/update-proxy.h> -#include <dali/integration-api/debug.h> -#include <dali/public-api/math/vector3.h> -#include "physics-impl.h" - -using Dali::Quaternion; -using Dali::Vector3; - -#if defined(DEBUG_ENABLED) -extern Debug::Filter* gPhysicsDemo; -#endif - -FrameCallback::FrameCallback(PhysicsImpl& physicsImpl) -: mPhysicsImpl(physicsImpl) -{ -} - -bool FrameCallback::Update(Dali::UpdateProxy& updateProxy, float elapsedSeconds) -{ - static int numCalls = 0; - - numCalls++; - if(numCalls % 30 == 0) - { - DALI_LOG_INFO(gPhysicsDemo, Debug::Concise, "Physics frame update\n"); - } - - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); - static float frameTime = 0; - frameTime += elapsedSeconds; - do - { - mPhysicsImpl.Integrate(mPhysicsTimeStep); - frameTime -= mPhysicsTimeStep; - } while(frameTime > 0); - - for(auto&& actor : mPhysicsImpl.mPhysicsActors) - { - // Get position, orientation from physics world. - Vector3 position = actor.second.GetActorPosition(); - updateProxy.BakePosition(actor.first, position); - Quaternion rotation = actor.second.GetActorRotation(); - updateProxy.BakeOrientation(actor.first, rotation); - } - - return true; -} diff --git a/examples/chipmunk-physics/frame-callback.h b/examples/chipmunk-physics/frame-callback.h deleted file mode 100644 index c45f0194..00000000 --- a/examples/chipmunk-physics/frame-callback.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef PHYSICS_DEMO_FRAME_CALLBACK_H -#define PHYSICS_DEMO_FRAME_CALLBACK_H - -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <dali/public-api/adaptor-framework/window.h> -#include <dali/devel-api/update/frame-callback-interface.h> -#include <map> -#include <chrono> - -class PhysicsImpl; - -class FrameCallback : public Dali::FrameCallbackInterface -{ -public: - /** - * Constructor - */ - explicit FrameCallback(PhysicsImpl& physicsImpl); - - /** - * Set the physics time step - * @param timeStep (in seconds) - */ - void SetPhysicsTimeStep(float timeStep) - { - mPhysicsTimeStep = timeStep; - } - -private: - /** - * Called each frame. - * @param[in] updateProxy Used to set world matrix and size - * @param[in] elapsedSeconds Time since last frame - * @return Whether we should keep rendering. - */ - bool Update(Dali::UpdateProxy& updateProxy, float elapsedSeconds) override; - -private: // Member variables - PhysicsImpl& mPhysicsImpl; - float mPhysicsTimeStep{1.0/180.0}; -}; - -#endif //PHYSICS_DEMO_FRAME_CALLBACK_H diff --git a/examples/chipmunk-physics/physics-actor.cpp b/examples/chipmunk-physics/physics-actor.cpp deleted file mode 100644 index 07f14a32..00000000 --- a/examples/chipmunk-physics/physics-actor.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "physics-actor.h" -#include "physics-impl.h" -#include <dali/public-api/common/constants.h> -#include <dali/public-api/math/vector3.h> -#include <dali/public-api/math/quaternion.h> - -using Dali::Vector3; -using Dali::Quaternion; -using Dali::Radian; - -void PhysicsActor::ClearForces() -{ - printf("Not Implemented\n"); - //mBody->clearForces(); - // No similar API -} - -Dali::Vector3 PhysicsActor::GetPhysicsPosition() -{ - cpVect cpPosition = cpBodyGetPosition(mBody); - return Vector3(cpPosition.x, cpPosition.y, 0.0f); -} - -void PhysicsActor::SetPhysicsPosition(Dali::Vector3 actorPosition) -{ - Dali::Mutex::ScopedLock lock(mImpl->mMutex); - Vector3 physicsPosition = mImpl->TranslateToPhysicsSpace(actorPosition); - cpBodySetPosition(mBody, cpv(physicsPosition.x, physicsPosition.y)); -} - -void PhysicsActor::SetPhysicsVelocity(Dali::Vector3 actorVelocity) -{ - Dali::Mutex::ScopedLock lock(mImpl->mMutex); - Vector3 physicsVelocity = mImpl->ConvertVectorToPhysicsSpace(actorVelocity); - cpBodySetVelocity(mBody, cpv(physicsVelocity.x, physicsVelocity.y)); -} - -void PhysicsActor::SetPhysicsAngularVelocity(Dali::Vector3 velocity) -{ - Dali::Mutex::ScopedLock lock(mImpl->mMutex); - printf("Not Implemented\n"); - //mBody->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z)); -} - -Quaternion PhysicsActor::GetPhysicsRotation() -{ - return Quaternion{}; -} - -void PhysicsActor::SetPhysicsRotation(Dali::Quaternion rotation) -{ - Dali::Mutex::ScopedLock lock(mImpl->mMutex); - - Vector3 axis; - Radian angle; - rotation.ToAxisAngle(axis, angle); - - //btQuaternion orn = btQuaternion(btVector3(axis.x, -axis.y, axis.z), btScalar(float(-angle))); - //btTransform& transform = mBody->getWorldTransform(); - //transform.setRotation(orn); - printf("Not Implemented\n"); -} - - -Vector3 PhysicsActor::GetActorPosition() -{ - cpVect cpPosition = cpBodyGetPosition(mBody); - return mImpl->TranslateFromPhysicsSpace(Vector3(cpPosition.x, cpPosition.y, 0.0f)); -} - -Vector3 PhysicsActor::GetActorVelocity() -{ - cpVect cpVelocity = cpBodyGetVelocity(mBody); - return mImpl->ConvertVectorFromPhysicsSpace(Vector3(cpVelocity.x, cpVelocity.y, 0.0f)); -} - -Quaternion PhysicsActor::GetActorRotation() -{ - cpFloat angle = cpBodyGetAngle(mBody); - return Quaternion(Radian(angle), -Vector3::ZAXIS); -} diff --git a/examples/chipmunk-physics/physics-actor.h b/examples/chipmunk-physics/physics-actor.h deleted file mode 100644 index aeaa6127..00000000 --- a/examples/chipmunk-physics/physics-actor.h +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H -#define DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <chipmunk/chipmunk.h> -#include <cstdint> -#include <dali/dali.h> -#include <iosfwd> - -// Forward declarations -class PhysicsImpl; - -/** - * Class that associates an actor with a physics body. (Initially, rigid body) - */ -class PhysicsActor -{ -public: - PhysicsActor() = default; - PhysicsActor(Dali::Actor& actor, cpBody* body, PhysicsImpl* impl, Dali::Property::Index brightnessId) - : mImpl(impl), - mActorId(actor.GetProperty<int>(Dali::Actor::Property::ID)), - mBody(body), - mBrightnessIndex(brightnessId) - { - cpBodySetUserData(mBody, this); - } - - PhysicsActor(const PhysicsActor& rhs)=delete; - PhysicsActor& operator=(const PhysicsActor& rhs)=delete; - - PhysicsActor(const PhysicsActor&& rhs) - { - if(this != &rhs) - { - mImpl = rhs.mImpl; - mActorId = rhs.mActorId; - mBody = rhs.mBody; - cpBodySetUserData(mBody, this); - mBrightnessIndex = rhs.mBrightnessIndex; - } - } - - PhysicsActor& operator=(const PhysicsActor&& rhs) - { - if(this != &rhs) - { - mActorId = rhs.mActorId; - mBody = rhs.mBody; - mImpl = rhs.mImpl; - mBrightnessIndex = rhs.mBrightnessIndex; - cpBodySetUserData(mBody, this); - } - return *this; - } - - uint32_t GetId() - { - return mActorId; - } - - cpBody* GetBody() - { - return mBody; - } - - Dali::Property::Index GetBrightnessIndex() - { - return mBrightnessIndex; - } - - Dali::Vector3 GetPhysicsPosition(); - Dali::Quaternion GetPhysicsRotation(); - - void SetPhysicsPosition(Dali::Vector3 actorPosition); - void SetPhysicsVelocity(Dali::Vector3 actorVelocity); - void SetPhysicsAngularVelocity(Dali::Vector3 actorVelocity); - void SetPhysicsRotation(Dali::Quaternion actorRotation); - Dali::Vector3 GetActorPosition(); - Dali::Vector3 GetActorVelocity(); - Dali::Quaternion GetActorRotation(); - void ClearForces(); - -private: - PhysicsImpl* mImpl{nullptr}; - uint32_t mActorId{0}; - cpBody* mBody{nullptr}; - Dali::Property::Index mBrightnessIndex{Dali::Property::INVALID_INDEX}; -}; - -#endif // DALI_PHYSICS_DEMO_PHYSICS_ACTOR_H diff --git a/examples/chipmunk-physics/physics-demo-controller.cpp b/examples/chipmunk-physics/physics-demo-controller.cpp index 377322a8..6baf71b9 100644 --- a/examples/chipmunk-physics/physics-demo-controller.cpp +++ b/examples/chipmunk-physics/physics-demo-controller.cpp @@ -15,7 +15,7 @@ */ #include <dali-toolkit/dali-toolkit.h> -#include <dali/dali.h> +#include <dali-physics/dali-physics.h> #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h> #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h> @@ -25,18 +25,18 @@ #include <iostream> #include <string> - -#include "generated/rendering-textured-shape-frag.h" -#include "generated/rendering-textured-shape-vert.h" -#include "physics-actor.h" -#include "physics-impl.h" +#include <chipmunk/chipmunk.h> using namespace Dali; +using namespace Dali::Toolkit::Physics; #if defined(DEBUG_ENABLED) Debug::Filter* gPhysicsDemo = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS_EXAMPLE"); #endif +#define GRABBABLE_MASK_BIT (1u << 31) +cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT}; + namespace KeyModifier { enum Key @@ -84,25 +84,39 @@ public: mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY); Window::WindowSize windowSize = mWindow.GetSize(); - mPhysicsRoot = mPhysicsImpl.Initialize(mWindow); + // Map Physics space (origin bottom left, +ve Y up) + // to DALi space (origin center, +ve Y down) + mPhysicsTransform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f)); + mPhysicsTransform.SetTranslation(Vector3(windowSize.GetWidth() * 0.5f, + windowSize.GetHeight() * 0.5f, + 0.0f)); + + mPhysicsAdaptor = PhysicsAdaptor::New(mPhysicsTransform, windowSize); + mPhysicsRoot = mPhysicsAdaptor.GetRootActor(); mPhysicsRoot.TouchedSignal().Connect(this, &PhysicsDemoController::OnTouched); mWindow.Add(mPhysicsRoot); + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>(); + + CreateBounds(space, windowSize); // Ball area = 2*PI*26^2 ~= 6.28*26*26 ~= 5400 // Fill quarter of the screen... int numBalls = 10 + windowSize.GetWidth() * windowSize.GetHeight() / 20000; - for(int i = 0; i < numBalls; ++i) { - CreateBall(); + CreateBall(space); } // For funky mouse drag - mMouseBody = mPhysicsImpl.AddMouseBody(); + mMouseBody = cpBodyNewKinematic(); // Mouse actor is a kinematic body that is not integrated + + // Process any async queued methods next frame + mPhysicsAdaptor.CreateSyncPoint(); } - void CreateBall() + void CreateBall(cpSpace* space) { const float BALL_MASS = 10.0f; const float BALL_RADIUS = 26.0f; @@ -111,13 +125,81 @@ public: auto ball = Toolkit::ImageView::New(BALL_IMAGE); - auto& physicsBall = mPhysicsImpl.AddBall(ball, BALL_MASS, BALL_RADIUS, BALL_ELASTICITY, BALL_FRICTION); + cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero))); + + cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero)); + cpShapeSetElasticity(shape, BALL_ELASTICITY); + cpShapeSetFriction(shape, BALL_FRICTION); + + ball.RegisterProperty("uBrightness", 0.0f); + + PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body); + Window::WindowSize windowSize = mWindow.GetSize(); - const float s = BALL_RADIUS; - const float fw = windowSize.GetWidth() - BALL_RADIUS; - const float fh = windowSize.GetHeight() - BALL_RADIUS; - physicsBall.SetPhysicsPosition(Vector3(Random::Range(s, fw), Random::Range(s, fh), 0.0f)); - physicsBall.SetPhysicsVelocity(Vector3(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0), 0.0f)); + + const float fw = 0.5f*(windowSize.GetWidth() - BALL_RADIUS); + const float fh = 0.5f*(windowSize.GetHeight() - BALL_RADIUS); + + // Example of setting physics property on update thread + physicsBall.AsyncSetPhysicsPosition(Vector3(Random::Range(-fw, fw), Random::Range(-fh, fh), 0.0f)); + + // Example of queuing a chipmunk method to run on the update thread + mPhysicsAdaptor.Queue([body](){ + cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0))); + }); + } + + void CreateBounds(cpSpace* space, Window::WindowSize size) + { + // We're working in physics space here - coords are: origin: bottom left, +ve Y: up + int xBound = size.GetWidth(); + int yBound = size.GetHeight(); + + cpBody* staticBody = cpSpaceGetStaticBody(space); + + if(mLeftBound) + { + cpSpaceRemoveShape(space, mLeftBound); + cpSpaceRemoveShape(space, mRightBound); + cpSpaceRemoveShape(space, mTopBound); + cpSpaceRemoveShape(space, mBottomBound); + cpShapeFree(mLeftBound); + cpShapeFree(mRightBound); + cpShapeFree(mTopBound); + cpShapeFree(mBottomBound); + } + mLeftBound = AddBound(space, staticBody, cpv(0, 0), cpv(0, yBound)); + mRightBound = AddBound(space, staticBody, cpv(xBound, 0), cpv(xBound, yBound)); + mTopBound = AddBound(space, staticBody, cpv(0, 0), cpv(xBound, 0)); + mBottomBound = AddBound(space, staticBody, cpv(0, yBound), cpv(xBound, yBound)); + } + + cpShape* AddBound(cpSpace* space, cpBody* staticBody, cpVect start, cpVect end) + { + cpShape* shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, start, end, 0.0f)); + cpShapeSetElasticity(shape, 1.0f); + cpShapeSetFriction(shape, 1.0f); + + cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); + return shape; + } + + void MoveMouseBody(cpBody* mouseBody, Vector3 position) + { + cpVect cpPosition = cpv(position.x, position.y); + cpVect newPoint = cpvlerp(cpBodyGetPosition(mouseBody), cpPosition, 0.25f); + cpBodySetVelocity(mouseBody, cpvmult(cpvsub(newPoint, cpBodyGetPosition(mouseBody)), 60.0f)); + cpBodySetPosition(mouseBody, newPoint); + } + + cpConstraint* AddPivotJoint(cpSpace* space, cpBody* body1, cpBody* body2, Vector3 localPivot) + { + cpVect pivot{localPivot.x, localPivot.y}; + cpConstraint* joint = cpPivotJointNew2(body2, body1, cpvzero, pivot); + cpConstraintSetMaxForce(joint, 50000.0f); // Magic numbers for mouse feedback. + cpConstraintSetErrorBias(joint, cpfpow(1.0f - 0.15f, 60.0f)); + cpConstraint* constraint = cpSpaceAddConstraint(space, joint); + return constraint; // Constraint & joint are the same... } void OnTerminate(Application& application) @@ -127,21 +209,24 @@ public: void OnWindowResize(Window window, Window::WindowSize newSize) { - mPhysicsImpl.CreateWorldBounds(newSize); + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>(); + + CreateBounds(space, newSize); } bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) { static enum { None, - MoveCameraXZ, MovePivot, } state = None; - auto renderTask = mWindow.GetRenderTaskList().GetTask(0); - auto screenCoords = touch.GetScreenPosition(0); - Vector3 origin, direction; - Dali::HitTestAlgorithm::BuildPickingRay(renderTask, screenCoords, origin, direction); + auto renderTask = mWindow.GetRenderTaskList().GetTask(0); + auto screenCoords = touch.GetScreenPosition(0); + // In this demo, physics space is equivalent to screen space with y inverted + auto windowSize = mWindow.GetSize(); + Vector3 rayPhysicsOrigin(screenCoords.x, windowSize.GetHeight() - screenCoords.y, 0.0f); switch(state) { @@ -149,30 +234,23 @@ public: { if(touch.GetState(0) == Dali::PointState::STARTED) { - if(mCtrlDown) - { - state = MoveCameraXZ; - // local to top left - //cameraY = touch.GetLocalPosition(0).y; - // Could move on fixed plane, e.g. y=0. - // position.Y corresponds to a z value depending on perspective - // position.X scales to an x value depending on perspective - } - else + state = MovePivot; + + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>(); + + Vector3 localPivot; + float pickingDistance; + + auto body = scopedAccessor->HitTest(rayPhysicsOrigin, rayPhysicsOrigin, localPivot, pickingDistance); + if(!body.Empty()) { - state = MovePivot; - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); + mPickedBody = body.Get<cpBody*>(); + mSelectedActor = mPhysicsAdaptor.GetPhysicsActor(mPickedBody); - Vector3 localPivot; - float pickingDistance; - auto body = mPhysicsImpl.HitTest(screenCoords, origin, direction, localPivot, pickingDistance); - if(body) - { - mPickedBody = body; - mPhysicsImpl.HighlightBody(mPickedBody, true); - mPickedSavedState = mPhysicsImpl.ActivateBody(mPickedBody); - mPickedConstraint = mPhysicsImpl.AddPivotJoint(mPickedBody, mMouseBody, localPivot); - } + mPickedSavedState = cpBodyIsSleeping(mPickedBody); + cpBodyActivate(mPickedBody); + mPickedConstraint = AddPivotJoint(space, mPickedBody, mMouseBody, localPivot); } } break; @@ -183,23 +261,11 @@ public: { if(mPickedBody && mPickedConstraint) { - if(!mShiftDown) - { - // Move point in XY plane, projected into scene - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); + // Ensure we get a lock before altering constraints + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); - Vector3 position = mPhysicsImpl.TranslateToPhysicsSpace(Vector3(screenCoords)); - mPhysicsImpl.MoveMouseBody(mMouseBody, position); - } - else - { - // Move point in XZ plane - // Above vanishing pt, it's on top plane of frustum; below vanishing pt it's on bottom plane. - // Kind of want to project onto the plane using initial touch xy, rather than top/bottom. - // Whole new projection code needed. - - // Cheat! - } + // Move point in physics coords + MoveMouseBody(mMouseBody, rayPhysicsOrigin); } } else if(touch.GetState(0) == Dali::PointState::FINISHED || @@ -207,11 +273,20 @@ public: { if(mPickedConstraint) { - mPhysicsImpl.HighlightBody(mPickedBody, false); + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor(); + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>(); - Dali::Mutex::ScopedLock lock(mPhysicsImpl.mMutex); - mPhysicsImpl.RestoreBodyState(mPickedBody, mPickedSavedState); - mPhysicsImpl.ReleaseConstraint(mPickedConstraint); + if(mPickedSavedState) + { + cpBodyActivate(mPickedBody); + } + else + { + cpBodySleep(mPickedBody); + } + + cpSpaceRemoveConstraint(space, mPickedConstraint); + cpConstraintFree(mPickedConstraint); mPickedConstraint = nullptr; mPickedBody = nullptr; } @@ -219,23 +294,9 @@ public: } break; } - case MoveCameraXZ: - { - if(touch.GetState(0) == Dali::PointState::MOTION) - { - // Move camera in XZ plane - //float y = cameraY; // touch point in Y. Move camera in an XZ plane on this point. - } - else if(touch.GetState(0) == Dali::PointState::FINISHED || - touch.GetState(0) == Dali::PointState::INTERRUPTED) - { - state = None; - } - break; - } } - //std::cout<<"Touch State: "<<state<<std::endl; + Stage::GetCurrent().KeepRendering(30.0f); return true; @@ -243,6 +304,8 @@ public: void OnKeyEv(const Dali::KeyEvent& event) { + static bool integrateState{true}; + if(event.GetState() == KeyEvent::DOWN) { switch(event.GetKeyCode()) @@ -273,11 +336,80 @@ public: } else if(!event.GetKeyString().compare(" ")) { - mPhysicsImpl.ToggleIntegrateState(); + integrateState = true^integrateState; + mPhysicsAdaptor.SetIntegrationState(integrateState? + PhysicsAdaptor::IntegrationState::ON: + PhysicsAdaptor::IntegrationState::OFF); + } - else if(!event.GetKeyString().compare("m")) + else if(!event.GetKeyString().compare("w")) { - mPhysicsImpl.ToggleDebugState(); + if(mSelectedActor) + { + Vector3 pos = mSelectedActor.GetActorPosition(); + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(0, -10, 0)); + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } + } + else if(!event.GetKeyString().compare("s")) + { + if(mSelectedActor) + { + Vector3 pos = mSelectedActor.GetActorPosition(); + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(0, 10, 0)); + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } + } + else if(!event.GetKeyString().compare("a")) + { + if(mSelectedActor) + { + Vector3 pos = mSelectedActor.GetActorPosition(); + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(-10, 0, 0)); + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } + } + else if(!event.GetKeyString().compare("d")) + { + if(mSelectedActor) + { + Vector3 pos = mSelectedActor.GetActorPosition(); + mSelectedActor.AsyncSetPhysicsPosition(pos + Vector3(10, 0, 0)); + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } + } + else if(!event.GetKeyString().compare("q")) + { + // Rotate anti-clockwise + if(mSelectedActor) + { + Quaternion quaternion = mSelectedActor.GetActorRotation(); + quaternion *= Quaternion(Degree(-15.0f), Vector3::ZAXIS); + mSelectedActor.AsyncSetPhysicsRotation(quaternion); + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } + } + else if(!event.GetKeyString().compare("e")) + { + // Rotate clockwise using native physics APIs + if(mSelectedActor) + { + cpBody* body = mSelectedActor.GetBody().Get<cpBody*>(); + float angle = cpBodyGetAngle(body); + mPhysicsAdaptor.Queue([body, angle]() { cpBodySetAngle(body, angle-Math::PI/12.0f); }); + mPhysicsAdaptor.Queue([body]() { cpBodyActivate(body); }); + mPhysicsAdaptor.CreateSyncPoint(); + } } break; } @@ -313,13 +445,20 @@ private: Application& mApplication; Window mWindow; - PhysicsImpl mPhysicsImpl; + PhysicsAdaptor mPhysicsAdaptor; + PhysicsActor mSelectedActor; + Matrix mPhysicsTransform; Actor mPhysicsRoot; cpBody* mMouseBody{nullptr}; cpBody* mPickedBody{nullptr}; cpConstraint* mPickedConstraint{nullptr}; int mPickedSavedState = -1; /// 0 : Active, 1 : Sleeping + cpShape* mLeftBound{nullptr}; + cpShape* mRightBound{nullptr}; + cpShape* mTopBound{nullptr}; + cpShape* mBottomBound{nullptr}; + bool mCtrlDown{false}; bool mAltDown{false}; bool mShiftDown{false}; diff --git a/examples/chipmunk-physics/physics-impl.cpp b/examples/chipmunk-physics/physics-impl.cpp deleted file mode 100644 index 1916ee0f..00000000 --- a/examples/chipmunk-physics/physics-impl.cpp +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "physics-impl.h" -#include "physics-actor.h" - -#include <devel-api/common/stage.h> -#include <iostream> -#include <map> -#include <utility> - -using Dali::Actor; -using Dali::Layer; -using Dali::Stage; -using Dali::Vector2; -using Dali::Vector3; -using Dali::Window; -using namespace Dali::DevelStage; - -#define GRABBABLE_MASK_BIT (1u << 31) -cpShapeFilter GRAB_FILTER = {CP_NO_GROUP, GRABBABLE_MASK_BIT, GRABBABLE_MASK_BIT}; -cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT}; - -Actor PhysicsImpl::Initialize(Window window) -{ - mWindow = window; - mSpace = cpSpaceNew(); - cpSpaceSetIterations(mSpace, 30); - cpSpaceSetSleepTimeThreshold(mSpace, 0.5f); - cpSpaceSetGravity(mSpace, cpv(0, -200)); - - auto windowSize = window.GetSize(); - CreateWorldBounds(windowSize); - - // Create an actor that can handle mouse events. - mPhysicsRoot = Layer::New(); - mPhysicsRoot[Actor::Property::SIZE] = Vector2(windowSize.GetWidth(), windowSize.GetHeight()); - mPhysicsRoot[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER; - mPhysicsRoot[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER; - - mFrameCallback = new FrameCallback(*this); - AddFrameCallback(Stage::GetCurrent(), *mFrameCallback, window.GetRootLayer()); - Stage::GetCurrent().KeepRendering(30); - - return mPhysicsRoot; -} - -Layer PhysicsImpl::CreateDebug(Vector2 windowSize) -{ - return Layer(); -} - -void PhysicsImpl::CreateWorldBounds(Window::WindowSize size) -{ - // Physics origin is 0,0,0 in DALi coords. - // But, Y is inverted, so bottom is -ve, top is +ve. - // Perform this correction when applying position to actor. - // But, can't use actors in update, so cache transform. - SetTransform(Vector2(size.GetWidth(), size.GetHeight())); - - int xBound = size.GetWidth() / 2; - int yBound = size.GetHeight() / 2; - - cpBody* staticBody = cpSpaceGetStaticBody(mSpace); - - if(mLeftBound) - { - cpSpaceRemoveShape(mSpace, mLeftBound); - cpSpaceRemoveShape(mSpace, mRightBound); - cpSpaceRemoveShape(mSpace, mTopBound); - cpSpaceRemoveShape(mSpace, mBottomBound); - cpShapeFree(mLeftBound); - cpShapeFree(mRightBound); - cpShapeFree(mTopBound); - cpShapeFree(mBottomBound); - } - mLeftBound = AddBound(staticBody, cpv(-xBound, -yBound), cpv(-xBound, yBound)); - mRightBound = AddBound(staticBody, cpv(xBound, -yBound), cpv(xBound, yBound)); - mTopBound = AddBound(staticBody, cpv(-xBound, -yBound), cpv(xBound, -yBound)); - mBottomBound = AddBound(staticBody, cpv(-xBound, yBound), cpv(xBound, yBound)); -} - -void PhysicsImpl::SetTransform(Vector2 worldSize) -{ - mWorldOffset.x = worldSize.x * 0.5f; - mWorldOffset.y = worldSize.y * 0.5f; - // y is always inverted. -} - -cpShape* PhysicsImpl::AddBound(cpBody* staticBody, cpVect start, cpVect end) -{ - cpShape* shape = cpSpaceAddShape(mSpace, cpSegmentShapeNew(staticBody, start, end, 0.0f)); - cpShapeSetElasticity(shape, 1.0f); - cpShapeSetFriction(shape, 1.0f); - cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); - return shape; -} - -PhysicsActor& PhysicsImpl::AddBall(::Actor actor, float mass, float radius, float elasticity, float friction) -{ - Dali::Mutex::ScopedLock lock(mMutex); - cpBody* body = cpSpaceAddBody(mSpace, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); - cpBodySetPosition(body, cpv(0, 0)); - cpBodySetVelocity(body, cpv(0, 0)); - - cpShape* shape = cpSpaceAddShape(mSpace, cpCircleShapeNew(body, radius, cpvzero)); - cpShapeSetElasticity(shape, elasticity); - cpShapeSetFriction(shape, friction); - - int id = actor[Actor::Property::ID]; - Dali::Property::Index index = actor.RegisterProperty("uBrightness", 0.0f); - mPhysicsActors.insert(std::make_pair(id, PhysicsActor{actor, body, this, index})); - actor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::TOP_LEFT; - actor[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER; - mPhysicsRoot.Add(actor); - return mPhysicsActors.at(id); -} - -PhysicsActor& PhysicsImpl::AddBrick(Dali::Actor actor, float mass, float elasticity, float friction, Vector3 size) -{ - Dali::Mutex::ScopedLock lock(mMutex); - cpBody* body = cpSpaceAddBody(mSpace, cpBodyNew(mass, cpMomentForBox(mass, size.width, size.height))); - cpBodySetPosition(body, cpv(0, 0)); - cpBodySetVelocity(body, cpv(0, 0)); - - cpShape* shape = cpSpaceAddShape(mSpace, cpBoxShapeNew(body, size.width, size.height, 0.0f)); - cpShapeSetFriction(shape, friction); - cpShapeSetElasticity(shape, elasticity); - - int id = actor[Actor::Property::ID]; - Dali::Property::Index index = actor.RegisterProperty("uBrightness", 0.0f); - mPhysicsActors.insert(std::make_pair(id, PhysicsActor{actor, body, this, index})); - actor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::TOP_LEFT; - actor[Actor::Property::ANCHOR_POINT] = Dali::AnchorPoint::CENTER; - mPhysicsRoot.Add(actor); - return mPhysicsActors.at(id); -} - -cpBody* PhysicsImpl::AddMouseBody() -{ - Dali::Mutex::ScopedLock lock(mMutex); - auto kinematicBody = cpBodyNewKinematic(); // Mouse actor is a kinematic body that is not integrated - return kinematicBody; -} - -PhysicsActor* PhysicsImpl::GetPhysicsActor(cpBody* body) -{ - return reinterpret_cast<PhysicsActor*>(cpBodyGetUserData(body)); -} - -void PhysicsImpl::HighlightBody(cpBody* body, bool highlight) -{ - auto physicsActor = GetPhysicsActor(body); - if(physicsActor) - { - Actor actor = mPhysicsRoot.FindChildById(physicsActor->GetId()); - if(actor) - { - actor[physicsActor->GetBrightnessIndex()] = highlight ? 1.0f : 0.0f; - } - } -} - -// Convert from root actor local space to physics space -Vector3 PhysicsImpl::TranslateToPhysicsSpace(Vector3 vector) -{ - // root actor origin is top left, DALi Y is inverted. - // Physics origin is center. Y: 0->1 => 0.5=>-0.5 - return Vector3(vector.x - mWorldOffset.x, mWorldOffset.y - vector.y, vector.z); -} - -// Convert from physics space to root actor local space -Vector3 PhysicsImpl::TranslateFromPhysicsSpace(Vector3 vector) -{ - return Vector3(vector.x + mWorldOffset.x, mWorldOffset.y - vector.y, vector.z); -} - -// Convert a vector from dali space to physics space -Vector3 PhysicsImpl::ConvertVectorToPhysicsSpace(Vector3 vector) -{ - // root actor origin is top left, DALi Y is inverted. - // @todo Add space config scale. - return Vector3(vector.x, -vector.y, vector.z); -} - -// Convert a vector physics space to root actor local space -Vector3 PhysicsImpl::ConvertVectorFromPhysicsSpace(Vector3 vector) -{ - return Vector3(vector.x, -vector.y, vector.z); -} - -void PhysicsImpl::Integrate(float timestep) -{ - if(mPhysicsIntegrateState) - { - cpSpaceStep(mSpace, timestep); - } - // if(mDynamicsWorld->getDebugDrawer() && mPhysicsDebugState) - // { - // mDynamicsWorld->debugDrawWorld(); - // } -} - -cpBody* PhysicsImpl::HitTest(Vector2 screenCoords, Vector3 origin, Vector3 direction, Vector3& localPivot, float& distanceFromCamera) -{ - Vector3 spacePosition = TranslateToPhysicsSpace(Vector3{screenCoords}); - cpVect mousePosition = cpv(spacePosition.x, spacePosition.y); - cpFloat radius = 5.0f; - cpPointQueryInfo info = {0}; - cpShape* shape = cpSpacePointQueryNearest(mSpace, mousePosition, radius, GRAB_FILTER, &info); - - cpBody* body{nullptr}; - - if(shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY) - { - // Use the closest point on the surface if the click is outside the shape. - cpVect nearest = (info.distance > 0.0f ? info.point : mousePosition); - body = cpShapeGetBody(shape); - cpVect local = cpBodyWorldToLocal(body, nearest); - localPivot.x = local.x; - localPivot.y = local.y; - localPivot.z = 0.0; - } - return body; -} - -cpConstraint* PhysicsImpl::AddPivotJoint(cpBody* body1, cpBody* body2, Vector3 localPivot) -{ - cpVect pivot{localPivot.x, localPivot.y}; - cpConstraint* joint = cpPivotJointNew2(body2, body1, cpvzero, pivot); - cpConstraintSetMaxForce(joint, 50000.0f); // Magic numbers for mouse feedback. - cpConstraintSetErrorBias(joint, cpfpow(1.0f - 0.15f, 60.0f)); - cpConstraint* constraint = cpSpaceAddConstraint(mSpace, joint); - return constraint; // Constraint & joint are the same... -} - -void PhysicsImpl::MoveMouseBody(cpBody* mouseBody, Vector3 position) -{ - cpVect cpPosition = cpv(position.x, position.y); - cpVect newPoint = cpvlerp(cpBodyGetPosition(mouseBody), cpPosition, 0.25f); - cpBodySetVelocity(mouseBody, cpvmult(cpvsub(newPoint, cpBodyGetPosition(mouseBody)), 60.0f)); - // Normally, kinematic body's position would be calculated by engine. - // For mouse, though, we want to set it. - cpBodySetPosition(mouseBody, newPoint); -} - -void PhysicsImpl::MoveConstraint(cpConstraint* constraint, Vector3 newPosition) -{ -} - -void PhysicsImpl::ReleaseConstraint(cpConstraint* constraint) -{ - cpSpaceRemoveConstraint(mSpace, constraint); - cpConstraintFree(constraint); -} - -int PhysicsImpl::ActivateBody(cpBody* body) -{ - int oldState = cpBodyIsSleeping(body); - cpBodyActivate(body); - - return oldState; -} - -void PhysicsImpl::RestoreBodyState(cpBody* body, int oldState) -{ - if(oldState) - { - cpBodyActivate(body); - } - else - { - cpBodySleep(body); - } -} diff --git a/examples/chipmunk-physics/physics-impl.h b/examples/chipmunk-physics/physics-impl.h deleted file mode 100644 index 9420fa4b..00000000 --- a/examples/chipmunk-physics/physics-impl.h +++ /dev/null @@ -1,175 +0,0 @@ -#ifndef DALI_PHYSICS_DEMO_PHYSICS_IMPL_H -#define DALI_PHYSICS_DEMO_PHYSICS_IMPL_H -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <dali/dali.h> -#include <dali/devel-api/threading/mutex.h> -#include <dali/devel-api/common/stage-devel.h> - -#include <map> -#include <chipmunk/chipmunk.h> - -#include "physics-actor.h" -#include "frame-callback.h" - - -class PhysicsImpl : public Dali::ConnectionTracker -{ -public: - Dali::Actor Initialize(Dali::Window window); - - /** - * Create a layer & debug renderer - */ - Dali::Layer CreateDebug(Dali::Vector2 windowSize); - - /** - * Converts a point in RootActor local coords (e.g. gesture) - * into physics space coords. - * @param vector The point to convert - * @return The converted point - */ - Dali::Vector3 TranslateToPhysicsSpace(Dali::Vector3 vector); - - /** - * Converts a point in physics space coords. - * into RootActor local coords - * @param vector The point to convert - * @return The converted point - */ - Dali::Vector3 TranslateFromPhysicsSpace(Dali::Vector3 vector); - - /** - * Converts a vector in DALi space into physics space. - * @param vector The vector to convert - * @return The converted vector - */ - Dali::Vector3 ConvertVectorToPhysicsSpace(Dali::Vector3 vector); - - /** - * Converts a vector in physics space to DALi space - * @param vector The vector to convert - * @return The converted vector - */ - Dali::Vector3 ConvertVectorFromPhysicsSpace(Dali::Vector3 vector); - - /** - * Set up the transform from world space to physics space - * @param[in] worldSize The 2d bounding box of the world in screen space - */ - void SetTransform(Dali::Vector2 worldSize); - - /** - * Run the physics integration over the given timestep. - * @param timeStep - */ - void Integrate(float timeStep); - - /** - * Toggle the integration state. If it's turned on, physics will run - * during the frame callback. - */ - void ToggleIntegrateState() - { - mPhysicsIntegrateState ^= true; - } - - /** - * Toggle the debug state. If debug is turned on, use the physics engine - * debug to show wireframes. - */ - void ToggleDebugState() - { - mPhysicsDebugState ^= true; - } - - void CreateWorldBounds(Dali::Window::WindowSize size); - cpShape* AddBound(cpBody* staticBody, cpVect start, cpVect end); - PhysicsActor& AddBall(Dali::Actor actor, float mass, float radius, float elasticity, float friction); - PhysicsActor& AddBrick(Dali::Actor actor,float mass, float elasticity, float friction, Dali::Vector3 size); - - cpBody* AddMouseBody(); - - /** - * @param[in] screenCoords The touch point in screen coordinates - * @param[in] origin The camera origin in DALi world space - * @param[in] direction The ray direction in DALi world space - * @param[out] localPivot The hit point local to the body - * @param[out] distanceFromCamera The distance of the pick point from the camera - * @return nullptr if no dynamic body found, otherwise a valid ptr to the hit body. - */ - cpBody* HitTest(Dali::Vector2 screenCoords, Dali::Vector3 origin, Dali::Vector3 direction, - Dali::Vector3& localPivot, float& distanceFromCamera); - - cpConstraint* AddPivotJoint(cpBody* body1, cpBody* body2, Dali::Vector3 localPivot); - - void MoveMouseBody(cpBody* mouseBody, Dali::Vector3 position); - - void MoveConstraint(cpConstraint* constraint, Dali::Vector3 newPosition); - - void ReleaseConstraint(cpConstraint* constraint); - - /** - * Ensure that the physics body does not go to sleep - * @param[in] body The physics body - * @return The old state - */ - int ActivateBody(cpBody* body); - - /** - * Restore the state of the physics body - * @param[in] body The physics body - * @param[in] oldState The previous state to restore - */ - void RestoreBodyState(cpBody* body, int oldState); - - /** - * Get the physics actor associated with the given body - * @param[in] body The physics body - * @return the associated physics actor - */ - PhysicsActor* GetPhysicsActor(cpBody* body); - - /** - * Set the highlight state of the actor associated with the physics body - * @param[in] body The physics body - * @param[in] highlight Whether to turn the highlight on or off. - */ - void HighlightBody(cpBody* body, bool highlight); - - -public: - std::map<uint32_t, PhysicsActor> mPhysicsActors; - bool mPhysicsIntegrateState{true}; - bool mPhysicsDebugState{true}; - - cpSpace* mSpace{nullptr}; - cpShape* mLeftBound{nullptr}; - cpShape* mRightBound{nullptr}; - cpShape* mTopBound{nullptr}; - cpShape* mBottomBound{nullptr}; - - Dali::Window mWindow; - Dali::Mutex mMutex; - - Dali::Actor mPhysicsRoot; - Dali::Vector2 mWorldOffset; - FrameCallback* mFrameCallback{nullptr}; -}; - -#endif // DALI_PHYSICS_DEMO_PHYSICS_IMPL_H diff --git a/examples/chipmunk-physics/shaders/rendering-textured-shape.frag b/examples/chipmunk-physics/shaders/rendering-textured-shape.frag deleted file mode 100644 index 42bf9473..00000000 --- a/examples/chipmunk-physics/shaders/rendering-textured-shape.frag +++ /dev/null @@ -1,29 +0,0 @@ -uniform sampler2D uTexture; -uniform mediump float uBrightness; -varying mediump vec2 vTexCoord; -varying mediump vec3 vIllumination; - -mediump vec3 redistribute_rgb(mediump vec3 color) -{ - mediump float threshold = 0.9999999; - mediump float m = max(max(color.r, color.g), color.b); - if(m <= threshold) - { - return color; - } - mediump float total = color.r + color.g + color.b; - if( total >= 3.0 * threshold) - { - return vec3(threshold); - } - mediump float x = (3.0 * threshold - total) / (3.0 * m - total); - mediump float gray = threshold - x * m; - return vec3(gray) + vec3(x)*color; -} - -void main() -{ - mediump vec4 texColor = texture2D( uTexture, vTexCoord ); - mediump vec3 pcol=texColor.rgb*(1.0+uBrightness); - gl_FragColor = vec4( redistribute_rgb(pcol), texColor.a); -}
\ No newline at end of file diff --git a/examples/chipmunk-physics/shaders/rendering-textured-shape.vert b/examples/chipmunk-physics/shaders/rendering-textured-shape.vert deleted file mode 100644 index 4c41f546..00000000 --- a/examples/chipmunk-physics/shaders/rendering-textured-shape.vert +++ /dev/null @@ -1,26 +0,0 @@ -attribute mediump vec3 aPosition; // DALi shader builtin -//attribute mediump vec2 aTexCoord; // DALi shader builtin -uniform mediump mat4 uMvpMatrix; // DALi shader builtin -uniform mediump mat4 uViewMatrix; // DALi shader builtin -uniform mediump mat4 uModelView; // DALi shader builtin -uniform mediump vec3 uSize; // DALi shader builtin -varying mediump vec3 vIllumination; -varying mediump vec2 vTexCoord; - -void main() -{ - mediump vec4 vertexPosition = vec4(aPosition, 1.0); - mediump vec3 normal = normalize(vertexPosition.xyz); - - vertexPosition.xyz *= uSize; - vec4 pos = uModelView * vertexPosition; - - vec4 lightPosition = vec4(400.0, 0.0, 100.0, 1.0); - vec4 mvLightPos = uViewMatrix * lightPosition; - vec3 vectorToLight = normalize(mvLightPos.xyz - pos.xyz); - float lightDiffuse = max(dot(vectorToLight, normal), 0.0); - - vIllumination = vec3(lightDiffuse * 0.5 + 0.5); - vTexCoord = aPosition.xy*2.0; - gl_Position = uMvpMatrix * vertexPosition; -}
\ No newline at end of file diff --git a/packaging/com.samsung.dali-demo.spec b/packaging/com.samsung.dali-demo.spec index 1fad55a0..c5ea126e 100755 --- a/packaging/com.samsung.dali-demo.spec +++ b/packaging/com.samsung.dali-demo.spec @@ -24,6 +24,7 @@ BuildRequires: pkgconfig(dali2-adaptor) BuildRequires: pkgconfig(dali2-toolkit) BuildRequires: pkgconfig(dali2-scene3d) BuildRequires: pkgconfig(dali2-physics-2d) +BuildRequires: pkgconfig(dali2-physics-3d) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(gles20) BuildRequires: pkgconfig(glesv2) |