EPhysics - Camera

The purpose of this example is to demonstrate the EPhysics_Camera usage.

The EPhysics_Camera facilitates the usage of scenarios bigger than the viewport, that's because the EPhysics handles the position of objects which has control.

camera.png

For this example we'll have an EPhysics_World, two distant EPhysics_Bodys, one with an impulse to collide each other and an EPhysics_Camera that follows the moving body using an animator.

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

Camera Data Struct

While in this example we'll be working with a struct to hold some objects in our code. For clarity sake we present you the struct declaration in the following block.

struct _Camera_Data {
Test_Data base;
Ecore_Animator *animator;
int old_x;
};

Adding a Camera

To move the camera in this example, we'll use an animator.

camera_data->animator = ecore_animator_add(_camera_move_cb, camera_data);

In the animators function, we'll have to create a specific type of variable: EPhysics Camera And also get the worlds rendered area width to define a limit to the camera.

_camera_move_cb(void *data)
{
Camera_Data *camera_data = data;
EPhysics_Camera *camera;
int x, y, w;
ephysics_world_render_geometry_get(camera_data->base.world,
NULL, NULL, NULL, &w, NULL, NULL);

Every world has a camera, so here we get this camera used by our EPhysics_World.

camera = ephysics_world_camera_get(camera_data->base.world);

Here we get the cameras position to after set the position based on previous.

Here we check if the camera reached the end of scenario (define the limit to the camera) then we stop the animator, else we move the camera + 2 pixel positions to the right.

if (x + w > WIDTH * 2)
{
camera_data->animator = NULL;
return EINA_FALSE;
}
x += 2;
}

Updating the floor

Here we'll use 2 floor images to give the impression of an infinite ground.

Calling ephysics_world_event_callback_add() will register a callback to a type of physics world event.

EPHYSICS_CALLBACK_WORLD_CAMERA_MOVED : called if the camera position changed on physics simulation tick.

In the function, we just get the cameras position to know how much the camera moved and move the same value to the floor passing it as delta_x to the function, note that we use an old_x variable to do this calculation.

_camera_moved_cb(void *data, EPhysics_World *world __UNUSED__, void *event_info)
{
EPhysics_Camera *camera = event_info;
Camera_Data *camera_data = data;
Evas_Object *floor_obj;
int x;
DBG("Camera moved");
ephysics_camera_position_get(camera, &x, NULL);
floor_obj = evas_object_data_get(camera_data->base.layout, "floor");
_update_floor(floor_obj, camera_data->old_x - x);
floor_obj = evas_object_data_get(camera_data->base.layout, "floor2");
_update_floor(floor_obj, camera_data->old_x - x);
camera_data->old_x = x;
}

Here we get the floors position and plus the delta_x value to move the floor in the same "velocity".

_update_floor(Evas_Object *floor_obj, Evas_Coord delta)
{
int fx, x, y;
evas_object_geometry_get(floor_obj, &x, &y, NULL, NULL);
fx = x + delta;

We use 2 floor images because whenever one exits the screen by the left side, another is being shown, when it happens the one which exit the screen is sent to the right side, entering into an infinite loop, giving the impression of an infinite ground image. Its important to note that we need to use the fx to don't gap the images.

if (fx < -FLOOR_WIDTH)
fx += 2 * FLOOR_WIDTH;
evas_object_move(floor_obj, fx, y);
}

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