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 Callback

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;

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);

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

ephysics_body_collision_group_add(fall_body, colors[row]);

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);
}
}

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