Page 1 of 1

An Asteroids programming question

Posted: Fri Oct 26, 2007 1:38 am
by Ice Cream Jonsey
This can be pseudo-cody, so it's out there for anyone.

OK, imagine if you will an Asteroids game where all the Asteroids are squares and they don't rotate. We're also using vectors.

It is easy to determine if a bullet his the asteroid, because you can easily determine the size of the asteroid by simply knowing it's upper-left-hand position and whether it's a big, medium or small asteroid. If the bullet is drawn inside the area an asteroid must encompass, then run the routine (or jump to the area in the code) where the asteroid breaks up.

But how do you solve this for asteroids that are not square chunks? You can cheat by assigning it square space anyway. But eventually that's going to look like shit and the player will notice.

Does anyone recall or know how this collision-detection-based problem is (well... was, let's be honest) generally solved in 2D games?

Re: An Asteroids programming question

Posted: Fri Oct 26, 2007 10:12 am
by pinbacker
Ice Cream Jonsey wrote:But how do you solve this for asteroids that are not square chunks? You can cheat by assigning it square space anyway.
That's what I did.
Does anyone recall or know how this collision-detection-based problem is (well... was, let's be honest) generally solved in 2D games?
Probably some math thing. If you know where all the "angles" are on your chunk of rock, you can probably use math to determine whether a point is inside it or out.

So that's my recommendation: Use MATH.

Posted: Fri Oct 26, 2007 11:27 am
by Ice Cream Jonsey
Do you have an Asteroids clone kicking around some place? I know for a fact that the one I did in .net is long gone, so if yours is long gone that's cool. We're just relaxing over here.

Posted: Fri Oct 26, 2007 1:13 pm
by pinbacker
Mine was in C, and written in 1990, and has been long gone too long to merely call it "long gone".

Posted: Fri Oct 26, 2007 8:01 pm
by bruce
If your rocks are roughly circular, then use basically the same detection algorithm, only compare (where x and y are the coordinates of the bullet, x1 and y1 the coordinates of the rock's center, and r the radius of the rock) (x -x1)^2 + (y - y1)^2 <= r^2.

Using circles for the real rock footprint rather than squares makes it look less fakey, since you don't have the "shooting the corner which is nowhere near the rock" problem.

Also, you'll note that the real distance formula would take the square root of all those values. Fuck that; sqrt() is expensive. Multiplication is (usually) cheap.

Bruce

Posted: Mon Oct 29, 2007 8:38 am
by pinbacker
See? Math. WhaddI tell you.

Posted: Mon Oct 29, 2007 9:42 am
by Ice Cream Jonsey
Thank you both. This WILL be dividends, I promise. You'll look back on this thread fondly. I swear it! Even those of you who use Linux, thanks to the wonders of "Blitz3D."

Posted: Mon Oct 29, 2007 12:48 pm
by pinbacker
And then it's a short hop and a skip from changing the word "asteroid" in these conversations to the word "Creeping Maw"!

Posted: Mon Oct 29, 2007 4:04 pm
by Ice Cream Jonsey
Yeah, there are asteroids in Creeping Maw... or at least there will be... so this is all very consistent.

I asked the same question in the Vectrex newsgroup, and got an answer similar to Bruce's. (I just wanted to see discussion there and here on this.) I need to take a personal day to do the Satan's Hollow thing I've got planned.