libGosu/Chingu - How to handle simultaneous collisions?

139 views Asked by At

I am working on a simple Asteroids remake using libGosu and Chingu. Similar to the original Asteroids, when a bullet hits a large meteor, the meteor breaks apart into two smaller meteors. When the player achieves a weapons upgrade, it can shoot more than one bullet at the same time. Sometimes two bullets hit the same asteroid at the same time, causing it to break into four smaller meteors, not two as intended. Is there a way in Chingu to cancel out one of the collisions when there are two simultaneous collisions? This is how I have it set up currently:

Bullet.each_collision(Meteor1) do |bullet, meteor|
  Explosion.create(:x => meteor.x, :y => meteor.y)
  Meteor2.create(:x => meteor.x, :y => meteor.y)
  Meteor2.create(:x => meteor.x, :y => meteor.y)
  meteor.destroy
  bullet.destroy
  @score += 100
  Sound["media/audio/explosion.ogg"].play(0.2)
end
Bullet.each_collision(Meteor2) do |bullet, meteor|
  Explosion.create(:x => meteor.x, :y => meteor.y)
  Meteor3.create(:x => meteor.x, :y => meteor.y)
  Meteor3.create(:x => meteor.x, :y => meteor.y)
  meteor.destroy
  bullet.destroy
  @score += 100
  Sound["media/audio/asplode.ogg"].play(0.2)
end
Bullet.each_collision(Meteor3) do |bullet, meteor|
  Explosion.create(:x => meteor.x, :y => meteor.y)
  meteor.destroy
  bullet.destroy
  @score += 100
  Sound["media/audio/asplode.ogg"].play(0.2)
end

Is there a way to omit the creation of the unwanted extra meteors when two bullets hit the same meteor at the same time?

1

There are 1 answers

1
Spooner On BEST ANSWER

You need to add "break" to the end of each of the blocks to prevent checking for collisions after you've hit the meteor with the first bullet and the meteor is no longer there.