LibGDX Tutorial - A simple game physics lesson with box2D (Understanding 1)

We are now ready to go to the next level, which is the actual development of our game world. To do so we will be using box2D physics (libGDX uses box2D for the physics). Box2D has a very good and detailed documentation which you can easily find and read for more information.

The purpose of this tutorial though, is to understand some fundamentals about a physics world and then i will present to you the features of box2D that we will use.

A simple explanation of the physics of a game

Imagine our real world physics, for example we have velocity. We were the ones who defined velocity of an object as the rate of change of its position with respect to a frame of reference.

Our planet physics, is what we know best (at least we are trying to do our best to understand them) until now and a nice way to create a physics engine for a game is to try and imitate the physics of the real world. Why not trying to define the concept of velocity on a pc? It is not the simplest task to do and it takes strong understating of both physics and low level programming to do so.

I won't explain much more because i myself am not an expert like some people who have created game physics engines from scratch but to give you an idea, imagine that we have a little rectangle in our screen, made by green pixels. Now, we want to move this rectangle on the x - axis with constant velocity. Suppose that our screen has 1280 pixels width. If we move every pixel of this rectangle simultaneously by 1 pixel only on the x  - axis and we do that every 1 second and if we consider every pixel to be 1 meter then we could say that we defined a horizontal velocity of 1 m/s. 

A simple view on box2D

We are lucky enough because box2D has all the physics we will need for our 2D game. And the best of all is that we are the creators of our game world and thus we could even create a world with zero gravity and friction for example, we have many options!

We will be using Box2D's 2D geometrical shapes, like a rectangle, bodies and fixtures. There are 3 types of bodies;

  1. Static bodies; Like a ground that won't move.
  2. Kinematic bodies; Like a constantly moving enemy. 
  3. Dynamic bodies; Like our character who will be able to both move and stay still.

A fixture is needed to bind a shape to a body and add properties such as mass. There can also be collisions, for example between our character and enemy and we can decide what could happen upon this collision. 

Conclusion

It is always important to know how to handle physics engines such as box2D, but it is also important to have a simple understanding of what's happening behind the provided code. Who knows, maybe you could be a part of the creation of a successful engine in the future!

Next time, we will create our game world, starting with a ground and 2 walls.

Comments

  1. It looks like the Documentation link has changed. Here's the one I found: https://box2d.org/documentation/

    ReplyDelete
    Replies
    1. Thanks for the information! I've just changed it.

      Delete

Post a Comment