Checking for collisions among bodies
To complete the prototype, we need to check whether the idol touches the ground. The simplest way, according to what you have learned about Box2D until now, is to continuously scan through idol collisions and check whether one of the bodies it collides with is the ground.
We need to add some lines to the update
function:
update:function(dt){ world.Step(dt,10,10); for (var b = world.GetBodyList(); b; b = b.GetNext()) { if (b.GetUserData() != null) { var mySprite = b.GetUserData().asset; mySprite.setPosition(b.GetPosition().x * worldScale, b.GetPosition().y * worldScale); mySprite.setRotation(-1 * cc.radiansToDegrees (b.GetAngle())); if(b.GetUserData().type=="totem"){ for(var c = b.GetContactList(); c; c = c.m_next){ if(c.other.GetUserData() && c.other.GetUserData().type=="ground"){ console.log("Oh no!!!!"); } } } } } }
In the way that we looped through bodies...