•      Sign In
• Create Account

## Converting an angle to vectors

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.

7 replies to this topic

### #1SMurf7  Members

122
Like
Likes
Like

Posted 23 June 2001 - 01:53 AM

Hi, I''m making a rather cheap clone of another 2D game (it''s my first game, so bear with me here) and I''m trying to convert an angle in radians to x and y vectors. My code looks like this:- void Create(int x, int y, double Dir) { xpos = x; ypos = y; Fh = cos(1.5707963267949 - Dir); Fv = cos(Dir); if ((int)Fh != Fh) X[0] = Fh - -(int)Fh; if ((int)Fv != Fv) Y[0] = Fv - -(int)Fv; active = true; } xpos and ypos track the current position of the object, Fh and Fv are doubles representing the vector results (horizontal and vertical), X[0] and Y[0] are doubles, overflow variables used during the movement of the object. However, this code does not appear to produce the correct vectors. Admittedly I''m quite rubbish at this sort of math, so could somebody put me out of my misery? Please?

### #2 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 23 June 2001 - 06:16 AM

ok tell me if I''m making any incorrect assumptions as I go

"void Create(int x, int y, double Dir)
{
xpos = x;
ypos = y;"

basically just assigning some sort of grid coordinates, Dir is the angle

"Fh = cos(1.5707963267949 - Dir);"

taking the sin of Dir, but doing it wrong. sin is dir + pi/2 or dir - pi/2. Dir shouldn''t be the negative part. Also bad naming convention. Fh? How does that remind you of the y value?

"Fv = cos(Dir);"

getting the x

"if ((int)Fh != Fh)
X[0] = Fh - -(int)Fh;"

what the hell is this? Ok let''s start with the (int)Fh. Casting to an int always rounds towards 0. Unless your value happens to be one of the very few cases where it is 1 or -1 you will always get 0. So that condition will always evaluate to true except in specific cases. Now if that is true it will assign something to X[0]. What is X[0] standing for, the x part? Remember that Fh is representing the y. Ok now what are we assigning. We start by turning FH into 0 (or -0), it always turns to 0. Now we take the opposite of it. Then we subtract it from itself. So in the end all we do is assign Fh (the y value) to X[0]. If the angle happened to be 0 or pi nothing gets assigned to X[0]

"f ((int)Fv != Fv)
Y[0] = Fv - -(int)Fv;
active = true;"

then we do the reverse here, with the x value being assigned to Y[0].

### #3SMurf7  Members

122
Like
Likes
Like

Posted 24 June 2001 - 02:48 AM

Fh and Fv represent horizontal and vertical components respectively (x and y). I lifted the names from a physics textbook (F is force).

I use X[0] and Y[0] to control the overflow of the vectors, i.e if the resultant Fh should be incremented by 1.26 (impossible to have "partial pixels"), then the .26 is put here and incremented in the move code every cycle until it is greater or equal to 1 (then the Fh is incremented and this gets set back to its original value).

Erm, is that any clearer?

### #4 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 24 June 2001 - 07:59 AM

Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)

### #5 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 24 June 2001 - 08:01 AM

Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)

### #6 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 24 June 2001 - 08:02 AM

Vector math isn''t that difficult. Consider this

+=object /= direction of the vector

^Y /
! /
! /
!/ )angle
+---------------> X

To easily calculate the Fh and Fv components we define a vector with lenght=1. This is a unit vector and simplies our calculations. To calculate Fh and Fv think of a rectangular triangle:

/!
/ !
1 / !
/ ! Fv
/ !
/ !
/)angle!
--------
Fh

We defined the hypothenusa to be 1. We use some trig and calculate Fv and Fh.

Fh= 1*cos(angle)
Fv= 1*sin(angle)

### #7SMurf7  Members

122
Like
Likes
Like

Posted 24 June 2001 - 10:36 AM

I''m afraid posting the same thing 3 times does not solve the fact that I am dealing with 360 degrees of movement and not 45. Subsequently, the functions used to calculate the vectors (sin, cos) would change depending on the angle (I think). I just don''t know how

### #8 Anonymous Poster_Anonymous Poster_*   Guests

Likes

Posted 25 June 2001 - 06:59 AM

Ok now see what you mean with the X[0] stuff. You have two problems. The first is that you still haven''t fixed the line where you get the height. That is not the way to get a sin out of a cos. Also you need to scale both parts by the length of the vector, otherwise the overflow will do nothing and your guy will move too slowly. Chance the 3rd and 4th lines to:

Fh = (vector.length)*cos(Dir+1.5707963267949 );
Fv = (vector.length)*cos(Dir);

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.