Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Basic vectors questions

This thread is locked; no one can reply to it. rss feed Print
Basic vectors questions
Ad@m22
Member #11,906
May 2010

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. ???::)

GullRaDriel
Member #3,861
September 2003
avatar

wikipedia to the rescue ?
ask google ?

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

KnightWhoSaysNi
Member #7,339
June 2006
avatar

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

Ad@m22
Member #11,906
May 2010

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?

edit
Is this right code? ::)

#SelectExpand
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}

bamccaig
Member #7,536
July 2006
avatar

LennyLen
Member #5,313
December 2004
avatar

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
Second in Command
February 2005
avatar

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

They all watch too much MSNBC... they get ideas.

Ad@m22
Member #11,906
May 2010

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

SiegeLord
Member #7,827
October 2006
avatar

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:

x=x+lenght*cos(angle*PI/180);
y=y+lenght*sin(angle*PI/180);

Which is exactly what you do anyway.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Ad@m22
Member #11,906
May 2010

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

SiegeLord
Member #7,827
October 2006
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

GullRaDriel
Member #3,861
September 2003
avatar

Where are X and Y on your picture ?

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Ad@m22
Member #11,906
May 2010

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? :'(

#SelectExpand
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()

gnolam
Member #2,030
March 2002
avatar

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?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Ad@m22
Member #11,906
May 2010

Okey, it moves to opposite directions
Here is picture to show you how it works, up and down mean keys
{"name":"14scvi0.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/c\/8c0097e3ed90fff4b1188fc8d9745fc0.jpg","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/c\/8c0097e3ed90fff4b1188fc8d9745fc0"}14scvi0.jpg

William Labbett
Member #4,486
March 2004
avatar

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 ?

Ad@m22
Member #11,906
May 2010

Yes, that's right

Johan Halmén
Member #1,550
September 2001

Checklist:

  • 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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Ad@m22
Member #11,906
May 2010

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

gnolam
Member #2,030
March 2002
avatar

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

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

LennyLen
Member #5,313
December 2004
avatar

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).

Ad@m22
Member #11,906
May 2010

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
Member #1,881
January 2002
avatar

Why hasn't anyone linked to this?
http://pixwiki.bafsoft.com/mags/5/articles/circle/sincos.htm

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Ad@m22
Member #11,906
May 2010

Thank you for that link and explain of it :)

Go to: