Triggering onCollision at certain times during play

Hey everyone, my first post here. Please bear with me.

In my game I check whenever the player taps the screen, what object they are sitting with (collision wise). At this point I would like the onCollision event to run and check the collisions at this moment only, as during gameplay they could drift on and off collisions until the user taps the screen to stop movement.

Is there a way to do this or does onCollision have to be on Runtime and always running? I hope it makes sense. So basically the player taps the screen and the game stops, collision runs and sees it colliding with A,B, but missed C

Thank you for the help.

You can’t trigger collisions that way.

You can track them (on your own), of you could use raycasts to for proximity checks.

https://docs.coronalabs.com/api/library/physics/rayCast.html

Hi @Pinro,

Another option is to briefly “flash” the object (object to detect) to inactive then immediately back to active, in two consecutive lines. That will (if done properly) make the physics engine think that the object just arrived at its location, and it will trigger a collision event. As a side benefit, because it’s done in the same time step, you shouldn’t see any actual visual result of that happening.

So basically, use this to set the body inactive then active again:

https://docs.coronalabs.com/api/type/Body/isBodyActive.html

Best regards,

Brent

Hi @Brent - Thanks for that suggestion but I’m still having issues with this. The main problem is when the player taps the screen and hasn’t hit anything at all - which means it doesn’t fire onCollision. Is there any way around this?

Hi @Pinro,

Is this basically like the character/object is drifting around and overlapping various “sensor” objects for collision? If so, can the player potentially overlap more than one sensor region at a given time, or would it always be just one or none?

Brent

Hi @Brent - It seems my OP might be a little confusing, sorry about that.  I thought I would draw up a super basic diagram to help illustrate what I am trying to achieve. I am thinking that showing this will probably result in a solution to the problem. 

http://i.imgur.com/lc9sRh5.png

The whole idea is that object 1(star) will move down the screen at a set speed. The player has to tap anytime to stop it inside the blue circle space (2). It has to fit in snuggly without touching the green edge stroke (3) which will probably be a separate PNG.

If the player taps to stop the star anywhere other than (2) & (3) its instantly game over. If the star is touching (2) but also touching the the outter edge (3) its gameover.

So the issue I am having are:

  1. How to detect if the star(1) is inside the space(2) but is touching the stroke(3) by any amount at the same time.
  2. How to detect if its not touching anything at all (game over)
  3. How to detect the above only after the player has tapped

I appreciate you taking the time to help me find a solution to this. Thanks for that Brent. I hope this also makes my predicament easier to understand. 

You can’t trigger collisions that way.

You can track them (on your own), of you could use raycasts to for proximity checks.

https://docs.coronalabs.com/api/library/physics/rayCast.html

Hi @Pinro,

Another option is to briefly “flash” the object (object to detect) to inactive then immediately back to active, in two consecutive lines. That will (if done properly) make the physics engine think that the object just arrived at its location, and it will trigger a collision event. As a side benefit, because it’s done in the same time step, you shouldn’t see any actual visual result of that happening.

So basically, use this to set the body inactive then active again:

https://docs.coronalabs.com/api/type/Body/isBodyActive.html

Best regards,

Brent

Hi @Brent - Thanks for that suggestion but I’m still having issues with this. The main problem is when the player taps the screen and hasn’t hit anything at all - which means it doesn’t fire onCollision. Is there any way around this?

Hi @Pinro,

Is this basically like the character/object is drifting around and overlapping various “sensor” objects for collision? If so, can the player potentially overlap more than one sensor region at a given time, or would it always be just one or none?

Brent

Hi @Brent - It seems my OP might be a little confusing, sorry about that.  I thought I would draw up a super basic diagram to help illustrate what I am trying to achieve. I am thinking that showing this will probably result in a solution to the problem. 

http://i.imgur.com/lc9sRh5.png

The whole idea is that object 1(star) will move down the screen at a set speed. The player has to tap anytime to stop it inside the blue circle space (2). It has to fit in snuggly without touching the green edge stroke (3) which will probably be a separate PNG.

If the player taps to stop the star anywhere other than (2) & (3) its instantly game over. If the star is touching (2) but also touching the the outter edge (3) its gameover.

So the issue I am having are:

  1. How to detect if the star(1) is inside the space(2) but is touching the stroke(3) by any amount at the same time.
  2. How to detect if its not touching anything at all (game over)
  3. How to detect the above only after the player has tapped

I appreciate you taking the time to help me find a solution to this. Thanks for that Brent. I hope this also makes my predicament easier to understand.