Basic vectors questions

Can somebody explain me basics of vectors please a lot?
I know how to calculate lenght of vector (lenght=sqrt(x*x+y*y)). I want to explain angles and how to get the new x and y. Please show me how on some simple example with this picture.
I want to use left and right keys to set the angle and up nad down to move forward and backward. But I want only know how to calculate angle and new x, y. The best solution will be show it on some example picture. Thanks a lot everybody. ???::)


wikipedia to the rescue ?
ask google ?


x = length * cos(angle)
y = length * sin(angle)


I tried wikipedia and google but nothing please explain it to me ::) yes I found new x and y as you but it did not work for me perhaps because I used wrong angle so can you show me it on some example in picture please?

Is this right code? ::)

1#include <cmath> 2#include <iostream> 3 4#define PI 3.14159265 5 6int main() { 7 8 using namespace std; 9 10 int x=100, y=100, angle=45, lenght=0; 11 // next x=110, next y = 110 12 lenght=sqrt(10*10+10*10); 13 14 cout<<"lenght: "<<lenght<<endl; 15 16 x=x+lenght*cos(angle*PI/180); 17 y=y+lenght*sin(angle*PI/180); 18 19 cout<<"x: "<<x<<endl; 20 cout<<"y: "<<y<<endl; 21 22 return 0; 23 24}


Length. :P If I knew the answer to what you are asking then I would help with that, but I don't. :-X

Ad@m22 said:

The best solution will be show it on some example picture.

Perhaps you should draw an illustration to let us know what you're trying to calculate. I can't understand what you're trying to say.

Arthur Kalliokoski

There's a Windows bitmap in the paperclip in the original post, but it looks like a simple transposition.


I am only asking that is my source code of calculating the new x and y right? ::) that was my question


Yes, you are calculating the new x and y values correctly. Not too sure why you are calculating the length that way, however...

Basically, if you want to move distance 'lenght' in the direction 'angle' you do:


Which is exactly what you do anyway.


Thanks, that is it. :)
Do you know about better way to calculate length? :-X


No, the way you calculate it is right if you want to calculate the length of the vector (10, 10). I'm not sure why you are calculating it.


Where are X and Y on your picture ?

What kind of data do you already know for computing X and Y ?


That is only example,the important thing is the way to do it :)
Thanks for all.
I tried this code but it doesn t work, where I am doing mistake please?
The length is in every case 10, the object move about 10 pixels. Can somebody fix me it? :'(

1#include <cmath> 2#include <allegro.h> 3 4#define PI 3.14159265 5 6int main() { 7 8 allegro_init(); 9 install_keyboard(); 10 11 set_color_depth(desktop_color_depth()); 12 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) { 13 14 allegro_message("Couldn't set graphics mode: 640x480.\n"); 15 return 1; 16 17 } 18 19 int x=100, y=100, angle=105, lenght=0; 20 // next x=110, next y = 110 21 lenght=10; 22 23 BITMAP *bmp=load_bitmap("pic.bmp",NULL); 24 25 rotate_sprite(screen, bmp, x, y, itofix(angle/1.40625)); 26 x=x+lenght*cos(angle*PI/180); 27 y=y+lenght*sin(angle*PI/180); 28 29 while(!key[KEY_ESC]) { 30 if(key[KEY_RIGHT]||key[KEY_LEFT]||key[KEY_UP]||key[KEY_DOWN]) { 31 if(key[KEY_UP]) { 32 x=x+lenght*cos(angle*PI/180); 33 y=y+lenght*sin(angle*PI/180); 34 clear_to_color(screen,makecol(0,0,0)); 35 rotate_sprite(screen, bmp, x, y, itofix(angle/1.40625)); 36 } 37 38 39 if(key[KEY_DOWN]){ 40 x=x-lenght*cos(angle*PI/180); 41 y=y-lenght*sin(angle*PI/180); 42 clear_to_color(screen,makecol(0,0,0)); 43 rotate_sprite(screen, bmp, x, y, itofix(angle/1.40625)); 44 } 45 46 47 } 48 rest(10); 49 } 50 51 return 0; 52 53} 54END_OF_MAIN()

Ad@m22 said:

I tried this code but it doesn t work, where I am doing mistake please?

In just using the hated phrase "doesn't work". :P
How does it not work?


Okey, it moves to opposite directions
Here is picture to show you how it works, up and down mean keys

William Labbett

Line of enquiry :-

You program says that when UP is pressed y gets incremented by

lenght*sin( 105 *PI/180);

which is a posiive value so the sprite should move down the screen.

Is this what's happening ?


Yes, that's right

Johan Halmén


  • use the right angle unit at every point

  • be sure of direction at zero angle value

  • be sure of direction of rotation when angle value increases

  • be sure of screen coordinates, where's origo, at what direction does x grow, at what direction does y grow

  • sin(0) == 0, cos(0) == 1, sin(π/2) == 1, cos(π/2) == 0

Your problems seem to relate to this checklist, not to your understanding of vectors. Someone continue my checklist. Or improve it.


Okey, but does anybody know how to fix my source code? ::)


Follow Halméns checklist. It will tell you exactly what's wrong with your code.

Ad@m22 said:

Okey, but does anybody know how to fix my source code?

People here don't make a habit of writing other people's source code for them, but we'll happily give you the tools to do it yourself (which has already been done in this case).


When I rotate my image in own image file it works good and all int are float :-/
rotated about 90 deegres to right

Chris Katko

Why hasn't anyone linked to this?

Read that article, it explains about all of the basics you need to know.

A quick summery for once you know what you're doing:

theta - the angle of your vector

Polar to Cartesian Coordinents

x=magnitude * cosine(theta)
y=magnitude * sine(theta)

Cartesian to Polar Coordinents
magnitude_of_x_y = sqrt(x^2 + y^2)
theta = atan2(x,y) *it's atan2(y,x) in MATLAB and some other languages.


Thank you for that link and explain of it :)

Thread #605230. Printed from