2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-physics/dali-physics.h>
24 #include <dali-toolkit-test-suite-utils.h>
25 #include <toolkit-event-thread-callback.h>
27 #include <dali-toolkit/devel-api/controls/alignment/alignment.h>
28 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
29 #include <dali/devel-api/adaptor-framework/window-devel.h>
30 #include <dali/devel-api/events/hit-test-algorithm.h>
32 #include <chipmunk/chipmunk.h>
35 using namespace Dali::Toolkit::Physics;
37 void utc_dali_physics2d_startup(void)
39 test_return_value = TET_UNDEF;
42 void utc_dali_physics2d_cleanup(void)
44 test_return_value = TET_PASS;
47 cpBody* CreateBody(cpSpace* space)
49 const float BALL_MASS = 10.0f;
50 const float BALL_RADIUS = 26.0f;
51 const float BALL_ELASTICITY = 0.5f;
52 const float BALL_FRICTION = 0.5f;
54 cpBody* body = cpSpaceAddBody(space, cpBodyNew(BALL_MASS, cpMomentForCircle(BALL_MASS, 0.0f, BALL_RADIUS, cpvzero)));
56 cpShape* shape = cpSpaceAddShape(space, cpCircleShapeNew(body, BALL_RADIUS, cpvzero));
57 cpShapeSetElasticity(shape, BALL_ELASTICITY);
58 cpShapeSetFriction(shape, BALL_FRICTION);
63 int UtcDaliPhysics2DCreateAdaptorP1(void)
65 ToolkitTestApplication application;
67 Matrix transform(true);
68 Uint16Pair size(640, 480);
70 PhysicsAdaptor handle = PhysicsAdaptor::New(transform, size);
71 DALI_TEST_CHECK(handle);
76 int UtcDaliPhysics2DCreateAdaptorN1(void)
78 ToolkitTestApplication application;
80 PhysicsAdaptor handle;
81 DALI_TEST_CHECK(!handle);
86 int UtcDaliPhysics2DDowncastP1(void)
88 ToolkitTestApplication application;
90 Matrix transform(true);
91 Uint16Pair size(640, 480);
93 BaseHandle handle = PhysicsAdaptor::New(transform, size);
95 auto adaptor = PhysicsAdaptor::DownCast(handle);
96 DALI_TEST_CHECK(adaptor);
97 //Following only works if type is registered
98 //DALI_TEST_EQUALS("PhysicsAdaptor", adaptor.GetTypeName(), TEST_LOCATION);
102 int UtcDaliPhysics2DDowncastN1(void)
105 auto adaptor = PhysicsAdaptor::DownCast(handle);
106 DALI_TEST_CHECK(!adaptor);
108 DALI_TEST_CHECK(typeid(PhysicsAdaptor) == typeid(decltype(adaptor)));
112 int UtcDaliPhysics2DAdaptorMoveConstructor(void)
114 ToolkitTestApplication application;
115 tet_infoline("Testing the move constructor");
117 Matrix transform(true);
118 Uint16Pair size(640, 480);
119 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
120 DALI_TEST_CHECK(adaptor);
122 PhysicsAdaptor moved = std::move(adaptor);
123 DALI_TEST_CHECK(moved);
124 DALI_TEST_CHECK(!adaptor);
125 DALI_TEST_CHECK(moved != adaptor);
130 int UtcDaliPhysics2DAdaptorCopyConstructor(void)
132 ToolkitTestApplication application;
133 tet_infoline("Testing the move constructor");
135 Matrix transform(true);
136 Uint16Pair size(640, 480);
137 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
139 DALI_TEST_CHECK(adaptor);
141 PhysicsAdaptor altAdaptor = adaptor;
142 DALI_TEST_CHECK(altAdaptor);
143 DALI_TEST_CHECK(adaptor);
144 DALI_TEST_CHECK(altAdaptor == adaptor); // should point at same object
149 int UtcDaliPhysics2DAdaptorCopyAssign(void)
151 ToolkitTestApplication application;
152 tet_infoline("Testing the copy assign");
154 Matrix transform(true);
155 Uint16Pair size(640, 480);
156 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
157 DALI_TEST_CHECK(adaptor);
159 PhysicsAdaptor altAdaptor = adaptor;
160 DALI_TEST_CHECK(altAdaptor);
161 DALI_TEST_CHECK(adaptor);
162 DALI_TEST_CHECK(altAdaptor == adaptor); // should point at same object
167 int UtcDaliPhysics2DAdaptorMoveAssignment(void)
169 ToolkitTestApplication application;
170 tet_infoline("Testing the move constructor");
172 Matrix transform(true);
173 Uint16Pair size(640, 480);
174 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
175 DALI_TEST_CHECK(adaptor);
177 PhysicsAdaptor moved;
178 moved = std::move(adaptor);
179 DALI_TEST_CHECK(moved);
180 DALI_TEST_CHECK(!adaptor);
185 int UtcDaliPhysics2DSetTimestep(void)
187 ToolkitTestApplication application;
189 Matrix transform(true);
190 Uint16Pair size(640, 480);
192 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
193 adaptor.SetTimestep(1.0f / 60.0f);
195 DALI_TEST_EQUALS(adaptor.GetTimestep(), 1.0f / 60.0f, 0.0001f, TEST_LOCATION);
200 int UtcDaliPhysics2DGetTimestep(void)
202 ToolkitTestApplication application;
204 Matrix transform(true);
205 Uint16Pair size(640, 480);
207 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
208 adaptor.SetTimestep(1.0f / 60.0f);
209 float timestep = adaptor.GetTimestep();
210 float expected = 1.0f / 60.0f;
211 DALI_TEST_EQUALS(timestep, expected, 0.0001f, TEST_LOCATION);
213 adaptor.SetTimestep(1.0f / 120.0f);
214 timestep = adaptor.GetTimestep();
215 expected = 1.0f / 120.0f;
216 DALI_TEST_EQUALS(timestep, expected, 0.0001f, TEST_LOCATION);
221 int UtcDaliPhysics2DGetPhysicsAccessorP1(void)
223 ToolkitTestApplication application;
225 Matrix transform(true);
226 Uint16Pair size(640, 480);
228 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
229 PhysicsAdaptor::ScopedPhysicsAccessorPtr accessor = adaptor.GetPhysicsAccessor();
230 DALI_TEST_CHECK(accessor.get() != nullptr);
232 Dali::Any world = accessor->GetNative();
233 DALI_TEST_CHECK(!world.Empty());
238 int UtcDaliPhysics2DGetPhysicsAccessorN1(void)
240 ToolkitTestApplication application;
242 PhysicsAdaptor handle;
243 DALI_TEST_CHECK(!handle);
247 auto ptr = handle.GetPhysicsAccessor();
248 DALI_TEST_CHECK(ptr == nullptr);
250 tet_result(TET_FAIL);
252 catch(DaliException& e)
254 DALI_TEST_ASSERT(e, "Physics adaptor handle is empty", TEST_LOCATION);
260 int UtcDaliPhysics2DAdaptorGetRootActor(void)
262 tet_infoline("Test that the root actor can be retrieved");
264 ToolkitTestApplication application;
265 Matrix transform(false);
266 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
267 Uint16Pair size(640, 480);
268 auto scene = application.GetScene();
269 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
270 Actor rootActor = adaptor.GetRootActor();
271 scene.Add(rootActor);
273 DALI_TEST_CHECK(rootActor);
274 DALI_TEST_EQUALS(rootActor.GetProperty<Vector2>(Actor::Property::SIZE), Vector2(640.0f, 480.0f), 0.001f, TEST_LOCATION);
279 int UtcDaliPhysics2DAdaptorCreateDebugLayer(void)
281 ToolkitTestApplication application;
282 Matrix transform(true);
283 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
284 Uint16Pair size(640, 480);
285 auto scene = application.GetScene();
287 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
288 Actor rootActor = adaptor.GetRootActor();
289 scene.Add(rootActor);
290 Window window = DevelWindow::Get(rootActor);
292 Layer layer = adaptor.CreateDebugLayer(window);
293 DALI_TEST_CHECK(!layer);
298 int UtcDaliPhysics2DAdaptorTranslateToPhysicsSpace1(void)
300 ToolkitTestApplication application;
301 Matrix transform(false);
302 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
303 Uint16Pair size(640, 480);
304 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
306 Vector3 a(30, 20, 10);
307 Vector3 expected(60, 40, 10);
308 DALI_TEST_EQUALS(adaptor.TranslateToPhysicsSpace(a), expected, 0.0001f, TEST_LOCATION);
313 int UtcDaliPhysics2DAdaptorTranslateToPhysicsSpace2(void)
315 ToolkitTestApplication application;
316 Matrix transform(false);
317 tet_infoline("Test that using an alternative scale doesn't change rotation");
318 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
319 Uint16Pair size(640, 480);
320 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
322 // Rotation shouldn't change under this scale
323 Quaternion q(Degree(30.0f), Vector3::XAXIS);
324 Quaternion expected(Degree(30.0f), Vector3::XAXIS);
325 DALI_TEST_EQUALS(adaptor.TranslateToPhysicsSpace(q), expected, 0.0001f, TEST_LOCATION);
330 int UtcDaliPhysics2DAdaptorTranslateToPhysicsSpace3(void)
332 ToolkitTestApplication application;
333 Matrix transform(false);
334 tet_infoline("Test that using an inverted Y scale inverts rotation");
336 transform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f));
337 Uint16Pair size(640, 480);
338 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
340 Quaternion q(Degree(30.0f), Vector3::ZAXIS);
341 Quaternion expected(Degree(-30.0f), Vector3::ZAXIS);
343 DALI_TEST_EQUALS(adaptor.TranslateToPhysicsSpace(q), expected, 0.0001f, TEST_LOCATION);
348 int UtcDaliPhysics2DAdaptorTranslateFromPhysicsSpace1(void)
350 ToolkitTestApplication application;
351 Matrix transform(false);
352 tet_infoline("Test that using a double scale halves position");
354 transform.SetIdentityAndScale(Vector3(2.0f, -2.0f, 1.0f));
355 Uint16Pair size(640, 480);
356 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
358 Vector3 position(20.0f, 20.0f, 0.0f);
359 Vector3 expected(10.0f, -10.0f, 0.0f);
361 DALI_TEST_EQUALS(adaptor.TranslateFromPhysicsSpace(position), expected, 0.0001f, TEST_LOCATION);
366 int UtcDaliPhysics2DAdaptorConvertVectorToPhysicsSpace01(void)
368 ToolkitTestApplication application;
369 Matrix transform(false);
370 tet_infoline("Test that using a translation does not translate vector");
372 transform.SetIdentityAndScale(Vector3(1.0f, 1.0f, 1.0f));
373 transform.SetTranslation(Vector3(0.0f, 100.0f, 0.0f));
374 Uint16Pair size(640, 480);
375 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
376 Vector3 vector(20.0f, 20.0f, 0.0f);
377 DALI_TEST_EQUALS(adaptor.ConvertVectorToPhysicsSpace(vector), vector, 0.0001f, TEST_LOCATION);
382 int UtcDaliPhysics2DAdaptorConvertVectorToPhysicsSpace02(void)
384 ToolkitTestApplication application;
385 Matrix transform(false);
386 tet_infoline("Test that using a translation with inverse Y does not translate vector");
388 transform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f));
389 transform.SetTranslation(Vector3(0.0f, 100.0f, 0.0f));
390 Uint16Pair size(640, 480);
391 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
392 Vector3 vector(20.0f, 20.0f, 0.0f);
393 Vector3 expected(20.0f, -20.0f, 0.0f);
394 DALI_TEST_EQUALS(adaptor.ConvertVectorToPhysicsSpace(vector), expected, 0.0001f, TEST_LOCATION);
399 int UtcDaliPhysics2DAdaptorConvertVectorFromPhysicsSpace01(void)
401 ToolkitTestApplication application;
402 Matrix transform(false);
403 tet_infoline("Test that using a translation does not translate vector");
405 transform.SetIdentityAndScale(Vector3(1.0f, 1.0f, 1.0f));
406 transform.SetTranslation(Vector3(0.0f, 100.0f, 0.0f));
407 Uint16Pair size(640, 480);
408 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
409 Vector3 vector(20.0f, 20.0f, 0.0f);
410 DALI_TEST_EQUALS(adaptor.ConvertVectorFromPhysicsSpace(vector), vector, 0.0001f, TEST_LOCATION);
415 int UtcDaliPhysics2DAdaptorConvertVectorFromPhysicsSpace02(void)
417 ToolkitTestApplication application;
418 Matrix transform(false);
419 tet_infoline("Test that using a translation with inverse Y does not translate vector");
421 transform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f));
422 transform.SetTranslation(Vector3(0.0f, 100.0f, 0.0f));
423 Uint16Pair size(640, 480);
424 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
425 Vector3 vector(20.0f, 20.0f, 0.0f);
426 Vector3 expected(20.0f, -20.0f, 0.0f);
427 DALI_TEST_EQUALS(adaptor.ConvertVectorFromPhysicsSpace(vector), expected, 0.0001f, TEST_LOCATION);
432 int UtcDaliPhysics2DAdaptorSetTransformAndSize(void)
434 ToolkitTestApplication application;
435 Matrix transform(false);
436 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
437 Uint16Pair size(640, 480);
438 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
440 Vector3 a(30, 20, 10);
441 Vector3 expected(60, 40, 10);
442 DALI_TEST_EQUALS(adaptor.TranslateToPhysicsSpace(a), expected, 0.0001f, TEST_LOCATION);
444 transform.SetIdentityAndScale(Vector3(1.0f, -1.0f, 1.0f));
445 transform.SetTranslation(Vector3(0.0f, 100.0f, 0.0f));
446 adaptor.SetTransformAndSize(transform, size);
448 Vector3 expect2(30, 80, 10);
449 DALI_TEST_EQUALS(adaptor.TranslateToPhysicsSpace(a), expect2, 0.0001f, TEST_LOCATION);
454 int UtcDaliPhysics2DAdaptorSetIntegrationState(void)
456 tet_infoline("Test that changing the integration state is reflected");
458 ToolkitTestApplication application;
459 Matrix transform(false);
460 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
461 Uint16Pair size(640, 480);
462 auto scene = application.GetScene();
463 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
464 Actor rootActor = adaptor.GetRootActor();
465 scene.Add(rootActor);
467 DALI_TEST_CHECK(adaptor.GetIntegrationState() == PhysicsAdaptor::IntegrationState::ON);
469 adaptor.SetIntegrationState(PhysicsAdaptor::IntegrationState::OFF);
470 DALI_TEST_CHECK(adaptor.GetIntegrationState() == PhysicsAdaptor::IntegrationState::OFF);
475 int UtcDaliPhysics2DAdaptorGetIntegrationState(void)
477 tet_infoline("Test that changing the integration state is reflected");
479 ToolkitTestApplication application;
480 Matrix transform(false);
481 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
482 Uint16Pair size(640, 480);
483 auto scene = application.GetScene();
484 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
485 Actor rootActor = adaptor.GetRootActor();
486 scene.Add(rootActor);
488 adaptor.SetIntegrationState(PhysicsAdaptor::IntegrationState::OFF);
489 DALI_TEST_CHECK(adaptor.GetIntegrationState() == PhysicsAdaptor::IntegrationState::OFF);
491 adaptor.SetIntegrationState(PhysicsAdaptor::IntegrationState::ON);
492 DALI_TEST_CHECK(adaptor.GetIntegrationState() == PhysicsAdaptor::IntegrationState::ON);
494 // Can't test actual integration step runs without adding actors - see utc-Dali-PhysicsActor.cpp.
498 int UtcDaliPhysics2DAdaptorSetDebugState(void)
500 tet_infoline("Test that changing the debug state is reflected");
502 ToolkitTestApplication application;
503 Matrix transform(false);
504 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
505 Uint16Pair size(640, 480);
506 auto scene = application.GetScene();
507 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
508 Actor rootActor = adaptor.GetRootActor();
509 scene.Add(rootActor);
511 DALI_TEST_CHECK(adaptor.GetDebugState() == PhysicsAdaptor::DebugState::OFF);
513 adaptor.SetDebugState(PhysicsAdaptor::DebugState::ON);
514 DALI_TEST_CHECK(adaptor.GetDebugState() == PhysicsAdaptor::DebugState::ON);
516 adaptor.SetDebugState(PhysicsAdaptor::DebugState::OFF);
517 DALI_TEST_CHECK(adaptor.GetDebugState() == PhysicsAdaptor::DebugState::OFF);
522 int UtcDaliPhysics2DAdaptorGetDebugState(void)
524 tet_infoline("Test that changing the debug state is reflected");
526 ToolkitTestApplication application;
527 Matrix transform(false);
528 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
529 Uint16Pair size(640, 480);
530 auto scene = application.GetScene();
531 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
532 Actor rootActor = adaptor.GetRootActor();
533 scene.Add(rootActor);
535 adaptor.SetDebugState(PhysicsAdaptor::DebugState::OFF);
536 DALI_TEST_CHECK(adaptor.GetDebugState() == PhysicsAdaptor::DebugState::OFF);
538 adaptor.SetDebugState(PhysicsAdaptor::DebugState::ON);
539 DALI_TEST_CHECK(adaptor.GetDebugState() == PhysicsAdaptor::DebugState::ON);
541 // Can't test actual debug step runs without adding actors - see utc-Dali-PhysicsActor.cpp.
545 int UtcDaliPhysics2DAdaptorAddActorBody(void)
547 tet_infoline("Test that an actor/body pair can be added");
549 ToolkitTestApplication application;
550 Matrix transform(false);
551 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
552 Uint16Pair size(640, 480);
553 auto scene = application.GetScene();
554 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
555 Actor rootActor = adaptor.GetRootActor();
556 scene.Add(rootActor);
558 auto accessor = adaptor.GetPhysicsAccessor();
559 auto space = accessor->GetNative().Get<cpSpace*>();
561 cpBody* body = CreateBody(space);
562 Dali::Actor ballActor = Toolkit::ImageView::New("gallery-small-1.jpg");
563 auto physicsActor = adaptor.AddActorBody(ballActor, body);
565 DALI_TEST_CHECK(physicsActor);
566 int id = ballActor[Actor::Property::ID];
568 DALI_TEST_EQUALS(physicsActor.GetId(), id, TEST_LOCATION);
569 DALI_TEST_EQUALS(physicsActor.GetBody().Get<cpBody*>(), body, TEST_LOCATION);
574 void removeShape(cpBody* body, cpShape* shape, void* data)
576 cpSpace* space = static_cast<cpSpace*>(data);
577 cpSpaceRemoveShape(space, shape);
578 cpShapeSetBody(shape, nullptr);
582 int UtcDaliPhysics2DAdaptorRemoveActorBodyP01(void)
584 tet_infoline("Test that an actor/body pair can be removed");
586 ToolkitTestApplication application;
587 Matrix transform(false);
588 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 2.0f));
589 Uint16Pair size(640, 480);
590 auto scene = application.GetScene();
591 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
592 Actor rootActor = adaptor.GetRootActor();
593 scene.Add(rootActor);
597 auto accessor = adaptor.GetPhysicsAccessor();
598 auto space = accessor->GetNative().Get<cpSpace*>();
600 body = CreateBody(space);
602 Dali::Actor ballActor = Toolkit::ImageView::New("gallery-small-1.jpg");
603 auto physicsActor = adaptor.AddActorBody(ballActor, body);
605 application.SendNotification();
606 application.Render();
607 application.SendNotification();
608 application.Render();
610 adaptor.RemoveActorBody(physicsActor);
611 DALI_TEST_CHECK(!ballActor.GetParent());
614 auto accessor = adaptor.GetPhysicsAccessor();
615 auto space = accessor->GetNative().Get<cpSpace*>();
619 cpBodyEachShape(body, removeShape, space);
620 cpSpaceRemoveBody(space, body);
621 tet_result(TET_PASS);
623 catch(std::exception& e)
625 tet_result(TET_FAIL);
632 int UtcDaliPhysics2DAdaptorRemoveActorBodyN01(void)
634 ToolkitTestApplication application;
635 Matrix transform(false);
636 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 2.0f));
637 Uint16Pair size(640, 480);
638 auto scene = application.GetScene();
639 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
640 Actor rootActor = adaptor.GetRootActor();
641 scene.Add(rootActor);
643 tet_infoline("Test that removing a physics actor that hasn't been created with AddActorBody does nothing");
644 Dali::Actor actor = Dali::Actor::New();
645 cpBody* body = cpBodyNew(1.0f, 1.0f);
646 PhysicsActor physicsActor = PhysicsActor::New(actor, body, adaptor);
650 adaptor.RemoveActorBody(physicsActor);
651 tet_result(TET_PASS);
653 catch(std::exception& e)
655 // Should fail silently, without exception!
656 tet_result(TET_FAIL);
662 int UtcDaliPhysics2DAdaptorRemoveActorBodyN02(void)
664 tet_infoline("Test that an empty actor/body pair doesn't break adaptor");
666 ToolkitTestApplication application;
667 Matrix transform(false);
668 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 2.0f));
669 Uint16Pair size(640, 480);
670 auto scene = application.GetScene();
671 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
672 Actor rootActor = adaptor.GetRootActor();
673 scene.Add(rootActor);
675 PhysicsActor physicsActor;
678 adaptor.RemoveActorBody(physicsActor);
679 tet_result(TET_FAIL);
681 catch(DaliException& e)
683 DALI_TEST_ASSERT(e, "Physics actor handle is empty", TEST_LOCATION);
689 int UtcDaliPhysics2DAdaptorGetPhysicsActor(void)
691 tet_infoline("Test that an actor/body pair can be retrieved");
693 ToolkitTestApplication application;
694 Matrix transform(false);
695 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
696 Uint16Pair size(640, 480);
697 auto scene = application.GetScene();
698 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
699 Actor rootActor = adaptor.GetRootActor();
700 scene.Add(rootActor);
702 auto accessor = adaptor.GetPhysicsAccessor();
703 auto space = accessor->GetNative().Get<cpSpace*>();
705 cpBody* body = CreateBody(space);
706 Dali::Actor ballActor = Toolkit::ImageView::New("gallery-small-1.jpg");
707 auto physicsActor = adaptor.AddActorBody(ballActor, body);
709 DALI_TEST_CHECK(physicsActor);
711 PhysicsActor testActor = adaptor.GetPhysicsActor(body);
712 DALI_TEST_CHECK(testActor);
713 DALI_TEST_CHECK(physicsActor == testActor);
718 int UtcDaliPhysics2DAdaptorBuildPickingRay(void)
720 tet_infoline("Test that picking ray converts screen coords");
722 ToolkitTestApplication application;
723 Matrix transform(false);
724 transform.SetIdentityAndScale(Vector3(1.0f, 1.0f, 1.0f));
725 Uint16Pair size(TestApplication::DEFAULT_SURFACE_WIDTH, TestApplication::DEFAULT_SURFACE_HEIGHT);
726 const Vector2 center(TestApplication::DEFAULT_SURFACE_WIDTH * 0.5f, TestApplication::DEFAULT_SURFACE_HEIGHT * 0.5f);
727 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
728 Actor rootActor = adaptor.GetRootActor();
729 auto scene = application.GetScene();
730 scene.Add(rootActor);
733 adaptor.BuildPickingRay(Vector3(center), Vector3(center), from, to); // Hit test centre of screen
734 Vector3 physCenter = adaptor.TranslateToPhysicsSpace(Vector3(center));
735 DALI_TEST_EQUALS(from, physCenter, 0.001f, TEST_LOCATION);
740 int UtcDaliPhysics2DAdaptorProjectPoint(void)
742 tet_infoline("Test that a point is projected into physics space");
744 ToolkitTestApplication application;
745 Matrix transform(false);
746 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
747 Uint16Pair size(640, 480);
748 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
750 Vector3 projectedPoint = adaptor.ProjectPoint(Vector3(), -Vector3::ZAXIS, 200);
752 DALI_TEST_EQUALS(projectedPoint, Vector3(0.0f, 0.0f, 0.0f), 0.001f, TEST_LOCATION);
757 int UtcDaliPhysics2DAdaptorQueue(void)
759 tet_infoline("Test that Queue and CreateSyncPoint both work");
761 ToolkitTestApplication application;
762 Matrix transform(false);
763 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
764 Uint16Pair size(640, 480);
765 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
766 Actor rootActor = adaptor.GetRootActor();
767 auto scene = application.GetScene();
768 scene.Add(rootActor);
770 cpBody* body{nullptr};
772 auto accessor = adaptor.GetPhysicsAccessor();
773 auto space = accessor->GetNative().Get<cpSpace*>();
774 body = CreateBody(space);
775 Dali::Actor ballActor = Toolkit::ImageView::New("gallery-small-1.jpg");
776 auto physicsActor = adaptor.AddActorBody(ballActor, body);
779 tet_infoline("Test that Queue works without accessor");
780 adaptor.Queue([body]() {
781 cpBodySetPosition(body, cpv(100.0f, 20.0f));
783 adaptor.CreateSyncPoint();
785 application.SendNotification();
786 application.Render();
787 // Should trigger an Update
790 auto accessor = adaptor.GetPhysicsAccessor();
791 cpVect origin = cpBodyGetPosition(body);
793 DALI_TEST_EQUALS(origin.x, cpFloat(100.0f), 0.001f, TEST_LOCATION);
794 DALI_TEST_EQUALS(origin.y, cpFloat(20.0f), 0.001f, TEST_LOCATION);
800 int UtcDaliPhysics2DAdaptorCreateSyncPoint(void)
802 tet_infoline("Test that a delayed CreateSyncPoint delays update");
804 ToolkitTestApplication application;
805 Matrix transform(false);
806 transform.SetIdentityAndScale(Vector3(2.0f, 2.0f, 1.0f));
807 Uint16Pair size(640, 480);
808 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
809 Actor rootActor = adaptor.GetRootActor();
810 auto scene = application.GetScene();
811 scene.Add(rootActor);
813 cpBody* body{nullptr};
815 auto accessor = adaptor.GetPhysicsAccessor();
816 auto space = accessor->GetNative().Get<cpSpace*>();
817 body = CreateBody(space);
818 Dali::Actor ballActor = Toolkit::ImageView::New("gallery-small-1.jpg");
819 auto physicsActor = adaptor.AddActorBody(ballActor, body);
821 tet_infoline("Test that Queue works with accessor");
822 adaptor.Queue([body]() {
823 cpBodySetPosition(body, cpv(100.0f, 20.0f));
827 // Should trigger an Update without processing queue
828 application.SendNotification();
829 application.Render();
832 auto accessor = adaptor.GetPhysicsAccessor();
834 cpVect origin = cpBodyGetPosition(body);
835 DALI_TEST_EQUALS(origin.x, cpFloat(0.0f), 0.01f, TEST_LOCATION);
836 DALI_TEST_EQUALS(origin.y, cpFloat(0.0f), 0.01f, TEST_LOCATION);
839 // Should now execute queue
840 adaptor.CreateSyncPoint();
841 application.SendNotification();
842 application.Render();
845 auto accessor = adaptor.GetPhysicsAccessor();
847 cpVect origin = cpBodyGetPosition(body);
848 DALI_TEST_EQUALS(origin.x, cpFloat(100.0f), 0.01f, TEST_LOCATION);
849 DALI_TEST_EQUALS(origin.y, cpFloat(20.0f), 0.01f, TEST_LOCATION);
855 int UtcDaliPhysics2DAdaptorHitTestP(void)
857 tet_infoline("Test that hit testing finds a body");
859 ToolkitTestApplication application;
860 Matrix transform(false);
861 transform.SetIdentityAndScale(Vector3(1.0f, 1.0f, 1.0f));
862 Uint16Pair size(TestApplication::DEFAULT_SURFACE_WIDTH, TestApplication::DEFAULT_SURFACE_HEIGHT);
863 const Vector2 center(TestApplication::DEFAULT_SURFACE_WIDTH * 0.5f, TestApplication::DEFAULT_SURFACE_HEIGHT * 0.5f);
864 PhysicsAdaptor adaptor = PhysicsAdaptor::New(transform, size);
865 Actor rootActor = adaptor.GetRootActor();
866 auto scene = application.GetScene();
867 scene.Add(rootActor);
870 auto accessor = adaptor.GetPhysicsAccessor(); // Prevent integration
871 auto space = accessor->GetNative().Get<cpSpace*>();
872 Dali::Actor ballActor = Toolkit::ImageView::New(TEST_RESOURCE_DIR "/gallery-small-1.jpg");
873 cpBody* body = CreateBody(space);
874 cpBodySetPosition(body, cpv(center.x, center.y));
876 ballActor[Actor::Property::PARENT_ORIGIN] = ParentOrigin::CENTER;
877 ballActor[Actor::Property::ANCHOR_POINT] = AnchorPoint::CENTER;
879 auto physicsActor = adaptor.AddActorBody(ballActor, body);
881 Test::WaitForEventThreadTrigger(1);
883 // Should trigger an Update without processing queue
884 application.SendNotification();
885 application.Render();
888 adaptor.BuildPickingRay(Vector3(center), Vector3(center), from, to); // Hit test centre of screen
891 auto accessor = adaptor.GetPhysicsAccessor();
893 float distanceFromCamera;
894 auto body = accessor->HitTest(from, from, localPivot, distanceFromCamera);
896 DALI_TEST_CHECK(!body.Empty());