Skip to content
Alexandre Poumaroux edited this page Dec 8, 2018 · 20 revisions

Welcome to the AdaPhysics2D wiki! This page describes how to use the engine easily.

Creating a world

First, you need to create a World object (in the Worlds package), and initialize it with its delta time dt. This is the time that will be used while integrating the forces and the velocities: the smaller the better, but keep in mind that a too small value will be very intensive. In the code below, you can as well edit the fps that stands for "frames per seconds", and it will automatically deduce the the dt to use.

with Worlds;

procedure Main is
    -- Create the World object
    W1 : Worlds.World;
    -- Frames per seconds desired
    fps : Float := 30;
    dt : Float := 1.0 / fps;
begin
    -- Initialize the world with its dt
    W1.Init(dt);
end Main;

Adding entities to the world

Now that the world is created, you need to add entities to it. Each type of entity has its own package. For now, there is only two: Circles and Rectangles. Each package comes with a Create member function returning an access to the entity. Here's how to create an entity of each type, with its parameters, and how to add it the world we created:

with Worlds;
with Rectangles;
with Circles;
with Materials;

procedure Main is
    W1 : Worlds.World;
    C1 : Circles.CircleAcc;
    R1 : Rectangles.RectangleAcc;
    fps : Float := 30;
    dt : Float := 1.0 / fps;
begin
    W1.Init(dt);
    
    -- Create a Circle
    C1 := Circles.Create(
                Pos => (0.0, 5.0),        -- the (x, y) coordinates of the center of the circle
                Vel => (0.0, 0.0),        -- the (x, y) initial speed of the circle
                Grav => (0.0, 9.81),      -- the (x, y) force field in which of the circle is
                Rad => 5.0                -- the radius of the circle
                Mat => Materials.RUBBER); -- the material: further details later

    -- Create a Rectangle
    R1 := Rectangles.Create(
                Pos => (0.0, 5.0),        -- the (x, y) coordinates of the bottom left corner of the rectangle
                Vel => (0.0, 0.0),        -- the (x, y) initial speed of the rectangle
                Grav => (0.0, 9.81),      -- the (x, y) force field in which of the rectangle is
                Dim => (5.0, 10.0),       -- the (width, height) of the rectangle
                Mat => Materials.RUBBER); -- the material: further details later

    -- Add the Circle and the Rectangle to the World we created
    W1.Add(C1);
    W1.Add(R1);

end Main;

Setting the world in motion and tracking entities

Now that the world is full of entities, you just need to use the Step primitive on the World object. It will then make the entities evolve for dt seconds. The primitive GetEntities will return an array, a EArray containing accesses to all the entities. You just then need to get their coordinates and render !

loop
    W1.Step; -- Make the World evolve for dt seconds
    declare
        Ents : Worlds.EArray := W1.GetEntities; -- Get the entities array
    begin
        for E of Ents loop
            Put(if E.EntityType = Entities.EntRectangle then "Re" else "Ci");
            Put_Line(+E.Coords); -- Displays the type and coordinates of each entity at that time 
        end loop;
    end;
    delay dt; -- Delay for dt seconds
end loop;

Note however that this is a naive implementation, especially the delay dt part. Indeed, on a real computer, the Step primitive will take some time to execute, and this need to be accounted for for a realistic simulation.

Materials

Each entity has a defined material type. Each material has specific values for the restitution factor (the bounciness), the density (from which, with the area, the mass is deduced), and for friction factors (static and dynamic). There are some predefined materials in the Materials package, but you can create your own ! A specific material is Materials.STATIC, which has a density of 0, which will result in an infinite mass: the entities with that material will never be affected by anything, so they'll make good borders or floors.

On a side note, you can make any material static with the SetStatic() function.

Clone this wiki locally