Search in sources :

Example 1 with GameLoop

use of de.gurkenlabs.litiengine.GameLoop in project litiengine by gurkenlabs.

the class PhysicsTests method testBasicCollisionDetection.

@Test
public void testBasicCollisionDetection() {
    try (GameLoop loop = new GameLoop(30)) {
        Creature ent = new Creature();
        ent.setSize(16, 16);
        ent.setCollision(true);
        ent.setCollisionBoxWidth(16);
        ent.setCollisionBoxHeight(16);
        ent.setLocation(10, 10);
        IPhysicsEngine engine = new PhysicsEngine();
        engine.add(ent);
        loop.start();
        loop.attach(() -> {
            assertFalse(engine.collides(9, 9));
            assertFalse(engine.collides(27, 27));
            assertTrue(engine.collides(10.00001, 10.00001));
            assertTrue(engine.collides(25.99999, 25.99999));
            Rectangle2D rect1 = new Rectangle2D.Double(0, 0, 10, 10);
            Rectangle2D rect2 = new Rectangle2D.Double(10, 10, 0, 0);
            Rectangle2D rect3 = new Rectangle2D.Double(10, 10, 1, 1);
            Rectangle2D rect4 = new Rectangle2D.Double(8, 8, 3, 3);
            Rectangle2D rect5 = new Rectangle2D.Double(25.99999, 10, 10, 20);
            assertFalse(engine.collides(rect1));
            assertFalse(engine.collides(rect2));
            assertTrue(engine.collides(rect3));
            assertTrue(engine.collides(rect4));
            assertTrue(engine.collides(rect5));
        });
    }
}
Also used : Creature(de.gurkenlabs.litiengine.entities.Creature) GameLoop(de.gurkenlabs.litiengine.GameLoop) Rectangle2D(java.awt.geom.Rectangle2D) Test(org.junit.jupiter.api.Test)

Aggregations

GameLoop (de.gurkenlabs.litiengine.GameLoop)1 Creature (de.gurkenlabs.litiengine.entities.Creature)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Test (org.junit.jupiter.api.Test)1