benchmark-2d-physics-controller.cpp 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
/*
 * 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-physics/dali-physics.h>
#include <dali-toolkit/dali-toolkit.h>

#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
#include <dali/devel-api/adaptor-framework/key-devel.h>
#include <dali/devel-api/events/hit-test-algorithm.h>
#include <dali/integration-api/debug.h>

#include <chipmunk/chipmunk.h>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace Dali;
using namespace Dali::Toolkit::Physics;
using namespace Dali::ParentOrigin;
using namespace Dali::AnchorPoint;

#if defined(DEBUG_ENABLED)
Debug::Filter* gPhysicsDemo = Debug::Filter::New(Debug::Concise, false, "LOG_PHYSICS_EXAMPLE");
#endif

const float    MAX_ANIMATION_DURATION{60.0f};
const uint32_t ANIMATION_TIME{30000};
const uint32_t DEFAULT_BALL_COUNT{500};

#if defined(_ARCH_ARM_)
#define DEMO_ICON_DIR "/usr/share/icons"
#else
#define DEMO_ICON_DIR DEMO_IMAGE_DIR
#endif

const std::string BALL_IMAGES[] = {DEMO_IMAGE_DIR "/blocks-ball.png",
                                   DEMO_ICON_DIR "/dali-tests.png",
                                   DEMO_ICON_DIR "/dali-examples.png",
                                   DEMO_ICON_DIR "/com.samsung.dali-demo.png"};

const Vector2 BALL_SIZE{26.0f, 26.0f};

// Groups that can collide with each other:
const cpGroup BALL_GROUP{1 << 0};
const cpGroup BOUNDS_GROUP{1 << 1};

const cpBitmask COLLISION_MASK{0xfF};

const cpBitmask BALL_COLLIDES_WITH{BALL_GROUP | BOUNDS_GROUP};

enum BenchmarkType
{
  ANIMATION,
  PHYSICS_2D,
};

/**
 * @brief The physics demo using Chipmunk2D APIs.
 */
class Physics2dBenchmarkController : public ConnectionTracker
{
public:
  Physics2dBenchmarkController(Application& app, int numberOfBalls)
  : mApplication(app),
    mBallNumber(numberOfBalls)
  {
    app.InitSignal().Connect(this, &Physics2dBenchmarkController::OnInit);
    app.TerminateSignal().Connect(this, &Physics2dBenchmarkController::OnTerminate);
  }

  ~Physics2dBenchmarkController() = default;

  void OnInit(Application& application)
  {
    mWindow = application.GetWindow();
    mWindow.ResizeSignal().Connect(this, &Physics2dBenchmarkController::OnWindowResize);
    mWindow.KeyEventSignal().Connect(this, &Physics2dBenchmarkController::OnKeyEv);
    mWindow.GetRootLayer().TouchedSignal().Connect(this, &Physics2dBenchmarkController::OnTouched);
    mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY);

    mType = BenchmarkType::ANIMATION;

    CreateSimulation();

    mTimer = Timer::New(ANIMATION_TIME);
    mTimer.TickSignal().Connect(this, &Physics2dBenchmarkController::AnimationSimFinished);
    mTimer.Start();
  }

  void CreateSimulation()
  {
    switch(mType)
    {
      case BenchmarkType::ANIMATION:
      default:
      {
        CreateAnimationSimulation();
        break;
      }
      case BenchmarkType::PHYSICS_2D:
      {
        CreatePhysicsSimulation();
        break;
      }
    }
  }

  void CreateAnimationSimulation()
  {
    Window::WindowSize windowSize = mWindow.GetSize();
    mBallActors.resize(mBallNumber);
    mBallVelocity.resize(mBallNumber);

    mAnimationSimRootActor = Layer::New();
    mAnimationSimRootActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
    mAnimationSimRootActor[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
    mAnimationSimRootActor[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;

    mWindow.Add(mAnimationSimRootActor);
    std::ostringstream oss;
    oss << "Animation simulation of " << mBallNumber << " balls";
    auto title = Toolkit::TextLabel::New(oss.str());
    mAnimationSimRootActor.Add(title);
    title[Toolkit::TextLabel::Property::TEXT_COLOR]           = Color::WHITE;
    title[Actor::Property::PARENT_ORIGIN]                     = Dali::ParentOrigin::TOP_CENTER;
    title[Actor::Property::ANCHOR_POINT]                      = Dali::AnchorPoint::TOP_CENTER;
    title[Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT] = HorizontalAlignment::CENTER;
    title.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);

    const float margin(BALL_SIZE.width * 0.5f);

    for(int i = 0; i < mBallNumber; ++i)
    {
      Actor ball = mBallActors[i]          = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]);
      ball[Actor::Property::PARENT_ORIGIN] = Dali::ParentOrigin::CENTER;
      ball[Actor::Property::ANCHOR_POINT]  = Dali::AnchorPoint::CENTER;

      ball[Actor::Property::NAME]     = "Ball";
      ball[Actor::Property::SIZE]     = BALL_SIZE; // Halve the image size
      int width                       = windowSize.GetWidth() / 2;
      int height                      = windowSize.GetHeight() / 2;
      ball[Actor::Property::POSITION] = Vector3(Random::Range(margin - width, width - margin), Random::Range(margin - height, height - margin), 0.0f);
      ball.RegisterProperty("index", i);
      mAnimationSimRootActor.Add(ball);

      mBallVelocity[i] = Vector3(Random::Range(-25.0f, 25.0f), Random::Range(-25.0f, 25.0f), 0.0f);
      mBallVelocity[i].Normalize();
      mBallVelocity[i] = mBallVelocity[i] * Random::Range(15.0f, 50.0f);

      PropertyNotification leftNotify = mBallActors[i].AddPropertyNotification(Actor::Property::POSITION_X, LessThanCondition(margin - width));
      leftNotify.NotifySignal().Connect(this, &Physics2dBenchmarkController::OnHitLeftWall);

      PropertyNotification rightNotify = mBallActors[i].AddPropertyNotification(Actor::Property::POSITION_X, GreaterThanCondition(width - margin));
      rightNotify.NotifySignal().Connect(this, &Physics2dBenchmarkController::OnHitRightWall);

      PropertyNotification topNotify = mBallActors[i].AddPropertyNotification(Actor::Property::POSITION_Y, LessThanCondition(margin - height));
      topNotify.NotifySignal().Connect(this, &Physics2dBenchmarkController::OnHitTopWall);

      PropertyNotification bottomNotify = mBallActors[i].AddPropertyNotification(Actor::Property::POSITION_Y, GreaterThanCondition(height - margin));
      bottomNotify.NotifySignal().Connect(this, &Physics2dBenchmarkController::OnHitBottomWall);
    }

    title.RaiseToTop();
    ContinueAnimation();
  }

  bool AnimationSimFinished()
  {
    switch(mType)
    {
      case BenchmarkType::ANIMATION:
      default:
      {
        UnparentAndReset(mAnimationSimRootActor);
        mBallAnimation.Stop();
        mBallAnimation.Clear();

        mType = BenchmarkType::PHYSICS_2D;

        CreateSimulation();
        return true;
      }
      case BenchmarkType::PHYSICS_2D:
      {
        mApplication.Quit();
        break;
      }
    }
    return false;
  }

  void ContinueAnimation()
  {
    if(mBallAnimation)
    {
      mBallAnimation.Clear();
    }
    mBallAnimation = Animation::New(MAX_ANIMATION_DURATION);
    for(int i = 0; i < mBallNumber; ++i)
    {
      mBallAnimation.AnimateBy(Property(mBallActors[i], Actor::Property::POSITION), mBallVelocity[i] * MAX_ANIMATION_DURATION);
    }
    mBallAnimation.Play();
  }

  void OnHitLeftWall(PropertyNotification& source)
  {
    auto actor = Actor::DownCast(source.GetTarget());
    if(actor)
    {
      int index              = actor["index"];
      mBallVelocity[index].x = fabsf(mBallVelocity[index].x);
      ContinueAnimation();
    }
  }

  void OnHitRightWall(PropertyNotification& source)
  {
    auto actor = Actor::DownCast(source.GetTarget());
    if(actor)
    {
      int index              = actor["index"];
      mBallVelocity[index].x = -fabsf(mBallVelocity[index].x);
      ContinueAnimation();
    }
  }

  void OnHitBottomWall(PropertyNotification& source)
  {
    auto actor = Actor::DownCast(source.GetTarget());
    if(actor)
    {
      int index              = actor["index"];
      mBallVelocity[index].y = -fabsf(mBallVelocity[index].y);
      ContinueAnimation();
    }
  }

  void OnHitTopWall(PropertyNotification& source)
  {
    auto actor = Actor::DownCast(source.GetTarget());
    if(actor)
    {
      int index              = actor["index"];
      mBallVelocity[index].y = fabsf(mBallVelocity[index].y);
      ContinueAnimation();
    }
  }

  void CreatePhysicsSimulation()
  {
    Window::WindowSize windowSize = mWindow.GetSize();

    // 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();
    mWindow.Add(mPhysicsRoot);

    auto     scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
    cpSpace* space          = scopedAccessor->GetNative().Get<cpSpace*>();
    cpSpaceSetGravity(space, cpv(0, 0));

    CreateBounds(space, windowSize);

    for(int i = 0; i < mBallNumber; ++i)
    {
      mBalls.push_back(CreateBall(space));
    }

    // Process any async queued methods next frame
    mPhysicsAdaptor.CreateSyncPoint();

    std::ostringstream oss;
    oss << "Physics simulation of " << mBallNumber << " balls";
    auto title = Toolkit::TextLabel::New(oss.str());
    mPhysicsRoot.Add(title);
    title[Toolkit::TextLabel::Property::TEXT_COLOR]           = Color::WHITE;
    title[Actor::Property::PARENT_ORIGIN]                     = Dali::ParentOrigin::TOP_CENTER;
    title[Actor::Property::ANCHOR_POINT]                      = Dali::AnchorPoint::TOP_CENTER;
    title[Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT] = HorizontalAlignment::CENTER;
    title.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
    title.RaiseToTop();
  }

  PhysicsActor CreateBall(cpSpace* space)
  {
    const float BALL_MASS       = 10.0f;
    const float BALL_RADIUS     = BALL_SIZE.x * 0.25f;
    const float BALL_ELASTICITY = 1.0f;
    const float BALL_FRICTION   = 0.0f;

    auto ball                   = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]);
    ball[Actor::Property::NAME] = "Ball";
    ball[Actor::Property::SIZE] = BALL_SIZE * 0.5f;
    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);

    PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body);

    Window::WindowSize windowSize = mWindow.GetSize();

    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.5), 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)));
    });
    return physicsBall;
  }

  void CreateBounds(cpSpace* space, Window::WindowSize size)
  {
    // We're working in physics space here - coords are: origin: bottom left, +ve Y: up
    int32_t xBound = static_cast<int32_t>(static_cast<uint32_t>(size.GetWidth()));
    int32_t yBound = static_cast<int32_t>(static_cast<uint32_t>(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, cpShapeFilterNew(BOUNDS_GROUP, COLLISION_MASK, COLLISION_MASK));
    return shape;
  }

  void OnTerminate(Application& application)
  {
    UnparentAndReset(mAnimationSimRootActor);
    UnparentAndReset(mPhysicsRoot);
  }

  void OnWindowResize(Window window, Window::WindowSize newSize)
  {
    switch(mType)
    {
      case BenchmarkType::ANIMATION:
      default:
      {
        // TODO : Implement here if you want.
        break;
      }
      case BenchmarkType::PHYSICS_2D:
      {
        if(mPhysicsAdaptor)
        {
          auto     scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
          cpSpace* space          = scopedAccessor->GetNative().Get<cpSpace*>();

          CreateBounds(space, newSize);
        }
        break;
      }
    }
  }

  bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch)
  {
    mApplication.Quit();
    return false;
  }

  void OnKeyEv(const Dali::KeyEvent& event)
  {
    if(event.GetState() == KeyEvent::DOWN)
    {
      if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
      {
        mApplication.Quit();
      }
    }
  }

private:
  Application& mApplication;
  Window       mWindow;

  BenchmarkType mType{BenchmarkType::ANIMATION};

  PhysicsAdaptor            mPhysicsAdaptor;
  std::vector<PhysicsActor> mBalls;
  Matrix                    mPhysicsTransform;
  Actor                     mPhysicsRoot;
  Layer                     mPhysicsDebugLayer;
  Layer                     mAnimationSimRootActor;
  cpShape*                  mLeftBound{nullptr};
  cpShape*                  mRightBound{nullptr};
  cpShape*                  mTopBound{nullptr};
  cpShape*                  mBottomBound{nullptr};

  std::vector<Actor>   mBallActors;
  std::vector<Vector3> mBallVelocity;
  int                  mBallNumber;
  Animation            mBallAnimation;
  Timer                mTimer;
};

int DALI_EXPORT_API main(int argc, char** argv)
{
  setenv("DALI_FPS_TRACKING", "5", 1);
  Application application = Application::New(&argc, &argv);

  int numberOfBalls = DEFAULT_BALL_COUNT;
  if(argc > 1)
  {
    numberOfBalls = atoi(argv[1]);
  }

  Physics2dBenchmarkController controller(application, numberOfBalls);
  application.MainLoop();
  return 0;
}