Win an Ipad
  Developer   Demos & Samples   Box2D Contacts

Box2D Contacts

Screenshot
Moscrif includes Box2D physical engine wll known also from many other game frameworks. In the virtual physics world is as well as in the real world is important to detect colisions between two bodies. This artice describes how to manages colisions in Moscrif.

Contacts

When two bodies collide together the onBeginContact and onEndContact is called. OnBeginContact is called when collision starts and onEndContact is called at the end of the collision. These functions are good opportunities to play some sounds or make other effects linked with body contact. However, the world is locked during the call-back, which means that bodies can’t be removed in call-back function (We suggest to remove them in onProcess function). Both functions have two parameters. First one is object, which called this function and second one is instance of PhysicsContact. Second parameter carries information about current contacts. Functions getBodyA and getBodyB returns bodies which collide. One instance of b2Contact class carries information about all actual contacts in the world. Function getNext is used to switch between the contacts.

this._world.onBeginContact = function(sender, contact)
{
    var current = contact;
    // check all contacts
    while (current) {
        // check if something collide with ground
        if (current.getBodyA() == this super._ground || current.getBodyB() == this super._ground)
            console<<"something hit the ground\n";;
            current = contact.getNext();
    }
}

Diferent behaviour of bodies

Box2d supports three diferent types of bodies (static, kinematic and dynamic). They behaviour different in the world (do not response to the force or do not move under the simulation). The different is also that not all types of bodies interacts together. More about how different types of bodies behaves in the world can be found in Box2dCreates sample.  And how diferent types behave when they colide together you can see in the next table. 

body type static dynamic kinematic
static      
dynamic      
kinematic      
   collide together
   do not collide together

Sometimes, we want to deny the collision for one or more bodies. If we set property active to false, no call-back functions are called, when this body collides with another body.

 Scene transitions   Game2D Animations 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.