View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Moving a projectile from an angle.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1L i n c k  Members

Posted 06 November 2012 - 11:59 AM

Hi, I'm trying to make something like a game... where I have a sort of a cannon that shoots projectiles, and this cannon is constantly rotating around itself, and around a point in the center of the screen. And it's 2D

Well, I have a bullets vector, that I can get the X an Y position of them. When I create every bullet I create them in the cannon X and Y position, and I also keep the angle that the cannon was in another vector.

The question is... how do I move them in that angle?

I do not want to use gravity or anything like that, just move them in a constant velocity in the direction of the angle I have.

I'm using SFML, and I'm using sf::Shapes for all. So to move the projectiles, I can either use the Move() function, that has X and Y parameters and move the sf::Shape with the value that I put on the parameters, or I can use SetPosition(), that also has X and Y values.

### #2Álvaro  Members

Posted 06 November 2012 - 12:16 PM

Each bullet needs a velocity vector that looks something like (speed*cos(angle), speed*sin(angle)). You may have to tweak the formula slightly depending on the convention you used to specify the angle.

On a time step of length delta_t, you update the position like this:
new_position.x = old_position.x + velocity.x * delta_t;
new_position.y = old_position.y + velocity.y * delta_t;

As usual, I recommend that you use a unit-length vector instead of an angle to begin with, and then you just need to multiply it by the speed.

Edited by Álvaro, 06 November 2012 - 12:18 PM.

### #3clb  Members

Posted 06 November 2012 - 12:19 PM

The general update mechanism of a 2D point with constant velocity towards a given aimed angle looks something like
float newX = x + cos(angle) * velocity * deltaTime;
float newY = y + sin(angle) * velocity * deltaTime;

The angle is expressed in radians, velocity is the speed of the projectiles, and deltaTime is the amount of time that has passed since the last update.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

### #4Olof Hedman  Members

Posted 06 November 2012 - 12:31 PM

The general update mechanism of a 2D point with constant velocity towards a given aimed angle looks something like

float newX = x + cos(angle) * velocity * deltaTime;
float newY = y + sin(angle) * velocity * deltaTime;

The angle is expressed in radians, velocity is the speed of the projectiles, and deltaTime is the amount of time that has passed since the last update.

Though it makes sense to only recalculate the velocity-vector when the input change, since that should be a lot less often then the framerate.
Not that it likely matters much. (and maybe it's animated, and need to change every frame)
Another nitpick: "velocity" is always a vector, and speed is a scalar (the length of the velocity vector), so it would look neater if it said "speed" in the code

Edited by Olof Hedman, 06 November 2012 - 12:33 PM.

### #5L i n c k  Members

Posted 07 November 2012 - 06:52 AM

Thank you all. I've had already tried to do with this formula, It doesn't work. But then I tried on another simplified code and it works perfectly, so there is something to do with the rest of my code, not the formula.

One more question. I've seen people saying do avoid moving things with angles, like Alvaro. Why is that so?

### #6Álvaro  Members

Posted 07 November 2012 - 07:26 AM

One more question. I've seen people saying do avoid moving things with angles, like Alvaro. Why is that so?

Vectors are a more natural representation, and as a result many operations are easier to do with vectors than with angles.

If you use angles, you'll have to use sine and cosine all the time, precisely to do what you were trying to do in this thread. You are also likely to have to use atan2 to extract an angle from a vector at some point. These operations are very expensive, and at some point you realize that you don't need to go through the angle at all: Just keep a unit-length vector, which is effectively (cos(angle), sin(angle)). Rotating vectors is not too hard, especially if you are familiar with complex numbers, since multiplying by a unit-length complex number is precisely a 2D rotation.

A second reason to avoid angles is that the fact that 0 and 360 degrees are the same angle means that at some point you'll either have non-uniqueness in your representation or a discontinuity somewhere. It also means that figuring out if a direction is between two other directions is tricky, and so is interpolating between two directions.

A third reason is that angles are incredibly messy when you move to 3D. One can still represent a rotation as three angles (see Euler angles), but other representations (quaternions and orthogonal matrices) are much easier to work with (faster, better behavior when interpolating, no gimbal lock).

Getting rid of angles usually results in code that has fewer special cases, is more elegant, is easier to get right and runs faster. The only downside is that you have to educate yourself to think in terms of vectors and matrices instead of the more familiar angle. But it's totally worth it.

### #7L i n c k  Members

Posted 07 November 2012 - 07:56 PM

Hmm, makes sense. Indeed I had some problems with this equality between 0 and 360.

I don't have any idea how it works using vectors for that. Do you have any link for a tutorial? Or can you tell me what to search for?

### #8Álvaro  Members

Posted 08 November 2012 - 09:37 AM

I don't know of any learning materials for this. But if you tell me what operations you want to perform on your directions, I can tell you how to do them.

Are you familiar with complex-number arithmetic?

### #9L i n c k  Members

Posted 09 November 2012 - 03:42 PM

Hmm, I don't have any task I need to do right now. I just wanted to learn so I can use in some future project. The game I was making is almost finished, I solved all my problems.

### #10Álvaro  Members

Posted 09 November 2012 - 03:46 PM

I don't know how old you are or if you are still a student, but the best place to gain some solid foundation about these things is a Linear Algebra course, usually offered in the first year of college.

### #11L i n c k  Members

Posted 10 November 2012 - 01:51 PM

I have an idea what complex numbers are, and I know there are some great videos on youtube about this, I actually watched some, but I don't remember anything about it now. I know they'll be there for when I want to learn =)

I'm not in college anymore, I'm 21. I'm 100% sure they didn't teach that. =|

### #12Álvaro  Members

Posted 10 November 2012 - 04:20 PM

There is a guy at my work that told her daughter he would pay for her college education if and only if she took Linear Algebra and Statistics. Smart man...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.