Computer Science Canada

Shooting Help

Author:  evan467 [ Tue Feb 09, 2010 11:34 am ]
Post subject:  Shooting Help

Ive tried many different ways to do this but im starting to draw blanks, I want the bullets to be there own type and they have to be drawn going to the mouse where and have a bullet count of 6
How can i do it so that ll bullets move at equals speed in the direction going through the mouswheres x and y
(please point me in the right direction dont straight up give me the answer)

Author:  DemonWasp [ Tue Feb 09, 2010 12:22 pm ]
Post subject:  RE:Shooting Help

The answer lies in trigonometry (your math teacher said it'd be useful, and they weren't lying). Get a piece of paper and pencil. Draw a position for the mouse as a point and a position for the bullet as a point. Draw the straight line between them (should be on an angle for this explanation to make the most sense).

Select some portion of that line, starting at the bullet's position and heading towards the mouse's, and label that length "bullet speed". Now, use that part as the hypotenuse of a right-angle triangle that's aligned with the X and Y axes. You want to find the component of the bullet-speed line in the X and Y directions. You should be able to do this with Grade 10 trigonometry (sin, cos, tan).

Move the bullet by that amount in each direction. Done!

Author:  Turing_Gamer [ Tue Feb 09, 2010 12:43 pm ]
Post subject:  Re: Shooting Help

I would suggest to have an array of the bullets.
Then have for i : 1 .. 6 (these are bullets), have them say if the bullet is shot and hasn't collided, then xbullet (1) := x, ybullet (1) := y then the coord's = themselves. Draw.FillOval (xbullet (1), ybullet (1), 50 - sizereduction, 50 - size reduction)
sizereduction =- 1

Something like that...

Author:  SNIPERDUDE [ Tue Feb 09, 2010 1:54 pm ]
Post subject:  Re: Shooting Help

a = the angle (direction) the player is facing. (if you are just using 4 directions, right = 0, left = 180, up = 90, down = 270)
green circle is the player
b = bullet velocity (how many pixels does the bullet move each frame? 6?)
bx = bullet x direction (use trig to figure out from the player angle what this will be, this is how you figure out where it can be drawn. ex: bx = bx + cosd(player_angle) * bullet_velocity
by = bullet y direction (same as above. ex: by = by + sind(player_angle) * bullet_velocity)

Author:  TerranceN [ Tue Feb 09, 2010 5:32 pm ]
Post subject:  Re: Shooting Help

How I would do it:

Find the angle using trigonometry
angle = arctangent of (Δy/Δx)

Make a velocity x and y for each bullet
velocity x = cosine of (angle) multiplied by speed
velocity y = sine of (angle) multiplied by speed

Increment position by velocity each frame
position x += velocity x
position y += velocity y

I also attached an interactive diagram of a unit circle, and it also shows how the components of an angle are found (its an executable because op said not to give it away, if anyone wants the code, just ask).