Asteroids style movements and shooting

Just starting out? Need help? Post your questions and find answers here.
Yerrel
User
User
Posts: 14
Joined: Mon Sep 13, 2004 9:15 pm

Asteroids style movements and shooting

Post by Yerrel »

Hoi,

I am a real noob :oops: on the following:

How do i calculate or better, program a ship that will shoot in any direction the ship is pointing to? like the Ship in Asteroids.
And how do i let the ship rotate too?
And how do i let the bullet shoot in the direction the ship is pointing at?

Can anyone help me with an easy tutorial about this topic?

All that in 2D ( not 3D )

Tnx in advance...
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

this is really not as hard as you may think..

there are a couple of ways to handle the subject.. one way is to use a bunch of if's and stuff to calculate the direction the ship and bullets are going to use.. I've seen it done before, but I think it makes it all very complex, and not to mention somewhat "clumpsy"..

the way I usually go about this sort of thing, is by using sine and cosine to calculate the ship and bullet coordinates..

you might want to read up on vector mathematics if you want more detail, but basically what I do is this.

Every object I work with (ie. ship and bullets) have atleast a x and y coord, and also an angle and speed.

the wayt I calculate stuff is by adding a vector, based on the speed and angle, to the x and y coord. This is easily done like this (example for the ship):

Code: Select all

xvector = Cos(ship\angle * RADMultiplier) * ship\speed
yvector = Sin(ship\angle * RADMultiplier) * ship\speed

ship\x = ship\x + xvector
ship\y = ship\y + yvector
notice the RADMultiplier constant.. it is a value you have to multiply to every sin and cos calculations in your program, since sin() and cos() in PB works with 400 degree circles.. (and we usually use 360 deg.)
this constant is 3.141593 / 180..

now, to move the ship, we do the following.. for turning, we only add or subtract the angle of the ship (remember to only use a value between 0 and 360) according to the turn left and right buttons.
to move the ship, you just add to the ship speed (when the key for accelleration is pressed)..

I hope you understood the basics of the calculations above...

Now graphics-wise, I'd suggest you draw a ship, and then make rotated copies if it in one or several graphic file(s).. you then load the files for example into an array of, say 36 files.. (this is to make the rotations smooth).. then to show the right graphics on screen, you just get the current ship angle, and divide it by 10, then show the sprite in that arraynumber.. :)

another solution is to make a series of points, and draw lines between them (to manually draw the ship), but then you'd have to also calculate the new position of each point every frame, so that they ship points also rotate in accordance with the ship angle..

simple, huh.. :D

If you want, I'm sure I can knock up an example for you on the ship movement, when I have som spare time...

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Tributoids makes good use of Sin and Cos as well as ATan. Also to save in file size, instead of pre calculating and rendering sprites into graphic files, it calculates points in real time and connects them using LineXY :)

http://www.reelmedia.org/tributoids

It also compiled quite nicely on both Linux and Windows with no modifications.
Image Image
Yerrel
User
User
Posts: 14
Joined: Mon Sep 13, 2004 9:15 pm

Post by Yerrel »

If you want, I'm sure I can knock up an example for you on the ship movement, when I have som spare time...[/quote]


Yes please do, it would be like, send from heaven... :lol:

But plz in realy easy to follow code okay? :roll:

Thanks...
Post Reply