Object Taking Multiple Hits And Changes Its Size With Each Hit

Hello guys,

in my game, I have following scenario:

  • There are aliens moving on the screen
  • Each alien has specific colour and "level’ - the level is represented by his size
  • To eliminate him, you have to hit him with bullet of matching colour
  • When you hit him with matching colour, his level decreases by 1 (represented by making him smaller) and his colour changes
  • When you hit him with incorrect colour, his level increases by 1 (represented by making him bigger) and his colour changes to the colour of the bullet that hit him
  • When you hit him “right” multiple times, his level goes to 0 and he dies
  • When you hit him “wrong” multiple times, his level goes beyond pre-set limit and you have lost the game
  • Attached is sample mockup to ilustrate the topic - the bigger circle represents the alien, the smaller one the bullet

My idea how to solve this is based on this

  • Using physics engine
  • Each alien has physics body attached
  • Each alien has been set into movement by using the setLinearVelocity method
  • Each alien’s display object handles collision event where the evaluation of successful/unsuccessful hit is performed and the appropriate “delayed” action is planned (via timer.performWithDelay(1, …))
  • The action means to change the size and colour of alien

The problem I have is that I need to rebuild (remove and add new) the physics body after each hit when I want change size?

  • The physics body properties cannot be changed - e.g. I cannot change the radius of the circular physics body

Is this an OK way of handling such kind of issue?

Would you use physics engine for this scenario at all?

Hi @corona.shaman,

For this, I can suggest 3 options.

  1. do exactly what you suggest: remove and re-add a smaller physics body (this will probably need to be done after a 10 millisecond timer, as several things during a collision can’t be done while the system is “resolving” the collision math).

  2. you can create each body with multiple radial parts of various sizes, and selectively “turn them off” using the physics pre-collision and “event.contact”. I wrote a tutorial on this here:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

  1. you can attempt to handle this without the physics engine, using non-physics collision sensing. 

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Hope this helps,

Brent

Thanks for the answer Brent.

I like the #2 approach.

However, do you think it would be usable also in situation if “the next expected hits” were visible for the player?

By that I mean, that the “smaller circles” were also visible, so the player knows in advance bullet of what colour he should use for the specific alien as next. Please see the following mockup

I need to prevent the circles (their bodies) from colliding with each other anyway. Any idea how?

  • My idea was to mask them out from each other via collision filter

Also, I face the problem that when the bullet body moves at high speed, it triggers the collision not only for the largest circle, but also for the smaller one before I’m able to remove the bullet (via running the delayed action in timer).

Do you have any experience with such issue?

  • I know I can play with the isBullet and isSensor attributes of the bullet body and maybe find that out in the end
  • However, maybe there is some common “best practice” approach of how to solve this kind of issue that could save me lot of time

Hi @corona.shaman,

Yes, I think collision filters are your best bet for this.

To prevent the bullet from triggering the smaller circle while that timer delay goes, you could just set a custom flag on it (bullet.canCollide = false) when it hits the largest circle, and if that flag is false when it hits the smaller circle, just ignore that collision response.

Another thing you might want to do is increase the physics “sensitivity” properties:

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

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

So basically, I think you’re going in the right direction. Just needs a bit more tweaking and you’re there. :slight_smile:

Brent

Hi @corona.shaman,

For this, I can suggest 3 options.

  1. do exactly what you suggest: remove and re-add a smaller physics body (this will probably need to be done after a 10 millisecond timer, as several things during a collision can’t be done while the system is “resolving” the collision math).

  2. you can create each body with multiple radial parts of various sizes, and selectively “turn them off” using the physics pre-collision and “event.contact”. I wrote a tutorial on this here:

http://www.coronalabs.com/blog/2013/01/08/working-with-multi-element-physics-bodies/

  1. you can attempt to handle this without the physics engine, using non-physics collision sensing. 

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Hope this helps,

Brent

Thanks for the answer Brent.

I like the #2 approach.

However, do you think it would be usable also in situation if “the next expected hits” were visible for the player?

By that I mean, that the “smaller circles” were also visible, so the player knows in advance bullet of what colour he should use for the specific alien as next. Please see the following mockup

I need to prevent the circles (their bodies) from colliding with each other anyway. Any idea how?

  • My idea was to mask them out from each other via collision filter

Also, I face the problem that when the bullet body moves at high speed, it triggers the collision not only for the largest circle, but also for the smaller one before I’m able to remove the bullet (via running the delayed action in timer).

Do you have any experience with such issue?

  • I know I can play with the isBullet and isSensor attributes of the bullet body and maybe find that out in the end
  • However, maybe there is some common “best practice” approach of how to solve this kind of issue that could save me lot of time

Hi @corona.shaman,

Yes, I think collision filters are your best bet for this.

To prevent the bullet from triggering the smaller circle while that timer delay goes, you could just set a custom flag on it (bullet.canCollide = false) when it hits the largest circle, and if that flag is false when it hits the smaller circle, just ignore that collision response.

Another thing you might want to do is increase the physics “sensitivity” properties:

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

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

So basically, I think you’re going in the right direction. Just needs a bit more tweaking and you’re there. :slight_smile:

Brent