Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Spawning asteroids
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Paul




PostPosted: Thu Jun 02, 2005 3:14 pm   Post subject: Spawning asteroids

I'm making an asteroids game (I know, not very creative), and I have a couple of ideas about spawning asteroids, y'know on the screen and shooting them will kill them into smaller pieces. Just wondering if some one has a suggestion about it (e.g. the way the seperate pieces move after a large piece has been destroyed) . I'm not planning on anything complex, basically a flexible array holding the locations of the asteroids, collision detection etc. If anyone has any suggestions from experience or anything, please post, it might make the process easier for me Smile and I don't mean code or anything, just ideas. One thing I've been wondering is if I should make irregularily shaped asteroids, if so which kind of collision detection I could use, other than whatdotcolor.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Jun 02, 2005 4:10 pm   Post subject: (No subject)

You can make irregularly shaped asteroids, just make them fairly close to circular. Then you can just use some Math.Distance() and you'll get a good enough approximation.
Here's how I would tackle the breaking of the asteroids:
assume the laser / bullet / whatever your ship shoots is a massive ball. It has a certain amount of energy, determined by E_sub_k = 1/2 mv^2. Assuming all energy is transferred into the asteroid, you do some vector addition to determine the new path of the asteroid. Next, you've got to split the asteroid into two, so you half the mass (or maybe a little less than half, since some surface rock is blown away) and set the new angles of these asteroids to x degrees + the new angle for the big one and x degrees - the new agngle for the big one.
Paul




PostPosted: Thu Jun 02, 2005 8:00 pm   Post subject: (No subject)

I understand the energy part, could you explain the angle part a bit more? And also I've never learned using Math.Distance, but I can go read the tutorial on it.
Delos




PostPosted: Thu Jun 02, 2005 9:27 pm   Post subject: (No subject)

Basically Cervantes has given you a brief tour of the mores of Momentum and Collisions. Physics stuff.

I'm going to explain this in components, so follow allong.

asteroid_a has mass m_a and velocity v_a
v_a has v_a_x and v_a_y
v_a_x = v_a_cosT (T = angle at which asteroid is travelling)
v_a_y = v_a_sinT

asteroid_a undergoes explosion, into asteroid_b and asteroid_c
mass (asteroid_b) = 0.5m_a = m_b (assume half mass, to make it easier)
m_c = m_b (same mass)

assume asteroid_a was travelling vertically up, therefore, it only had a y-component:
v_a_x = 0
v_a_y = v_a

Now, when it explodes, all the energy inertial to the asteroid is transfered into its fragments:
v_a_y = v_b_y + v_c_y
v_a_x = v_b_x + v_c_x = 0
therefore
v_b_x = -v_c_x

Now you can see that asteroid_b and asteroid_c have equal speeds, but opposite signs to their velocities. Simple, eh?
To figure out their y-components, you need to know the angles with with they leave. You could randomize this or something...
Or you could set their y-components (remembering that their sum must equal v_a_y and then figure out the angle using trig.)

I'm sure you're asking yourself, why did I mention mass?

Well, since we're talking physics, and since Cervantes mentioned Kinetic Energy, let's expand a bit.

The above equations are simplified versions of the concept of conservation of momentum:
p = mv
momentum = mass * velocity
If you work with this eq'n instead (as would be more realistically correct), you'd get interesting answers.
If asteroid_a is travelling at speed v1, and for some strange reason, looses half of its mass - the resulting speed would be 2*v1. Momentum is conserved. p-before_strange_loss_of_mass = p-after_strange_loss_of_mass.

Apply this idea to your asteroids as well, remembering that their velocities are a result of their mass and momentum. Ah, the joys. Have fun.
Paul




PostPosted: Fri Jun 03, 2005 10:43 pm   Post subject: (No subject)

Ah thanks delos for the expansion, I understand the energy and mass part of it though, just was a bit confused by "set the new angles of these asteroids to x degrees + the new angle for the big one and x degrees - the new agngle for the big one."
c0bra54




PostPosted: Sat Jun 04, 2005 7:29 am   Post subject: (No subject)

UUSSEE whatdotcolou.. althought not sure how you would know what asteroid it would hit, because this way it would account for irregular shape, no matter how messed it is, it will always detect it.. have fun though, i barely understand the physics leson and i have gr 11 done Razz.. although we never did momentum yet...

lol you really gotta onder, how many ppl on compsci will go on to work in computer programming.. i meen look hwo good some are/were.. catalyst.. tony.. cervantes... lol honestly.. Razz we are the next generation of game design Razz.. kinda scary
Cervantes




PostPosted: Sat Jun 04, 2005 7:41 am   Post subject: (No subject)

c0bra54 wrote:
UUSSEE whatdotcolou.. althought not sure how you would know what asteroid it would hit, because this way it would account for irregular shape, no matter how messed it is, it will always detect it..

Sure, whatdotcolour would work for irregular shapes, but as you mentioned, determining which asteroid you actually hit would be a pain. Also, it limits you to making your asteroids look really gross. You can't use a nice picture for them, you would have to use either Turing's Draw.FillOval and only one or two colours (you could do more, but it just makes the process take longer) or a really bad looking image.

c0bra54 wrote:
we are the next generation of game design Razz.. kinda scary

Yeah, and we learn Turing like nobody's business!
c0bra54




PostPosted: Sat Jun 04, 2005 12:43 pm   Post subject: (No subject)

but why.. can't you jsut draw your asteroid, fill the backround of the image with 255,0,255 in RGB... colour 13 in turing, then use

Pic.SetTransparentColour. on the pink.. and make the outside of the asteroid brown, or red or sumthing else turing will recognize... like if red then go
... o and u can track x and y

also you don't need tooutline the asteroid at all actually.. there is a trick where you draw a second image directly behind the asteroid, and that has the asteroid be soid pink on white, and it goes before you collision check, collision checks, then you draw your normal asteroid ontop, since turing will see what isdrawn in your back buffer.. makes for REALLY handy collision detection, without gross outlining (i use in RPG game, soon to be released!!!)
umm here is my rough draft of it..

o and this won't work, i have to goto work now, but here is my general idea

blah this doen't use whatdotcolour, but ou use it, and then cehck which x and y the laser is closest to or sumthing.. but yeh, this would work for approximate hit.. i am used to using whatdotcolour for like .. different detection Razz...

code:



var asteroids flexible array 1..0,1..2 of int

%then go

create asteroid
new upper (asteroids)
asteroids(upper(asteroids))(1) := rnd x
asteroids(upper(asteroids))(2) := top screen or w/e  (y coordinate)

for i: 1.. upper(asteroids)%check every asteroid

if laserx = asteroid(i)(1) and lasery = asteroid(i)(2) then
COLLISION
else
NO COLLISION
end if


i know, no what dot colour, but you could implement it.. it would just be a pain in da but... Razz
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat Jun 04, 2005 1:51 pm   Post subject: (No subject)

Whatdotcolour is mostly useful for things that produce a certain effect every time. For example, in TheOneTrueGod's donkey kong vs. mario game, a green platform bounces the character up with a certain velocity. In an asteroids game, a collision means the asteroid will split up. But the whatdotcolour does not tell you which asteroid is hit: it only tells you that an asteroid was hit. You'd then have to go through regular collision detection (Math.Distance or the like) to determine which one you actually hit. That just begs the question, why not start off like that?
As to your idea of drawing some colour behind each asteroid, I'd be shocked if that would work. As far as I know, whatdotcolour returns the colour value that is currently on the front buffer at the specified coordinates. Thus, if you wanted to use that technique, you'd have to draw your pink ovals, View.Update them, then draw your asteroids, then View.Update them. Not only is that an unnecessary extra View.Update, the user will see some pink circles flashing!

If that's not the case, well, that's interesting. But whatdotcolour still wouldn't be the best method of collision detection for an asteroids game, IMO.
c0bra54




PostPosted: Sun Jun 05, 2005 1:10 pm   Post subject: (No subject)

your righ about the asteroids .. sry my bad, but yes it is possible to do tat.. here lemme write a simple prg to do it.. it will have a picture, since i hate the draw commands in turing...

it will use the mouse and when you have collision it will tell you, this is easily adapted into a program, where all you do is set move speed to 0 when collision is seen.. here Razz

use Win rar to open archive.. and have pics in same folder



whatdotcolour collision.rar
 Description:
This has whatdotcolour collision detection, without a dumb outline

Download
 Filename:  whatdotcolour collision.rar
 Filesize:  13.15 KB
 Downloaded:  152 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: