|
Basic vectors questions |
Ad@m22
Member #11,906
May 2010
|
Can somebody explain me basics of vectors please a lot? |
GullRaDriel
Member #3,861
September 2003
|
wikipedia to the rescue ? "Code is like shit - it only smells if it is not yours" |
KnightWhoSaysNi
Member #7,339
June 2006
|
x = length * cos(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 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
|
Length. If I knew the answer to what you are asking then I would help with that, but I don't. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
LennyLen
Member #5,313
December 2004
|
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
|
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
|
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. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Ad@m22
Member #11,906
May 2010
|
Thanks, that is it. |
SiegeLord
Member #7,827
October 2006
|
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 |
GullRaDriel
Member #3,861
September 2003
|
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" |
Ad@m22
Member #11,906
May 2010
|
That is only example,the important thing is the way to do 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()
|
gnolam
Member #2,030
March 2002
|
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". -- |
Ad@m22
Member #11,906
May 2010
|
Okey, it moves to opposite directions |
William Labbett
Member #4,486
March 2004
|
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:
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 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
|
Follow Halméns checklist. It will tell you exactly what's wrong with your code. -- |
LennyLen
Member #5,313
December 2004
|
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 |
Chris Katko
Member #1,881
January 2002
|
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) Cartesian to Polar Coordinents -----sig: |
Ad@m22
Member #11,906
May 2010
|
Thank you for that link and explain of it |
|