An Asteroids programming question

Video Game Discussions and general topics.

Moderators: AArdvark, Ice Cream Jonsey

User avatar
Ice Cream Jonsey
Posts: 30077
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

An Asteroids programming question

Post 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?
the dark and gritty...Ice Cream Jonsey!

pinbacker
Posts: 317
Joined: Mon Jul 23, 2007 11:03 am
Location: The Sun!

Re: An Asteroids programming question

Post 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.
That's the wrong video, by the way.

User avatar
Ice Cream Jonsey
Posts: 30077
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

Post 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.
the dark and gritty...Ice Cream Jonsey!

pinbacker
Posts: 317
Joined: Mon Jul 23, 2007 11:03 am
Location: The Sun!

Post by pinbacker »

Mine was in C, and written in 1990, and has been long gone too long to merely call it "long gone".
That's the wrong video, by the way.

bruce
Posts: 2544
Joined: Tue Jun 04, 2002 10:43 pm

Post 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

pinbacker
Posts: 317
Joined: Mon Jul 23, 2007 11:03 am
Location: The Sun!

Post by pinbacker »

See? Math. WhaddI tell you.
That's the wrong video, by the way.

User avatar
Ice Cream Jonsey
Posts: 30077
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

Post 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."
the dark and gritty...Ice Cream Jonsey!

pinbacker
Posts: 317
Joined: Mon Jul 23, 2007 11:03 am
Location: The Sun!

Post 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"!
That's the wrong video, by the way.

User avatar
Ice Cream Jonsey
Posts: 30077
Joined: Sat Apr 27, 2002 2:44 pm
Location: Colorado
Contact:

Post 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.
the dark and gritty...Ice Cream Jonsey!

Post Reply