Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm experimenting on a small top-down space shooter where the player controls a ship and he can shoot bullets toward the direction of the mouse cursor. My question is how can I shoot the bullets with the angle given by the mouse cursor ?

Currently my bullets are created with random "angle values" ranging from -10 to 10 to each the X and Y axis so that way their position values are augmented or diminished each update giving the illusion that they follow an angle. But that just gives me a bullet that gets shot in a random direction and I need to shoot them in a specific direction (towards the mouse cursor).

I get the feeling I'm missing something here.

Edit : To make things clearer I'm looking for ways to move the projectile alongside an angle

share|improve this question
    
Is it that you need help calculating the angle based on the mouse cursor? –  Nate Jun 7 '11 at 20:41
    
Yeah, sorry if my question is somewhat vague in that regard. And also how to move the projectile based on the angle, that too I need. –  Tristan Dube Jun 7 '11 at 21:42
    
Wait, you're asking us about basic trigonometry, or the means to implement it? –  akled Jan 21 '12 at 20:45
add comment

2 Answers

up vote 16 down vote accepted

If I understood your problem properly, you just want to shoot a bullet towards a mouse position. Here is how I would do:

First of all, you must find the movement required for the bullet to get to the mouse, like so:

Vector2 movement = mousePosition - bulletStartPosition;

Then, you should normalize it to have a vector with a length of 1 so that you can hold a vector which tells you in which direction to go, like so:

movement.Normalize();

But here you have a little problem, if the direction is equal to (0, 0) (meaning that the mouse is on the bullet start position), then you'll divide by zero, so make sure you check for that with the last piece of code:

if (movement != Vector2.Zero)
    movement.Normalize();

So, you've got the movement required to move towards the mouse. You have to keep a Vector2 within your bullet class which holds the Direction of the bullet.

What's next? You have to actually move the bullet!

In your bullet update code, do the following:

bullet.Position += bullet.Direction * bullet.Speed * gameTime.ElapsedGameTime.TotalSeconds; // multiply by delta seconds to keep a consistent speed on all computers.

Where bullet.Speed is a float representing the bullet's speed in units per second.

Basically, here are the things to change:

Inside you bullet class, add a float Speed and a Vector2 Direction.

When shooting, set your bullet.Direction to mousePosition - bullet.Position and safely normalize it (by checking for equality with Vector2.Zero first).

When updating your bullet, do the following: bullet.Position += bullet.Direction * bullet.Speed * gameTime.ElapsedGameTime.TotalSeconds;.

It should work.

share|improve this answer
2  
+1 Vectors are a wonderful thing. Learn them! –  Jonathan Hobbs Jun 7 '11 at 22:43
    
Thanks, that did the trick ! Awesome link on the vectors too, definitely something I'll read soon. –  Tristan Dube Jun 8 '11 at 1:27
    
what is movement used for after you normalize it? I dont see it being used. –  Raptrex Aug 23 '11 at 18:40
    
@Raptrex: Actually movement is what I called the Direction later on. I just called it movement in the code to first show what it represents. It is basically just the Direction vector. –  Jesse Emond Aug 28 '11 at 2:13
1  
@JesseEmond Welcome to the vote to close privilege, use it wisely. –  Byte56 May 17 '13 at 19:44
show 1 more comment

If you're having trouble calculating the angle, you can use this:

Vector2 target = mousePos - startPos;
float angle = Math.Atan2( target.Y, target.X );
share|improve this answer
    
+1 Keepin' it simple. –  Nick Wiggill Jan 21 '12 at 16:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.