Jump to content
  • Advertisement
Sign in to follow this  
Spawn_Kcb

Collision Detection Questions....

This topic is 1072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As a follow up of sorts to my last question I have started to write some basic games in C# before moving onto...well other things.

 

Like most people tell you start with simple games you know you can finish and shouldn't take to much time.

 

So, yesterday afternoon I wrote Space Invaders.I should point out I used my own solution instead of googling how it "Should be done". With that in mind two questions arose while I was working on collision detection.

 

1)I have a collection of "Enemy" objects that I check each iteration of the game loop to make sure the bullet hasn't collided with any of them. However, I just can't shake the feeling that there must be a better way than looping through them all and going "Has the bullet hit this one?". 

 

2)Once the bullet has struck said "Enemy" object it removes it from the collection and the game carries on. How should I handle the "Explosion"? Without having to slow/stop the game while the "Boom" runs. 

 

As I said these might not be valid questions if you where using the proper "Space Invaders" implementation but they way I work I am never going to learn typing in someone else's code...or at least not quickly anyway :-)

 

Thanks again for the help.

 

Kevin

Share this post


Link to post
Share on other sites
Advertisement
1)I have a collection of "Enemy" objects that I check each iteration of the game loop to make sure the bullet hasn't collided with any of them. However, I just can't shake the feeling that there must be a better way than looping through them all and going "Has the bullet hit this one?".

With space invaders, the space aliens nicely form a regular row, making it easy to shoot them down tongue.png

 

The trick is to first do a rough estimate. In this case put a grid of rectangles onto the aliens, and decide which grid cell is the interesting one. No need to check all the other rectangles.

 

The grid starts at from the top-left alien position. From that point you can compute the position of all other aliens. The other way around, by subtracting pullet position from the position of the top-left alien, you can compute which alien it should hit, if any, ie

 

alien_width = 50; // Every 50 pixels, you have a new alien next to the previous one (just random example numbers)

alien_height = 40; // One row of aliens is 40 pixels high (another example number)

 

alien_topleft = (100, 110);

bullet = (220, 140);

 

column = (220 - 100) / 50 = 4; // Integer division, truncate fraction away.

row = (140 - 110) / 40 = 0;

 

Thus the alien 0 rows down from the top alien row, and 4 columns to the right in that row is where the bullet is. Now just check that one alien for precise hit, and you're done.

 

 

 

2)Once the bullet has struck said "Enemy" object it removes it from the collection and the game carries on. How should I handle the "Explosion"? Without having to slow/stop the game while the "Boom" runs.

You are moving and drawing the bullet every frame, right?

An explosion is the same, except you draw a different picture each frame, first frame shows small but deep ornage fire ball, second and third one show expanding fire ball, and then you get a series of frames where the fireball becomes deep red,and reduces size. So you have say 6 or 7 different images, showing a fire ball at different point in time during the explosion. Now if you draw these after each other at the same spot (each frame the next image), it looks like a 'real' explosion.

 

You could do the same with aliens or with bullets, instead of rendering the same image each time, pick a different one each frame. Then you have animated aliens and bullets.

 

 

 

 

if you where using the proper "Space Invaders" implementation but they way I work I am never going to learn typing in someone else's code

If you really type it, and try to understand it while you type, you do learn from it. It may now be a good time to study how your solution is different from their solution (I don't think there is a single proper solution; all solutions that implement the game are equivalent.)

Edited by Alberth

Share this post


Link to post
Share on other sites

1)I have a collection of "Enemy" objects that I check each iteration of the game loop to make sure the bullet hasn't collided with any of them. However, I just can't shake the feeling that there must be a better way than looping through them all and going "Has the bullet hit this one?".


There are ways you can have the computer do less work but for the small number of enemies on screen for space invaders your solution should work fine. I wouldn't worry about it unless you have a noticeable slowdown.

2)Once the bullet has struck said "Enemy" object it removes it from the collection and the game carries on. How should I handle the "Explosion"? Without having to slow/stop the game while the "Boom" runs.


You could have a separate list of explosion objects. When you remove an enemy, you create an explosion in its place and handle its logic separately. A more general solution would be to have a list of sprites and you create different subclasses for sprites, one for an enemy, one for an explosion, ect. And simply have a list of sprites with a draw and update method. Neither of these solutions would scale well to a larger more complicated game but for space invaders, it would work fine. Edited by HappyCoder

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!