EPhysics - Shapes

The purpose of this example is to demonstrate the EPhysics Shapes usage - The code creates two EPhysics_Bodys using a custom shape.

shapes.png

For this example we'll have an EPhysics_World, and two basic EPhysics_Bodys.

The basic concepts like - defining an EPhysics_World, render geometry, physics limiting boundaries, add an EPhysics_Body, associate it to evas objects, change restitution, friction and impulse properties, were already covered in EPhysics - Bouncing Ball

Adding a Shape

Shapes are used to create bodies with shapes that differ from primitive ones, like box and circle.

A shape consists in a group of points, the vertices of the body to be created later with ephysics_body_shape_add(). You can also save and load it from a file.

We'll have to create a specific type of variable: EPhysics Shape

_world_populate(Test_Data *test_data)
{
EPhysics_Shape *pentagon_shape, *hexagon_shape;
EPhysics_Body *pentagon_body, *hexagon_body;
Evas_Object *pentagon, *hexagon;

First we add an image we want to add an EPhysics_Body to have a reference to after set the points (vertices).

pentagon = elm_image_add(test_data->win);
pentagon, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "pentagon");
evas_object_move(pentagon, WIDTH / 3, HEIGHT / 2 - 80);
evas_object_resize(pentagon, 70, 68);
evas_object_show(pentagon);

Here we create a new shape, note that the returned shape initially doesn't has points set, so its requiered to set vertices.

pentagon_shape = ephysics_shape_new();

Now we're setting the shape points (vertices) basing on the image that we added, two vertices form a link between them, an edge, so with some vertices is possible to create polygons, in this case a pentagon.

ephysics_shape_point_add(pentagon_shape, -1, -9/33., -1);
ephysics_shape_point_add(pentagon_shape, -1, -9/33., 1);
ephysics_shape_point_add(pentagon_shape, 0, -1, -1);
ephysics_shape_point_add(pentagon_shape, 0, -1, 1);
ephysics_shape_point_add(pentagon_shape, 1, -9/33., -1);
ephysics_shape_point_add(pentagon_shape, 1, -9/33., 1);
ephysics_shape_point_add(pentagon_shape, -21/35., 1, -1);
ephysics_shape_point_add(pentagon_shape, -21/35., 1, 1);
ephysics_shape_point_add(pentagon_shape, 21/35., 1, -1);
ephysics_shape_point_add(pentagon_shape, 21/35., 1, 1);

Here we create a new physics body using a custom shape. The center of mass will be the center of the shape. Its collision shape will be the convex shape that has all the points (and edges) we added to this shape before.

pentagon_body = ephysics_body_shape_add(test_data->world, pentagon_shape);
ephysics_body_evas_object_set(pentagon_body, pentagon, EINA_TRUE);
ephysics_body_restitution_set(pentagon_body, 1);

Here we just delete the custom shape (not the body) after used to create the wanted bodies, it's required to delete it. It won't be deleted automatically by ephysics at any point, even on shutdown.

ephysics_shape_del(pentagon_shape);

In the example we add another shape with the same process we just used, but with different image and points.

ephysics_shape_point_add(hexagon_shape, 0, 30, -10);
ephysics_shape_point_add(hexagon_shape, 0, 30, 10);
ephysics_shape_point_add(hexagon_shape, 18, 0, -10);
ephysics_shape_point_add(hexagon_shape, 18, 0, 10);
ephysics_shape_point_add(hexagon_shape, 52, 0, -10);
ephysics_shape_point_add(hexagon_shape, 52, 0, 10);
ephysics_shape_point_add(hexagon_shape, 70, 30, -10);
ephysics_shape_point_add(hexagon_shape, 70, 30, 10);
ephysics_shape_point_add(hexagon_shape, 52, 60, -10);
ephysics_shape_point_add(hexagon_shape, 52, 60, 10);
ephysics_shape_point_add(hexagon_shape, 18, 60, -10);
ephysics_shape_point_add(hexagon_shape, 18, 60, 10);

Here we finish the example. The full source code can be found at test_shapes.c.