Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Asteroids bullets Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Craig

July 18, 1999, 04:40 PM

Does anyone have an algorithm that will let me fire a bullet
in the direction the player is facing (eg. like in Asteroids),
if not, anywhere I can get these type of algorithms from.

Cheerz in advance.

 
Why?

July 20, 1999, 01:52 PM


Craig wrote:
>>Does anyone have an algorithm that will let me fire a bullet
>>in the direction the player is facing (eg. like in Asteroids),
>>if not, anywhere I can get these type of algorithms from.
>>
>>Cheerz in advance.
>>

Hi. Although i have now abandoned any attempts to make an Asteroids game of my own due
to lack of interest, I think i can help you. First, the game itself(aside from menu
interfaces and startup/shutdown procedures) should run in an infinite game loop, such
like this (I assume you use C/C++, but this can be modified for other languages):

while(1) // 1 is always a true constant
{
// game code here
// include a base case so you can exit the loop when you perform a specified action
// ie. if the user presses 'q', pause the game and run the menu
}

Second, I assume you use rotation formulas such as those below for rotating points and
sprites:

newx=currentx * cos(rotation_angle) - currenty * sin(rotation_angle);
newy=currenty * cos(rotation_angle) + currentx * sin(rotation_angle);

As for the bullet, you could make a bullet structure type which contains 2 floats
(x and y) storing the bullet's current position, 2 floats (vx and vy) storing it's
x/y velocity, and an integer variable, life, stating whether the bullet is "alive"(still
flying) or "dead"(has it hit a target or flown too far?). Then place an if condition
that tests whether the use has pressed a fire button (SPACEBAR, CTRL, whatever), and if so
fire off a bullet or a stream of bullets (using arrays of bullets) like so:

Say the ship is at 0 degrees when it points straight up, and u rotate it to 120 degrees
(store this angle). Now, take the ship's x/y position relative to the screen (example:
at 640x480 resolution, the ship's location could be (120,300) ). When the fire button
is pressed, spawn a bullet at the ship's x/y screen location. Now u have a bullet, how to
make it move? U have to "rotate" the bullet around the center of the ship, using the
above rotation formulas and the ship's rotation angle. Then, move the bullet in
the direction of its angle by calculating its x/y velocity and adding it to
the bullet's x/y possition. Example:

Ship's x: 320
Ship's y: 240
Ship's rotation angle: 120 degrees (clockwise)

Bullet's starting x: 320
Bullet's starting y: 240
Bullet's rotation angle: 120

To move the bullet, simply add or subtract the desired x/y velocity from the bullet's
x/y possition and THEN rotate the bullet. For instance, if the ship was at the bottom
left corner of the screen facing the upper right corner of the screen
with (0,0) at the top left corner, you would add the bullet's velocity to its x possition
and subtract the y velocity from the y position. Also, whne the bullet is created,
set it's life to some number. Then, each time the bullet moves, its life will decrease by one.
When life is down to 0, the bullet will dissappear.
I hope this helps. Sorry for the long message :)

Greets,
Why?

 
Craig

July 20, 1999, 04:09 PM

Cheerz mate!

That's exactly what I was looking for. I'm not actually doing an Asteroids game, what you gave me will help me with a couple of game ideas whirring round in my head eg. a little top-view tank game; the principles are the same.

Thanx.

 
Craig

July 20, 1999, 04:10 PM

Cheerz mate!

That's exactly what I was looking for. I'm not actually doing an Asteroids game, what you gave me will help me with a couple of game ideas whirring round in my head eg. a little top-view tank game; the principles are the same.

Thanx.

 
Why?

July 20, 1999, 06:40 PM

Craig wrote:
>>Cheerz mate!
>>
>>That's exactly what I was looking for. I'm not actually doing an Asteroids game, what you gave me will help me with a couple of game ideas whirring round in my head eg. a little top-view tank game; the principles are the same.
>>
>>Thanx.
>>

Im curious: what kind of game do you plan to make? I've been doing graphics here and there
in DOS and recently made the switch to Glide and Visual C++. A lot of the issues I've
faced confused me, such as raycasting and 3D. I've often wished i could ask someone
or just share my ideas with a friend so we could solve the puzzle at hand. Anyway,
are you looking for anyone to work with on a project of some sort? I would be glad to help
you and I would learn a lot myself in the process. How about working on a small game
or something? Just a thought.

Greets:
Why?

 
Why?

July 20, 1999, 06:40 PM

Craig wrote:
>>Cheerz mate!
>>
>>That's exactly what I was looking for. I'm not actually doing an Asteroids game, what you gave me will help me with a couple of game ideas whirring round in my head eg. a little top-view tank game; the principles are the same.
>>
>>Thanx.
>>

Im curious: what kind of game do you plan to make? I've been doing graphics here and there
in DOS and recently made the switch to Glide and Visual C++. A lot of the issues I've
faced confused me, such as raycasting and 3D. I've often wished i could ask someone
or just share my ideas with a friend so we could solve the puzzle at hand. Anyway,
are you looking for anyone to work with on a project of some sort? I would be glad to help
you and I would learn a lot myself in the process. How about working on a small game
or something? Just a thought.

Greets:
Why?

 
Craig

July 21, 1999, 07:50 PM

Yeah, great idea!!

Check your email!

 
This thread contains 7 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.