Turning Your EFL Application Physics Powered

EPhysics was released last july and we have introduced many improvements ever since. Its main purpose is to make it very easy to use physics in any EFL app.

While designing ephysics our great concern was to have as clean and as easy a user API as possible, one should be able to write the physics bits just in a few minutes with minimal instrusiveness.

In the process of writing a game the software developer has lots of challenges ahead, the physics pieces is just one of them, the physics interactions should not tie the whole game implementation but smothly integrate to other areas. For application’s graphical interfaces the API should be minimal having their default behaviours the most straitforward.

The EPhysics source package comes with lots of examples, you can use it as a very good starting point. Each example shows a different set of the API’s usage and their capabilities.

Having that said, I’m happy to present the results we’ve achieved by showing a very simple and brief ephysics example. In the following example we’ll have a micro bouncing ball example.
To install the required libraries you can use your operating system packages or install it from the source code. We need the efl core packages(ecore, evas, ecore-evas, eina) and the ephysics itself.

Skeleton Source Code

To present how easy it’s to power an efl application with EPhysics we see a bare and simple application then we see how it’s changed after injecting the EPhysics calls.

We first need to initialize/shutdown the required libraries, set our window title, size, background and so forth. The following source code is our skeleton – withouth the physics bits. The result is a 500×500 pixels window with a white background.

The Physics Bits

To introduce the physics support we first need to include the EPhysics header(EPhysics.h) and add calls to properly initialize and shut it down(respectively ephysics_init() and ephysics_shutdown()).

The resulting source code should look like the following:

World and Bodies

To fine tune the physics aspects some attributes(like gravity force, simulation time resolution and so on) are global to the simulation, these definitions are done in the world abstraction. One can have as many worlds as desired. The remaining definitions are done within bodies where the restitution, friction, mass and many other properties can be set.

Actually EPhysics supports 2 types of bodies and 6 different shapes, that’s rigid box, rigid cylinder, rigid sphere, soft box, soft cylinder and soft sphere. Where soft bodies are deformable variations of their rigid body counter parts.

In our even very minimal example we need a world and 2 bodies(the ball and the surface where it’s about to bounce over). So lets see it in terms of line of code.

Our final bouncing ball example has introduced – between variable declarations and functions call – 11 lines of code – if init/shutdown functions are considered. Lets examine what we have added to the source code:

We first create a new world. This world is used to hold the bodies in our example.

Then we define our render geometry, it defines the world dimentions, its position and size and depth. In the example our world is positioned at (0, 0, 0) coordinates and 500×500 pixels sized with 10 pixels depth. This coordinates are used to compute the bodies positions and some facilities like boundaries.

Finally we add the ball rigid body. This call will add a rigid body with an sphere shape to our world.

Prior to the world initialization we have added a snippet of code to create an Evas_Object which is the body’s visible part. The function ephysics_body_evas_object_set() is used to associate an evas object with a body, that means the rendering representation of a body is done by repositioning that Evas_Object(it may be an Elementary widget or any arbitrary evas object). The third parameter – use_obj_pos – tell EPhysics if the body should initially be positioned based on the evas object’s geometry(size and position) saving some complexity on positioning the physics body.

We finally set our ball’s restitution factor, this will define how the body is supposed to bounce after colliding against any other body.

Like previously said we must have 2 bodies so they can collide against each other. Our final body is meant to be the floor(or the bottom boundary). The boundary functions(i.e ephysics_body_top_boundary_add(), ephysics_body_right_boundary_add()ephysics_body_left_boundary_add() and ephysics_body_bottom_boundary_add()) were designed to ease the processes of limiting the area where a body can “fly” over, in the end these functions create a rigid body, correctly resize and move it to their right place – all based on the parameters set with ephysics_world_render_geometry_set().

Final Words

This has been a very minimal example, I suggest reading the EPhysics examples source code, they are very functional. There we demonstrate how to use events, transformation, force, velocity and so many other features, and also take some time to skim the libraries API documentation.

Got something to add? Find me on Twitter and Google+