# Vector to Angle , and vice versa

## Recommended Posts

I'm currently trying to figure out how to convert XYZ to PYR. Here are my classes.

Also i'm currently looking it up. I know of sin/cos. I'm learning what i can from online docs.

Vector:
[SPOILER]
[CODE]
#region Properties
public float X;
public float Y;
public float Z;
public float Forward { get { return Y; } set { Y = value; } }
public float Right { get { return X; } set { X = value; } }
public float Up { get { return Z; } set { Z = value; } }
#endregion
#region Constructors
public Vector( )
{ X = 0; Y = 0; Z = 0; }
public Vector( float x , float y )
{ X = x; Y = y; Z = 0; }
public Vector( float x , float y , float z )
{ X = x; Y = y; Z = z; }
#endregion
#region Methods
public Vector Normalize( )
{ }
#endregion
#region Operators
public static Vector operator +( Vector vector1 , Vector vector2 )
{ return new Vector ( vector1.X + vector2.X , vector1.Y + vector2.Y , vector1.Z + vector2.Z ); }
public static Vector operator -( Vector vector1 , Vector vector2 )
{ return new Vector ( vector1.X - vector2.X , vector1.Y - vector2.Y , vector1.Z - vector2.Z ); }
public static Vector operator /( Vector vector1 , Vector vector2 )
{ return new Vector ( vector1.X / vector2.X , vector1.Y / vector2.Y , vector1.Z / vector2.Z ); }
public static Vector operator *( Vector vector1 , Vector vector2 )
{ return new Vector ( vector1.X * vector2.X , vector1.Y * vector2.Y , vector1.Z * vector2.Z ); }
public static void operator ++( Vector vector )
{ vector.Z++; }
public static void operator --( Vector vector )
{ vector.Z--; }
#endregion
[/CODE]
[/SPOILER]

Angle:
[SPOILER]
[CODE]
#region Properties
public float P;
public float Y;
public float R;
public float Pitch { get { return Pitch; } set { P = value; } }
public float Yaw { get { return Yaw; } set { Y = value; } }
public float Roll { get { return Roll; } set { R = value; } }
#endregion
#region Constructors
public Angle( )
{ P = 0; Y = 0; R = 0; }
public Angle( float pitch , float yaw )
{ P = pitch; Y = yaw; R = 0; }
public Angle( float pitch , float yaw , float roll )
{ P = pitch; Y = yaw; R = roll; }
#endregion
#region Methods
public Vector Forward( )
{
}
public Vector Right( )
{
}
public Vector Up( )
{
}
#endregion
[/CODE]
[/SPOILER] Edited by DarkScience

##### Share on other sites
I didn't look at your code, but there seems to be a conceptual problem with what you are doing: XYZ describes a vector, while PYR describes a rotation. These are different things, and you can't convert one to another.

What are you really trying to do?

##### Share on other sites
[quote name='alvaro' timestamp='1339437464' post='4948228']
I didn't look at your code, but there seems to be a conceptual problem with what you are doing: XYZ describes a vector, while PYR describes a rotation. These are different things, and you can't convert one to another.

What are you really trying to do?
[/quote]

You don't appear to understand the Vector/Angle model. It is possible and is done in just about every game out there. PYR can be converted to a normal which is a direction represented in XYZ and vice-versa.

Also i may of just figured it out. Ensuring consistency now ...
Never mind still don't have it .... Edited by DarkScience

##### Share on other sites
first set
[spoiler][quote]
double A_P = math.DegreeToRadian ( P ); // 0 to 360
double A_Y = math.DegreeToRadian ( Y );// 0 to 360
double A_R = math.DegreeToRadian ( R );// 0 to 360

normal.X = ( Math.Sin ( A_Y ) );
normal.Y = ( Math.Cos ( A_Y ) );
normal.Z = ( Math.Tan ( A_P ) );
[/quote]

seems to give.
[quote]
Angle: {P=0,Y=0,R=0}
Forward: {X=0,Y=1,Z=0}

