From d166edeac0a1d83aa1da6a2f22788a81ba6628ea Mon Sep 17 00:00:00 2001 From: David Steele Date: Wed, 6 Dec 2023 18:41:15 +0000 Subject: [PATCH] [Tizen] Fixed physics initialization in benchmark Physics bodies created in the same location creates too many collision arbiters when added to the space, and initialization takes too long Solution: ensure that they are positioned separately before adding to the space. Note, if there are so many particles that they overlap, then collision is noticeably slow. Change-Id: I88f8ce970bcceb90b213d0f9999400a4e2134944 Signed-off-by: David Steele --- .../benchmark-2d-physics-controller.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp b/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp index 331f1c3..70e316c 100644 --- a/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp +++ b/examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp @@ -376,6 +376,7 @@ public: PhysicsActor CreateBall(cpSpace* space) { + Window::WindowSize windowSize = mWindow.GetSize(); const float BALL_MASS = 10.0f; const float BALL_RADIUS = BALL_SIZE.x * 0.25f; const float BALL_ELASTICITY = 1.0f; @@ -384,7 +385,13 @@ public: 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))); + const float moment = cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero); + cpBody* body = cpBodyNew(BALL_MASS, moment); + const float fw = (windowSize.GetWidth() - BALL_RADIUS); + const float fh = (windowSize.GetHeight() - BALL_RADIUS); + cpBodySetPosition(body, cpv(Random::Range(0, fw), Random::Range(0, fh))); + cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0))); + cpSpaceAddBody(space, body); cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero)); cpShapeSetElasticity(shape, BALL_ELASTICITY); @@ -392,18 +399,6 @@ public: 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; } -- 2.7.4