Commit a9f29d47cf0ec4036df6b01ea7c739e1c65b23b8
Committed by
Gerrit Code Review
Merge "Fixed physics initialization in benchmark" into devel/master
Showing
1 changed file
with
8 additions
and
13 deletions
examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp
| @@ -376,6 +376,7 @@ public: | @@ -376,6 +376,7 @@ public: | ||
| 376 | 376 | ||
| 377 | PhysicsActor CreateBall(cpSpace* space) | 377 | PhysicsActor CreateBall(cpSpace* space) |
| 378 | { | 378 | { |
| 379 | + Window::WindowSize windowSize = mWindow.GetSize(); | ||
| 379 | const float BALL_MASS = 10.0f; | 380 | const float BALL_MASS = 10.0f; |
| 380 | const float BALL_RADIUS = BALL_SIZE.x * 0.25f; | 381 | const float BALL_RADIUS = BALL_SIZE.x * 0.25f; |
| 381 | const float BALL_ELASTICITY = 1.0f; | 382 | const float BALL_ELASTICITY = 1.0f; |
| @@ -384,7 +385,13 @@ public: | @@ -384,7 +385,13 @@ public: | ||
| 384 | auto ball = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]); | 385 | auto ball = Toolkit::ImageView::New(BALL_IMAGES[rand() % 4]); |
| 385 | ball[Actor::Property::NAME] = "Ball"; | 386 | ball[Actor::Property::NAME] = "Ball"; |
| 386 | ball[Actor::Property::SIZE] = BALL_SIZE * 0.5f; | 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 | cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero)); | 396 | cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero)); |
| 390 | cpShapeSetElasticity(shape, BALL_ELASTICITY); | 397 | cpShapeSetElasticity(shape, BALL_ELASTICITY); |
| @@ -392,18 +399,6 @@ public: | @@ -392,18 +399,6 @@ public: | ||
| 392 | 399 | ||
| 393 | PhysicsActor physicsBall = mPhysicsAdaptor.AddActorBody(ball, body); | 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 | return physicsBall; | 402 | return physicsBall; |
| 408 | } | 403 | } |
| 409 | 404 |