An Asteroids programming question
Moderators: AArdvark, Ice Cream Jonsey
- Ice Cream Jonsey
- Posts: 30077
- Joined: Sat Apr 27, 2002 2:44 pm
- Location: Colorado
- Contact:
An Asteroids programming question
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?
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!
-
- Posts: 317
- Joined: Mon Jul 23, 2007 11:03 am
- Location: The Sun!
Re: An Asteroids programming question
That's what I did.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.
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.Does anyone recall or know how this collision-detection-based problem is (well... was, let's be honest) generally solved in 2D games?
So that's my recommendation: Use MATH.
That's the wrong video, by the way.
- Ice Cream Jonsey
- Posts: 30077
- Joined: Sat Apr 27, 2002 2:44 pm
- Location: Colorado
- Contact:
-
- Posts: 2544
- Joined: Tue Jun 04, 2002 10:43 pm
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
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
- Ice Cream Jonsey
- Posts: 30077
- Joined: Sat Apr 27, 2002 2:44 pm
- Location: Colorado
- Contact:
- Ice Cream Jonsey
- Posts: 30077
- Joined: Sat Apr 27, 2002 2:44 pm
- Location: Colorado
- Contact:
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.
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!