Physics question
Elliot

I am making a simple physics-based game with the physics plugin for Wade. In this game you control a ball (it's a pinball sort of thing) and you have to collect coins.

 

I have set up my ball and coins as physics objects, and I have created an onCollision function for my coins. In this function, I simply remove the colliding coins from the scene and increment the score

coin.onCollision = function(eventData){    if (eventData.object.getName() == 'ball')    {        wade.removeSceneObject(coin);        score++;    }};

This works perfectly, but the ball is actually colliding with the coins. I mean, I don't want the trajectory of the ball to be affected by the collision with the coin objects, but this is what is happening at the moment. Is there any way to stop that?

All 2 Comments
Gio

You may want to use onPreCollision events rather than onCollision.

 

The difference is that onCollision gets called when two objects have collided and the collision has been resolved (in your case, after the velocity of the ball has been changed). Instead, onPreCollision gets called before the collision is resolved, and gives you chance to "cancel" the collision simply by returning true in your onPreCollision function. So exactly the same code just add "pre" in the function name and return true:

coin.onPreCollision = function(eventData){    if (eventData.object.getName() == 'ball')    {        wade.removeSceneObject(coin);        score++;        return true;    }};

You also have to remember to enable these preCollision events, because they are disabled by default. Somewhere in your init function add this:

wade.physics.enablePreCollisionEvents();
Elliot

Yeah, it's working!! :)

 

Thanks

Post a reply
Add Attachment
Submit Reply
Login to Reply