Author Topic: [SOLVED] Is it Possible to do Hit Detection Between All Instances of Two Classes  (Read 1279 times)

JakeWilund

  • Member
  • **
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Hello Flixel Forums,

I've lurked the forums for a while and have been able to find a lot of answers. However, I have a question that I don't think has been asked (at least that I can tell).

I am building a Bomberman style game, and need to do some kind of hit/collision/overlap detection between all instances of my Hero class and all instances of my Explosion class.

Here's a simplified walkthrough of how my game runs.

1. Game state is created.

2. Map and Hero are added to Game state.

3. After the "SPACE" key is just pressed, add instance of Bomb class at the current location of the Hero.

4. Bomb is created, added, and animated.

5. After Bomb finishes animation, animationCallback(onExplode) runs.

6. onExplode creates instances of the Explosion class. Multiple instances are created, each going in the available directions based on where the bomb is placed.

This is where I need to know how to detect a collision between any of the Explosion instances, and my Hero instance.

If I add the collision detection inside my Bomb class (where the Explosions are created), then I can successfully detect overlaps and kill my hero. However, I need this hit detection to take place in my Game state in the update function, because otherwise the hit detection will only run once (on the animationCallback). I need the hit detection to be running every frame, thus why it needs to be in the Game state's update function.

I am willing to post any and all of my code if it will help us find a solution.

Here is the game in its current form: http://jakewilund.com/rigrunners/
Please note, the hit detection in this version is being done within the Bomb class (on the animationCallback). You'll notice that if you press the Space bar, and don't move, the explosion will kill you. However, if you walk away before it explodes, and then walk back into the explosion during it's animation, it will not kill you.

This is what I'm trying to remedy.

Thanks in advance for any help, and please let me know if there's any additional information that you would find helpful.
« Last Edit: Mon, Nov 28, 2011 by JakeWilund »

JakeWilund

  • Member
  • **
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Innocent bump. Anyone have any ideas?

Kytin

  • Member
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
My inclination would be to put all instances of explosion objects in their own group, and in the update for the playstate call an overlap check of that group against the player, with a callback to a function that kills the player if an overlap is detected.

e.g. "FlxG.overlap(explosionGroup, player, killPlayer);"
I have seen the future. You will not.

Alextfish

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
Yep, precisely as Kytin says. This is a classic pattern that comes up in a very large number of games: for example, Space Invaders wants to check for collisions between (all the player's bullets) and (all the invaders) - you certainly don't want to have to manually loop over each bullet checking if it hit any invader.

The solution is indeed to use FlxGroups. You'll need to keep a few FlxGroup objects somewhere. If you're using the Registry pattern as Photonstorm's tutorials recommend, then the Registry is ideal, but they can just go in your PlayState. Call them things like Registry.allExplosions, Registry.allEnemyBullets, Registry.allEnemies etc; or just Registry.explosions, Registry.bullets etc.

Each time you create an Explosion object, be sure to go Registry.allExplosions.add(thisExplosion);

Then in your PlayState's update() function, just one call to overlap or collide will do:

FlxG.overlap(Registry.player, Registry.allExplosions, playerHitExplosion);

Either or both of the inputs to FlxG.overlap() and FlxG.collide() can be a FlxGroup, and Flixel does the right thing sensibly.

JakeWilund

  • Member
  • **
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Thank you for the responses guys. That is exactly what I ended up doing. I actually got it working a couple days ago, but if I hadn't then your responses would have led me to the solution, because adding each explosion to the explosions group (which existed in my play state) allowed me to do my overlap detection in the update function there.

Thanks again!