Making Collisions Work Every Time?

Hi all, I have been trying for the past couple days to get the collisions in my game to work properly. Currently, they only work sometimes, and only for the bullets.

Like sometimes, when bullets hit enemies, the score goes up, and the bullet and enemy objects both destroy, as they should. But other times, the bullets just go right past the enemy, but actually rotate the enemy a little bit, meaning that they do seem to be hitting, it’s just none of the code is executing. 

My guess is that I need some sort of collider on my objects, but I’m not quite sure how to go about doing that. I’ve tried a couple things like Box2D, but I couldn’t get it to work. I was hoping someone here could point me in the right direction. I’ve attached my code.

Try setting:

bullet.isBullet = true (after the line adding the physics body to the bullet)

Just added it. It does seem to be working a bit more often, but a lot of bullets are still flying right through enemies. Should I test for the isBullet condition somewhere in my onCollision function?

Hi @jrh7,

You might also experiment with increasing the position and velocity iteration check values. This will generally improve detection between small, fast-moving objects and other objects, but beware that it comes with a potential performance decrease.

http://docs.coronalabs.com/api/library/physics/setPositionIterations.html

http://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

Brent

Thanks, Brent. I’ve added both functions to my code, using the default values (8 and 3), and it has allowed me to notice something odd.

Whenever the enemy ships first spawn at the top of the screen, the bullets hit them just fine, every time now. It’s after the enemies started moving downward a bit that everything moves past them and none of my code executes. That also explains why my code when the player collides with an enemy is not working. The player is at the bottom of the screen, when the collisions for the enemies have already stopped working. What could cause that, do you think?

Have you checked the physics world in “hybrid” or “debug” mode to see where the actual bodies are?

Brent

Oh, no. How do I access “hybrid” and “debug” mode?

Hi @jrh7,

Have you studied most of the physics guides and tutorials? Physics is a moderate to advanced topic, so you should become very familiar with the core methods and behavior before you progress too far.

Here’s a collection of resources at Corona University:

http://coronalabs.com/resources/tutorials/physics-box2d/

Best regards,

Brent

I’ve tried a bunch of things from that link, but I still can’t seem to get my code to execute. 

I set up some print statements to tell me if things are actually hitting each other. I’m getting feedback from them, meaning they are colliding, but none of my code is executing and I can’t figure out why. I’ve made my physics bodies dynamic as well, since that link said that everything collides with them, but it didn’t fix the issue. 

It just seems like none of the code will execute after the enemies have fallen down a bit. When they first spawn, the bullets are able to hit them and the score goes up and everything, as they are supposed to. But then nothing happens after they move slightly downward. Has anyone seen a situation similar to this before? I’ve attached my updated code. 

Hi @jrh7,

Most likely, you’re placing physics objects into different display groups, then you’re moving/shifting these groups around independently of each other. This will cause issues with collisions. See the note here:

http://docs.coronalabs.com/guide/physics/limitations/index.html

Try setting:

bullet.isBullet = true (after the line adding the physics body to the bullet)

Just added it. It does seem to be working a bit more often, but a lot of bullets are still flying right through enemies. Should I test for the isBullet condition somewhere in my onCollision function?

Hi @jrh7,

You might also experiment with increasing the position and velocity iteration check values. This will generally improve detection between small, fast-moving objects and other objects, but beware that it comes with a potential performance decrease.

http://docs.coronalabs.com/api/library/physics/setPositionIterations.html

http://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

Brent

Thanks, Brent. I’ve added both functions to my code, using the default values (8 and 3), and it has allowed me to notice something odd.

Whenever the enemy ships first spawn at the top of the screen, the bullets hit them just fine, every time now. It’s after the enemies started moving downward a bit that everything moves past them and none of my code executes. That also explains why my code when the player collides with an enemy is not working. The player is at the bottom of the screen, when the collisions for the enemies have already stopped working. What could cause that, do you think?

Have you checked the physics world in “hybrid” or “debug” mode to see where the actual bodies are?

Brent

Oh, no. How do I access “hybrid” and “debug” mode?

Hi @jrh7,

Have you studied most of the physics guides and tutorials? Physics is a moderate to advanced topic, so you should become very familiar with the core methods and behavior before you progress too far.

Here’s a collection of resources at Corona University:

http://coronalabs.com/resources/tutorials/physics-box2d/

Best regards,

Brent

I’ve tried a bunch of things from that link, but I still can’t seem to get my code to execute. 

I set up some print statements to tell me if things are actually hitting each other. I’m getting feedback from them, meaning they are colliding, but none of my code is executing and I can’t figure out why. I’ve made my physics bodies dynamic as well, since that link said that everything collides with them, but it didn’t fix the issue. 

It just seems like none of the code will execute after the enemies have fallen down a bit. When they first spawn, the bullets are able to hit them and the score goes up and everything, as they are supposed to. But then nothing happens after they move slightly downward. Has anyone seen a situation similar to this before? I’ve attached my updated code. 

Hi @jrh7,

Most likely, you’re placing physics objects into different display groups, then you’re moving/shifting these groups around independently of each other. This will cause issues with collisions. See the note here:

http://docs.coronalabs.com/guide/physics/limitations/index.html