EPhysics - Collision Filter

The purpose of this example is to demonstrate the EPhysics Collision Filter usage - The code adds four balls in 2 rows and 2 columns, two on each collision group, the collision only happens when the balls are in the same group (row),to make it easier, balls in the same group has the same color and size.

For this example we'll have an EPhysics_World and four basic EPhysics_Bodys, we'll apply an impulse on then and see what happens when they're in other collision group.

The basic concepts like - initializing 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 the balls

We'll use two arrays (color and size) to distinguish the groups.

_world_populate(Test_Data *test_data)
{
static const char *colors[] = {"blue-ball", "big-red-ball"};
static const int sizes[] = {54, 70};
EPhysics_Body *fall_body;
Evas_Object *sphere;
int i, column, row;
EPhysics_Body
struct _EPhysics_Body EPhysics_Body
Body handle, represents an object on EPhysics world.
Definition: EPhysics.h:655
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

The balls declaration was placed into a For loop, just to simplify the coding and divide them in two groups.

for (i = 0; i < 4; i++)
{
column = i % 2;
row = i / 2;
sphere = elm_image_add(test_data->win);
elm_image_file_set(
sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
colors[row]);
evas_object_move(sphere, (1 + column) * WIDTH / 4 +
(70 - sizes[row]) / 2,
100 + row * 60 + row / 2 * 20);
evas_object_resize(sphere, sizes[row], sizes[row]);
evas_object_show(sphere);
test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
fall_body = ephysics_body_sphere_add(test_data->world);
ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
ephysics_body_restitution_set(fall_body, 0.95);
ephysics_body_friction_set(fall_body, 0.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_friction_set
EAPI void ephysics_body_friction_set(EPhysics_Body *body, double friction)
Set body's friction.
ephysics_body_sphere_add
EAPI EPhysics_Body * ephysics_body_sphere_add(EPhysics_World *world)
Create a new sphere physics body.
eina_list_append
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition: eina_list.c:584
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
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

Note in this part we divide the balls in two groups by color (row).

ephysics_body_collision_group_add(fall_body, colors[row]);
ephysics_body_collision_group_add
EAPI Eina_Bool ephysics_body_collision_group_add(EPhysics_Body *body, const char *group)
Add a body to a given collision group.

The impulse will be applied in only 1 ball per group, in this case:

The 1st row 2nd column ball will be applied an impulse to the left (-300kg * p/s).

The 2nd row 1st column ball will be applied an impulse to the right (300kg * p/s).

And then saving the body into a list.

if (column + row == 1)
ephysics_body_central_impulse_apply(fall_body, 600 * row - 300, 0, 0);
test_data->bodies = eina_list_append(test_data->bodies, fall_body);
}
ephysics_body_central_impulse_apply
EAPI void ephysics_body_central_impulse_apply(EPhysics_Body *body, double x, double y, double z)
Apply an impulse on the center of a body.
}

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