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

Username:   Password: 
 RegisterRegister   
 Shooting Help
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evan467




PostPosted: 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)
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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!
Turing_Gamer




PostPosted: 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...
SNIPERDUDE




PostPosted: 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)



ang.png
 Description:
 Filesize:  2.13 KB
 Viewed:  5466 Time(s)

ang.png


TerranceN




PostPosted: Tue Feb 09, 2010 5:32 pm   Post subject: Re: Shooting Help

How I would do it:

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


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


Increment position by velocity each frame
hint:
Spoiler:
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).


UnitCircles.zip
 Description:

Download
 Filename:  UnitCircles.zip
 Filesize:  334.58 KB
 Downloaded:  420 Time(s)

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

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: