Add platforms

To satisfy the goal of that project, we need to get some entities interacts with some platforms.

So, go and add some Entity with PhysicType.STATIC to the playground.

+-P1--------------------------------------+
|  00000                        ══════ ❤︎5|
+-+-------------------------------------+-+
P2|       +P5-----+                     P3|
| |       |       |                     | |
| |       +-------+                     | |
| |                                     | |
| |                                     | |
+P4-------------------------------------+-+
|                                         |
+-----------------------------------------+

Goto the PLayScene and add new entities :

public class PlayScene extends AbsytryactScene {
    //...
    public void create(KarmaPlatform app){
        //...
        createPlatforms(app);
        //...
    }
    //...

    private void createPlatforms(KarmaPlatform app) {
        KarmaPlatform.Entity platform1 = new KarmaPlatform.Entity("platform_01")
                .setPosition(100, 100)
                .setSize(100, 32)
                .setPhysicType(KarmaPlatform.PhysicType.STATIC)
                .setType(KarmaPlatform.EntityType.RECTANGLE)
                .setMass(4.0)
                .setBorderColor(Color.GRAY)
                .setBackgroundColor(Color.DARK_GRAY)
                .setPriority(10)
                .setMaterial(new KarmaPlatform.Material(1.0, 1.0, 0.1));
        addEntity(platform1);

        KarmaPlatform.Entity platform2 = new KarmaPlatform.Entity("platform_border_top")
                .setPosition(0, 0)
                .setSize((int) app.getWorld().getPlayArea().getWidth(), 16)
                .setPhysicType(KarmaPlatform.PhysicType.STATIC)
                .setType(KarmaPlatform.EntityType.RECTANGLE)
                .setMass(4.0)
                .setBorderColor(Color.GRAY)
                .setBackgroundColor(Color.DARK_GRAY)
                .setPriority(10)
                .setMaterial(new KarmaPlatform.Material(1.0, 1.0, 0.1));
        addEntity(platform2);

        KarmaPlatform.Entity platform3 = new KarmaPlatform.Entity("platform_border_bottom")
                .setPosition(0, getWorld().getPlayArea().getHeight())
                .setSize((int) getWorld().getPlayArea().getWidth(), 16)
                .setPhysicType(KarmaPlatform.PhysicType.STATIC)
                .setType(KarmaPlatform.EntityType.RECTANGLE)
                .setMass(4.0)
                .setBorderColor(Color.GRAY)
                .setBackgroundColor(Color.DARK_GRAY)
                .setPriority(10)
                .setMaterial(new KarmaPlatform.Material(1.0, 1.0, 0.35));
        addEntity(platform3);

        KarmaPlatform.Entity platform4 = new KarmaPlatform.Entity("platform_border_left")
                .setPosition(0, 16)
                .setSize(16, (int) getWorld().getPlayArea().getHeight())
                .setPhysicType(KarmaPlatform.PhysicType.STATIC)
                .setType(KarmaPlatform.EntityType.RECTANGLE)
                .setMass(4.0)
                .setBorderColor(Color.GRAY)
                .setBackgroundColor(Color.DARK_GRAY)
                .setPriority(10)
                .setMaterial(new KarmaPlatform.Material(1.0, 1.0, 0.35));
        addEntity(platform4);

        KarmaPlatform.Entity platform5 = new KarmaPlatform.Entity("platform_border_right")
                .setPosition((int) getWorld().getPlayArea().getWidth() - 16, 16)
                .setSize(16, (int) getWorld().getPlayArea().getHeight()-16)
                .setPhysicType(KarmaPlatform.PhysicType.STATIC)
                .setType(KarmaPlatform.EntityType.RECTANGLE)
                .setMass(4.0)
                .setBorderColor(Color.GRAY)
                .setBackgroundColor(Color.DARK_GRAY)
                .setPriority(10)
                .setMaterial(new KarmaPlatform.Material(1.0, 1.0, 0.35));
        addEntity(platform5);

    }
    //...
}

Here we now have a new bunch of platform entities to interact with:

Adding some platforms

figure 8.1 - Adding some platforms to the game

Enhancing the collision detection and resolution

Now we have platform, we need to know on wich side the of an entity the collision happened to run the correct behavior according to that collision.

Here is the right opportunity to create some CollisionEvent.

This CollisionEvent will provide :

  • srcCollision the entity colliding,
  • dstCollision the entity colliding with,
  • collisionNormal the normal vector for that collision,
  • penetrationDepth the penetration depth of this collision,
  • side:CollisionSide the side of the collision on the srcEntity.

So the corresponding class will be:

public static class CollisionEvent{
    private Entity srcCollision;
    private Entity dstCollision;
    private Vector2D collisionNormal;
    private double penetrationDepth;
    private CollisionSide side;
}

And the required ENUM for the side attribute:

public enum CollisionSide {
    TOP,
    BOTTOM,
    LEFT,
    RIGHT;
}

Modifying the existing algorithm