Need Help with Physics and collision

Hello,

I’m a new to corona sdk and basically it is my first ever app I’m working. I am working on a game app. Things fall from the top and we have to eat/catch them. For each item caught I’m adding 10 points to the score.

When the collision is detected this is what I’m doing:

[play sound code]

[score adding code here]

event.other.alpha =0

transition.to ( event.other, { time=1,y=screenTop - 5000,x=screenLeft - 2000,} )

so basically which ever object hits the character first fades out so it gives a feeling of being eaten and then that object flies of the screen somewhere :stuck_out_tongue:

everything is working fine, except if I am standing right under the object sometime the colloision is detected twice hence 20 points are added and sound is played twice and even also when the character is in motion sometimes it detects the collision twice and adds 20 score.

What is the best option for me here? Any help would be appreciated. Thanks!

Hi @amdriod95,

This could be caused by various things. Are you not isolating the collision to the “began” phase? Are any of your objects multi-element bodies? Please see the following guide if you haven’t read it already:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Take care,

Brent

@Brent Thanks for you reply. Yes the began phase is added although I didnt use the ended phase. I found a fix around it by adding:

event.other:removeSelf()  – So it removes whichever fruits collides with the character and its working fine.

But now the problem is with the other harful object which I want to collide with the character and then bounce off him subtracting only one life - but same thing is happening here the object detects collision twice sometimes. Would be looking forward to your reply.

edit: Oh and yes all of my objects which are falling are multi-element bodies, however for my character I’m using a circle as the physics body.

Also, your transition may be interfering with the physics engine trying to move the object (the collision response) and vice versa.  You can turn off collision response and only detect collisions by making the object a sensor.

obj = \<make object code here\> physics.addBody( obj, .... settings here ) obj.isSensor = true -- ADD THIS LINE AFTER ADDING BODY

Hi @amdriod95,

This could be caused by various things. Are you not isolating the collision to the “began” phase? Are any of your objects multi-element bodies? Please see the following guide if you haven’t read it already:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Take care,

Brent

@Brent Thanks for you reply. Yes the began phase is added although I didnt use the ended phase. I found a fix around it by adding:

event.other:removeSelf()  – So it removes whichever fruits collides with the character and its working fine.

But now the problem is with the other harful object which I want to collide with the character and then bounce off him subtracting only one life - but same thing is happening here the object detects collision twice sometimes. Would be looking forward to your reply.

edit: Oh and yes all of my objects which are falling are multi-element bodies, however for my character I’m using a circle as the physics body.

Also, your transition may be interfering with the physics engine trying to move the object (the collision response) and vice versa.  You can turn off collision response and only detect collisions by making the object a sensor.

obj = \<make object code here\> physics.addBody( obj, .... settings here ) obj.isSensor = true -- ADD THIS LINE AFTER ADDING BODY