#### Archived

This topic is now archived and is closed to further replies.

# Converting an angle to vectors

This topic is 6300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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].

##### Share on other sites
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?

##### Share on other sites
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)

##### Share on other sites
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)

##### Share on other sites
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)

##### Share on other sites
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

##### Share on other sites
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);

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632630
• Total Posts
3007525
• ### Who's Online (See full list)

There are no registered users currently online

×