Commit 29710d9332d9230d4d88c1bb6ef20f029e0af34a
1 parent
8144f125
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 <david.steele@samsung.com>
Showing
1 changed file
with
8 additions
and
13 deletions
examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp
| ... | ... | @@ -376,6 +376,7 @@ public: |
| 376 | 376 | |
| 377 | 377 | PhysicsActor CreateBall(cpSpace* space) |
| 378 | 378 | { |
| 379 | + Window::WindowSize windowSize = mWindow.GetSize(); | |
| 379 | 380 | const float BALL_MASS = 10.0f; |
| 380 | 381 | const float BALL_RADIUS = BALL_SIZE.x * 0.25f; |
| 381 | 382 | const float BALL_ELASTICITY = 1.0f; |
| ... | ... | @@ -384,7 +385,13 @@ public: |
| 384 | 385 | auto ball = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]); |
| 385 | 386 | ball[Actor::Property::NAME] = "Ball"; |
| 386 | 387 | ball[Actor::Property::SIZE] = BALL_SIZE * 0.5f; |
| 387 | - cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero))); | |
| 388 | + const float moment = cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero); | |
| 389 | + cpBody* body = cpBodyNew(BALL_MASS, moment); | |
| 390 | + const float fw = (windowSize.GetWidth() - BALL_RADIUS); | |
| 391 | + const float fh = (windowSize.GetHeight() - BALL_RADIUS); | |
| 392 | + cpBodySetPosition(body, cpv(Random::Range(0, fw), Random::Range(0, fh))); | |
| 393 | + cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0))); | |
| 394 | + cpSpaceAddBody(space, body); | |
| 388 | 395 | |
| 389 | 396 | cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero)); |
| 390 | 397 | cpShapeSetElasticity(shape, BALL_ELASTICITY); |
| ... | ... | @@ -392,18 +399,6 @@ public: |
| 392 | 399 | |
| 393 | 400 | PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body); |
| 394 | 401 | |
| 395 | - Window::WindowSize windowSize = mWindow.GetSize(); | |
| 396 | - | |
| 397 | - const float fw = 0.5f * (windowSize.GetWidth() - BALL_RADIUS); | |
| 398 | - const float fh = 0.5f * (windowSize.GetHeight() - BALL_RADIUS); | |
| 399 | - | |
| 400 | - // Example of setting physics property on update thread | |
| 401 | - physicsBall.AsyncSetPhysicsPosition(Vector3(Random::Range(-fw, fw), Random::Range(-fh, -fh * 0.5), 0.0f)); | |
| 402 | - | |
| 403 | - // Example of queuing a chipmunk method to run on the update thread | |
| 404 | - mPhysicsAdaptor.Queue([body]() { | |
| 405 | - cpBodySetVelocity(body, cpv(Random::Range(-100.0, 100.0), Random::Range(-100.0, 100.0))); | |
| 406 | - }); | |
| 407 | 402 | return physicsBall; |
| 408 | 403 | } |
| 409 | 404 | ... | ... |