Angle: {P=0,Y=90,R=0}
Forward: {X=1,Y=6.12303176911189E-17,Z=0}

Angle: {P=0,Y=180,R=0}
Forward: {X=1.22460635382238E-16,Y=-1,Z=0}

Angle: {P=0,Y=270,R=0}
Forward: {X=-1,Y=-1.83690953073357E-16,Z=0}

Angle: {P=0,Y=360,R=0}
Forward: {X=-2.44921270764475E-16,Y=1,Z=0}
[/quote]

Y seems to be correct at 0,180,360 but incorrect in between.
X seems to be correct at 0,90,270 but incorrect in between and oddly enough at 360.
[/spoiler]

second set
[spoiler]
[quote]
double A_P = P; //math.DegreeToRadian ( P );
double A_Y = Y; //math.DegreeToRadian ( Y );
double A_R = R; //math.DegreeToRadian ( R );

normal.X = ( Math.Sin ( A_Y ) );
normal.Y = ( Math.Cos ( A_Y ) );
normal.Z = ( Math.Tan ( A_P ) );
[/quote]

seems to give
[quote]
Angle: {P=0,Y=0,R=0}
Forward: {X=0,Y=1,Z=0}

Angle: {P=0,Y=90,R=0}
Forward: {X=0.893996663600558,Y=-0.44807361612917,Z=0}

Angle: {P=0,Y=180,R=0}
Forward: {X=-0.80115263573383,Y=-0.598460069057858,Z=0}

Angle: {P=0,Y=270,R=0}
Forward: {X=-0.176045946471211,Y=0.984381950632505,Z=0}

Angle: {P=0,Y=360,R=0}
Forward: {X=0.958915723414307,Y=-0.283691091486527,Z=0}
[/quote]

Y is correct at 0 and incorrect every where else.
X is correct at 0 and incorrect every where else.

though the values are -1 to 1 which is the correct format.

[/spoiler]

this is what im using to determine the accuracy of the values, they seem to slightly off in the second set though very close to what there suppose to be. Seems to increase in inaccuracy the larger the number.
[url="http://www.mathsisfun.com/flash.php?path=/geometry/images/circle-triangle.swf&w=405&h=510&col=%23FFFFFF&title=Sine%2C+Cosine%2C+Tangent"]http://www.mathsisfu...Cosine, Tangent[/url] Edited by DarkScience

##### Share on other sites
[quote name='DarkScience' timestamp='1339437639' post='4948229']
You don't appear to understand the Vector/Angle model.
[/quote]

##### Share on other sites
[quote name='alvaro' timestamp='1339440584' post='4948242']
[quote name='DarkScience' timestamp='1339437639' post='4948229']
You don't appear to understand the Vector/Angle model.
[/quote]

[/quote]

Hoping you're not saying that in a sarcastic way in which your mad. I don't insult people in that way and wasn't implying anything by that comment.

Also I believe i will try a more manual way of doing what it is i desire. By converting 0 to 360 to -180 to 180 and applying some arithmetic's to convert it to x and y. Edited by DarkScience

##### Share on other sites
My comment was sarcastic, but I am not mad. The point of my first post is that you should probably try to explain how that vector XYZ expresses a rotation (i.e., how do you apply it to another vector?), so you can make things more clear to yourself and others. The procedure to convert PYR to the other format might become apparent once you express things precisely.

Also, giving some background about what you are trying to accomplish will generally get you much better help. Perhaps the solution to your problem would be trivial if you used some other representation for rotations, for example. Edited by alvaro

##### Share on other sites
[quote name='alvaro' timestamp='1339437464' post='4948228']
I didn't look at your code, but there seems to be a conceptual problem with what you are doing: XYZ describes a vector, while PYR describes a rotation. These are different things, and you can't convert one to another.

What are you really trying to do?
[/quote]

Thought it was clear from the code i posted. But i shall explain further. Also Can't be done isn't asking for a better explanation nor is not reading all that i posted advised when your goal is to understand what i am after.

This is the object orientation in 3D space.
-X is Left, +X is Right
-Y is Backward , +Y is Forward
-Z is Down , +Z is Up

Now this is the view as in camera or object rotation.
-Pitch is Down, +Pitch is Up
-Yaw is Left, +Pitch is Right
-Roll is Roll Left, +Roll is Roll right // this isn't as important right now

The goal is to convert Pitch , Yaw , Roll to X , Y , Z and then back.
So Angle( 0 , 0 , 0 ) would be in XYZ ( 0 , 1 , 0 ).
So Angle( 0 , 90 , 0 ) would be in XYZ ( 1 , 0 , 0 ).
So Angle( 0 , 180 , 0 ) would be in XYZ ( 0 , -1 , 0 ).
So Angle( 0 , 270, 0 ) would be in XYZ ( -1 , -1 , 0 ).
So Angle( 0 , 360, 0 ) would be in XYZ ( 0 , 1 , 0 ). Edited by DarkScience

##### Share on other sites
[quote name='DarkScience' timestamp='1339437639' post='4948229']
You don't appear to understand the Vector/Angle model. It is possible and is done in just about every game out there. PYR can be converted to a normal which is a direction represented in XYZ and vice-versa.
[/quote]
Orientation and position are related but VERY different.

To represent position in 3D space you need spatial coordinates.
To represent orientation in 3D space you need a PYR triplet, or a 3x3 matrix, or a quaternion.

The first says where you are, the second defines your orientation (which can be used to determine what directions are up and forward).

##### Share on other sites
Alvaro is correct: a triplet of pitch, yaw and roll (Euler angles) represents a 3D orientation. An euclidean 3D vector can at most represent a direction in 3D space (plus a magnitude). That direction can be e.g. an axis of rotation, or a look-at direction, but it does not capture a full 3D orientation.

If representing an axis of rotation with a 3D euclidean vector, one needs to add in a single scalar (the angle of rotation about that axis) to make it a complete representation of a 3D orientation. This is commonly called an axis-angle representation of an orientation. If you are looking to convert an axis-angle representation to a 3D rotation matrix (and then to an Euler triplet), see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_RotateAxisAngle.php"]float3x3::RotateAxisAngle[/url] function (boils down to [url="http://clb.demon.fi/MathGeoLib/docs/Quat_SetFromAxisAngle.php"]Quat::SetFromAxisAngle[/url]).

If representing a look-at direction with a 3D euclidean vector, one needs to add in the concept of a 'up' vector (also called a 'world up' or a 'scene up' vector, a fixed vector specifying the upwards direction convention in the scene), or a single scalar 'roll' (being equivalent to above axis-angle) to make it a complete representation of a 3D orientation. This orientation however cannot have any roll, if the up vector is kept static. If you are looking how to turn a look-at direction vector into a 3D orientation, see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_LookAt.php"]float3x3::LookAt[/url].

To convert a rotation matrix to pitch-yaw-roll components (Euler angles), see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_ToEulerXYX.php"]float3x3::ToEuler***[/url] conversion functions.

##### Share on other sites
[quote name='DarkScience' timestamp='1339442924' post='4948262']
The goal is to convert Pitch , Yaw , Roll to X , Y , Z and then back.
So Angle( 0 , 0 , 0 ) would be in XYZ ( 0 , 1 , 0 ).
So Angle( 0 , 90 , 0 ) would be in XYZ ( 1 , 0 , 0 ).
So Angle( 0 , 180 , 0 ) would be in XYZ ( 0 , -1 , 0 ).
So Angle( 0 , 270, 0 ) would be in XYZ ( -1 , -1 , 0 ).
So Angle( 0 , 360, 0 ) would be in XYZ ( 0 , 1 , 0 ).
[/quote]
Your numbers don't make sense. How can (0,270,0) be (-1,-1,0) ?

Perhaps it is time for to go find a primer on 3D mathematics?

As for converting between them, it looks like you want to project one unit along the forward vector of the orientation and find that position.
As for reversing it, there are infinitely many orientations that can generate the forward vector.

##### Share on other sites
[quote name='frob' timestamp='1339443475' post='4948267']
[quote name='DarkScience' timestamp='1339442924' post='4948262']
The goal is to convert Pitch , Yaw , Roll to X , Y , Z and then back.
So Angle( 0 , 0 , 0 ) would be in XYZ ( 0 , 1 , 0 ).
So Angle( 0 , 90 , 0 ) would be in XYZ ( 1 , 0 , 0 ).
So Angle( 0 , 180 , 0 ) would be in XYZ ( 0 , -1 , 0 ).
So Angle( 0 , 270, 0 ) would be in XYZ ( -1 , -1 , 0 ).
So Angle( 0 , 360, 0 ) would be in XYZ ( 0 , 1 , 0 ).
[/quote]
Your numbers don't make sense. How can (0,270,0) be (-1,-1,0) ?

Perhaps it is time for to go find a primer on 3D mathematics?

As for converting between them, it looks like you want to project one unit along the forward vector of the orientation and find that position.
As for reversing it, there are infinitely many orientations that can generate the forward vector.
[/quote]

Sorry wrote that in a hurry to represent the directions in relation to x,y,z not the proper numbers.

##### Share on other sites
[quote name='clb' timestamp='1339443467' post='4948266']
Alvaro is correct: a triplet of pitch, yaw and roll (Euler angles) represents a 3D orientation. An euclidean 3D vector can at most represent a direction in 3D space (plus a magnitude). That direction can be e.g. an axis of rotation, or a look-at direction, but it does not capture a full 3D orientation.

If representing an axis of rotation with a 3D euclidean vector, one needs to add in a single scalar (the angle of rotation about that axis) to make it a complete representation of a 3D orientation. This is commonly called an axis-angle representation of an orientation. If you are looking to convert an axis-angle representation to a 3D rotation matrix (and then to an Euler triplet), see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_RotateAxisAngle.php"]float3x3::RotateAxisAngle[/url] function (boils down to [url="http://clb.demon.fi/MathGeoLib/docs/Quat_SetFromAxisAngle.php"]Quat::SetFromAxisAngle[/url]).

If representing a look-at direction with a 3D euclidean vector, one needs to add in the concept of a 'up' vector (also called a 'world up' or a 'scene up' vector, a fixed vector specifying the upwards direction convention in the scene), or a single scalar 'roll' (being equivalent to above axis-angle) to make it a complete representation of a 3D orientation. This orientation however cannot have any roll, if the up vector is kept static. If you are looking how to turn a look-at direction vector into a 3D orientation, see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_LookAt.php"]float3x3::LookAt[/url].

To convert a rotation matrix to pitch-yaw-roll components (Euler angles), see e.g. MathGeoLib's [url="http://clb.demon.fi/MathGeoLib/docs/float3x3_ToEulerXYX.php"]float3x3::ToEuler***[/url] conversion functions.
[/quote]

Alvaro is incorrect. I explained that im looking for the direction not the absolute full 3d orientation. He should of figured given the deffinition of Angle. Alvaro thanks for this stressful encounter. Go to hell.

clb thanks for an actual reply to my inquiry. Though go to hell for saying "Alvaro is correct".

Sorry i thought he would understand that i'm trying to get a Angle not a Position or Vector.

##### Share on other sites
Oh and Alarvo [url="http://en.wikipedia.org/wiki/Angle"]http://en.wikipedia.org/wiki/Angle[/url] Edited by DarkScience

##### Share on other sites
Each of your posts say you want to go back again. Only one direction makes sense.

You can project one unit along your orientation to find a 'forward' normal. That is a matter of simple trig.

The other direction, finding an orientation that generates the same forward normal, does not really make sense. There are infinitely many solutions.

##### Share on other sites
[quote name='DarkScience' timestamp='1339442924' post='4948262']
The goal is to convert Pitch , Yaw , Roll to X , Y , Z and then back.
So Angle( 0 , 0 , 0 ) would be in XYZ ( 0 , 1 , 0 ).
So Angle( 0 , 90 , 0 ) would be in XYZ ( 1 , 0 , 0 ).
So Angle( 0 , 180 , 0 ) would be in XYZ ( 0 , -1 , 0 ).
So Angle( 0 , 270, 0 ) would be in XYZ ( -1 , -1 , 0 ).
So Angle( 0 , 360, 0 ) would be in XYZ ( 0 , 1 , 0 ).
[/quote]

This sounds like you're trying to define a 3D orientation based on a LookAt direction of an object. See if the functions in my previous post can help you with that (float3x3::LookAt, and then float3x3::ToEuler***).

##### Share on other sites
[quote name='frob' timestamp='1339444044' post='4948276']
Each of your posts say you want to go back again. Only one direction makes sense.
[/quote]

What ???

[quote name='frob' timestamp='1339444044' post='4948276']
You can project one unit along your orientation to find a 'forward' normal. That is a matter of simple trig.
[/quote]

Thats what i'm asking how to do. I've been getting an output which i posted here.
[quote name='DarkScience' timestamp='1339439671' post='4948239']
first set
[spoiler][quote]
double A_P = math.DegreeToRadian ( P ); // 0 to 360
double A_Y = math.DegreeToRadian ( Y );// 0 to 360
double A_R = math.DegreeToRadian ( R );// 0 to 360

normal.X = ( Math.Sin ( A_Y ) );
normal.Y = ( Math.Cos ( A_Y ) );
normal.Z = ( Math.Tan ( A_P ) );
[/quote]

seems to give.
[quote]
Angle: {P=0,Y=0,R=0}
Forward: {X=0,Y=1,Z=0}

Angle: {P=0,Y=90,R=0}
Forward: {X=1,Y=6.12303176911189E-17,Z=0}

Angle: {P=0,Y=180,R=0}
Forward: {X=1.22460635382238E-16,Y=-1,Z=0}

Angle: {P=0,Y=270,R=0}
Forward: {X=-1,Y=-1.83690953073357E-16,Z=0}

Angle: {P=0,Y=360,R=0}
Forward: {X=-2.44921270764475E-16,Y=1,Z=0}
[/quote]

Y seems to be correct at 0,180,360 but incorrect in between.
X seems to be correct at 0,90,270 but incorrect in between and oddly enough at 360.
[/spoiler]

second set
[spoiler]
[quote]
double A_P = P; //math.DegreeToRadian ( P );
double A_Y = Y; //math.DegreeToRadian ( Y );
double A_R = R; //math.DegreeToRadian ( R );

normal.X = ( Math.Sin ( A_Y ) );
normal.Y = ( Math.Cos ( A_Y ) );
normal.Z = ( Math.Tan ( A_P ) );
[/quote]

seems to give
[quote]
Angle: {P=0,Y=0,R=0}
Forward: {X=0,Y=1,Z=0}

Angle: {P=0,Y=90,R=0}
Forward: {X=0.893996663600558,Y=-0.44807361612917,Z=0}

Angle: {P=0,Y=180,R=0}
Forward: {X=-0.80115263573383,Y=-0.598460069057858,Z=0}

Angle: {P=0,Y=270,R=0}
Forward: {X=-0.176045946471211,Y=0.984381950632505,Z=0}

Angle: {P=0,Y=360,R=0}
Forward: {X=0.958915723414307,Y=-0.283691091486527,Z=0}
[/quote]

Y is correct at 0 and incorrect every where else.
X is correct at 0 and incorrect every where else.

though the values are -1 to 1 which is the correct format.

[/spoiler]
[/quote]

PYR are in degrees in second set...

Seems to be a slight inaccuracy. I cant get it working properly even though from what i know second set should work fine.

Ignore the Z = nonsense. Its just a placeholder.

EDIT The issue is that when X or Y is suppose to be full (-1 ,0, 1) they are but the other number is 6.E~~~ (first set)

