Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Shooting Help
Author Message
evan467

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

DemonWasp

Posted: 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

Posted: 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

Posted: 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:  4508 Time(s)

TerranceN

Posted: 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:
Filename:  UnitCircles.zip
Filesize:  334.58 KB

 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: