EPhysics - Shapes

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

EPhysics - Shapes

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

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;
EPhysics_Body
struct _EPhysics_Body EPhysics_Body
Body handle, represents an object on EPhysics world.
Definition: EPhysics.h:655
EPhysics_Shape
struct _EPhysics_Shape EPhysics_Shape
Shape handle, represents a shape to be used to create a body.
Definition: EPhysics.h:507
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

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);
elm_image_file_set(
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);
elm_image_add
Evas_Object * elm_image_add(Evas_Object *parent)
Add a new image to the parent.
Definition: efl_ui_image.c:2735
elm_image_file_set
Eina_Bool elm_image_file_set(Evas_Object *obj, const char *file, const char *group)
Set the file that will be used as the image's source.
Definition: efl_ui_image.c:2435
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1814
evas_object_move
EVAS_API void evas_object_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
Move the given Evas object to the given location inside its canvas' viewport.
Definition: evas_object_main.c:1171
evas_object_resize
EVAS_API void evas_object_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
Changes the size of the given Evas object.
Definition: evas_object_main.c:1236

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();
ephysics_shape_new
EAPI EPhysics_Shape * ephysics_shape_new(void)
Create a new shape.

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);
ephysics_shape_point_add
EAPI Eina_Bool ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y, double z)
Add a new point to the shape.

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);
ephysics_body_evas_object_set
EAPI void ephysics_body_evas_object_set(EPhysics_Body *body, Evas_Object *evas_obj, Eina_Bool use_obj_pos)
Set an evas object to a physics body.
ephysics_body_restitution_set
EAPI void ephysics_body_restitution_set(EPhysics_Body *body, double restitution)
Set body's coefficient of restitution.
ephysics_body_shape_add
EAPI EPhysics_Body * ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape)
Create a new physics body using a custom shape.
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539

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);
ephysics_shape_del
EAPI void ephysics_shape_del(EPhysics_Shape *shape)
Delete a 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.