EDIT AGAIN:
[quote]Remember that 6.12303176911189E-17 is 0.00000000000000006 (I may have even missed a zero there!) so it is a very, very small deviation.[/quote]

That's the issue i was having, now that i know what it is i can fix it. So my calculations are correct. Just a issue that i must work around.

Also as to the -1 rating. Thanks. It means alot to me. Edited by DarkScience

##### Share on other sites
[b]SOLUTION[/b]
[CODE]
private PointF DegreesToXY( float degrees , float radius , Point origin )
{
PointF xy = new PointF ( );
double radians = degrees * Math.PI / 180.0;
xy.X = (float) Math.Round ( x * 1000000 ) / 1000000;
xy.Y = (float) Math.Round ( y * 1000000 ) / 1000000;
return xy;
}
[/CODE]

or
[spoiler]
[CODE]
public static double DegreesToX( double degrees , double radius )
{
double radians = degrees * ( Math.PI / 180.0 );

if ( x < 0.1 )
x = Math.Round ( x * 100000000000000 ) / 100000000000000;

return x;
}
public static float DegreesToXF( float degrees , float radius )
{
float radians = degrees * (float) ( Math.PI / 180.0 );

if ( x < 0.1 )
x = (float) Math.Round ( x * 1000000 ) / 1000000;

return x;
}

public static double DegreesToY( double degrees , double radius )
{
double radians = degrees * ( Math.PI / 180.0 );

if ( y < 0.1 )
y = Math.Round ( y * 100000000000000 ) / 100000000000000;

return y;
}
public static float DegreesToYF( float degrees , float radius )
{
float radians = degrees * (float) ( Math.PI / 180.0 );

if ( y < 0.1 )
y = (float) Math.Round ( y * 1000000 ) / 1000000;

return y;
}
[/CODE]
[/spoiler] Edited by DarkScience

##### Share on other sites
That's a solution to what exactly? I don't see a conversion from XYZ to PYR anywhere.

The strange rounding you do at the end will give you the illusion that the result is more precise, but you are actually throwing precision away in general.

##### Share on other sites
[CODE]
float __fastcall T3DPlanet::GetLongitude(t3dpoint cpos) //poludniki (Dlugosc)
{
float angle;
angle = n2dGetPolarCoordAngleA(cpos.x-pos.x,cpos.z-pos.z) / 6.2731860f;

angle = -360.0f*angle;
angle = VALIDUJ(angle)+90.0f;
angle = VALIDUJ(angle);
return angle;

}

float __fastcall T3DPlanet::GetLatitude(t3dpoint cpos)
{
t3dpoint cpn;
cpn = vectorAB(pos,cpos);
cpn = Normalize(cpn);
float t = cpn.x*cpn.x + cpn.z*cpn.z;
t = sqrt(t);
if (cpn.y == 0.0f) return 0.0f;
float tanges = t / cpn.y;
float angle;
angle = atan(tanges);
if (angle < 0) angle = -1.0f*(90.0f + angle); else
angle = 90.0f - angle;
return angle;
}
float __fastcall n2dGetPolarCoordAngleA(float x,float y)
{
if ( (x > 0) && (y >= 0) ) { return ArcTan(y/x); }
if ( (x > 0) && (y < 0) ) { return ArcTan(y/x)+2.0*3.1415926535897932384626433832795; }
if (x < 0) { return ArcTan(y/x)+3.1415926535897932384626433832795; }
if ( (x == 0) && (y > 0) ) { return 3.1415926535897932384626433832795/2.0; }
if ( (x == 0) && (y < 0) ) { return 3.0*3.1415926535897932384626433832795/2.0; }
//last versions were without .0 just simple 2 division
return - 1000.0;
}
[/CODE]

pos is a point(0,0,0)

glop is acually heading angle! imopi =pi / 180
[CODE]
[/CODE]

then the vector is +SUPERPOINT.x,
-SUPERPOINT.y,-SUPERPOINT.z

need more ? Edited by ___

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628277
• Total Posts
2981776

• 10
• 11
• 17
• 11
• 9