Collisions in Fisica


Most video games are about objects bumping into each other. Fisica automatically resolves the reactions (bouncing) when two FBody collide but it’s crucial to detect when that happens in order to give the player some meaningful feedback.

These functions can be added to any fisica world to detect collisions:

//the functions contactStarted contactEnded
//are executed at the beginning and end of a contact
//that ALWAYS involves two bodies
void contactStarted(FContact c) {

  //c is the event object containing the information about the collision
  //for example the two bodies that can be retrieved using:
  //if the contact involves the triangle
  if (c.contains(triangle) )
    //this condition checks if the collision happened with a
    //static body (in this case the margins)
    //I have to check both bodies
    //if (c.getBody1().isStatic()== false && c.getBody2().isStatic()==false)
    //remove the triangle
  //the triangle disappears upon contact

//similar event
void contactEnded(FContact c) {

  //the circle shrinks
  //if the contact involves the circle and the box
  //alternate use of c.contains
  if(c.contains(circle, box) )
    //read the current size
    //note: get size only works for FCircle types of bodies
    float currentSize = circle.getSize();
    //set the size as current - 4 pixels
    float newSize = currentSize-4;

    //negative size may throw an error so I check it first
    if (newSize > 0)

This function provides an object type FContact which contains information about the collision between two, and only two, bodies, in particular the references of the bodies. This is how you get them:

FBody body1 = c.getBody1();
FBody body2 = c.getBody2();

Now I can manipulate body1 and body2 in the same way I manipulate all the FBody, changing parameters, destroying them etc.

Note: a body involved in a collision can arbitrarily be the first or the second. So you may need to do two checks.

The FContact method .contains can also be useful to verify certain conditions.

Is one of the bodies triangle which I declared as global using

FPoly triangle;
if (c.contains(triangle) )

Is this a contact that involves a body named “hero” and a “star”? See naming below.

if(c.contains("hero", "star"))

Full example here

Names and collisions

Often you need to discriminate collision between certain groups of bodies (e.g. bullets vs enemies) of which you don’t have direct references (i.e. they are not declared as named object globally like “FCircle circle;”).
Names come in very handy for this purpose. Also check accessing dynamically created bodies.

You can set a name at any point using setName, usually when you create and object:


And then check that name when the collision is detected:

if(body1.getName() == "star")

Full example here in which I want the stars but not the walls to disappear upon contact with the avatar.

You are now ready to create your first video game!
Start with Pong or Breakout.

Posted in

Post a comment