How do you convert an x, y, z vector into heading, pitch, bank?

Started by stevmjon, September 16, 2023, 11:17:21 PM

Previous topic - Next topic

stevmjon

just trying this out. i have a vector that has an x,y,z value. i want to convert this into heading, pitch, bank.

i looked at polar co-ordinates into cartesian co-ordinates, but does this give heading, pitch, bank? i can only see degrees between 2 vectors.

i am trying to rotate a vector from any point of view. i can rotate the vector no problem, but converting this rotation amount into degrees (heading, pitch, bank) seems challenging...
i want to store degrees for the direction an object is facing.

would i need an up vector & look vector and rotate both of these? just throwing around a few idea's.

any ideas would be great?

   stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


so wanna do flight control stuff ?   good question..   I would have thought you'd need more than one vector though ?

stevmjon

i came up with an idea. i will use 2 vectors. one is an up vector, which i will use for bank, and a look vector which i will use for heading & pitch.

i want to use this in the ray tracer to be able to view an object from any angle, and use the rotate tool to rotate the object around this viewing angle as if the viewing angle is an axis.
i can already rotate the object using the object normal vector, but i want to save this rotation position as heading, pitch, bank.

keeping the objects as data (no polygons) is more challenging, but fun (sorta, lol).

i have noticed that in 3D software, there is actually 2 different rotations. one is when you edit the object by rotating the mesh (this does not effect the heading, pitch, bank). the other is when you are placing the object in the scene with move/rotate (this does effect the heading, pitch, bank). because i have no polygon mesh, i am combining both the rotations i do to the object when i edit it, but i need to save this rotation edit as heading, pitch, bank so it holds it's rotation position in the scene.
the reason is i am keeping each object seperate from each other, so you can't combine the objects together, just place each one where you want them.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

I asked ChatGPT :)


It sounds like you're trying to convert a 3D vector into Euler angles (heading, pitch, and bank), commonly known as roll, pitch, and yaw. These angles represent the orientation or rotation of an object in 3D space. The conversion from a vector to Euler angles is not straightforward and requires some mathematical operations. Here's an approach to do this:

1. Normalize your vector:

   Ensure your input vector is a unit vector, i.e., it has a length of 1. If it's not already normalized, divide each component (x, y, z) by the vector's magnitude to make it a unit vector.


2. Calculate the pitch angle (rotation around the x-axis):

   Use the arc sine function to find the pitch angle:
   
   pitch = asin(vector.y)


3. Calculate the heading angle (rotation around the y-axis):

   You can use the arctangent function to find the heading angle:

   heading = atan2(vector.x, vector.z)

4. Calculate the bank angle (rotation around the z-axis):
   Since you only have a single vector, the bank angle will be 0 unless you have additional information about the orientation of your object.

This method assumes you're working with a vector pointing in the direction your object is facing. The pitch angle gives you the tilt (up/down rotation), and the heading angle gives you the rotation around the vertical axis (left/right). The bank angle is typically 0 for a vector-based representation unless you have more information about the orientation.

If you want to incorporate the bank angle, you would need additional information, such as a reference up vector, to calculate it. In this case, you would create a rotation matrix and then decompose it into Euler angles.

Here's a pseudo-code example of how you could calculate the bank angle if you have a reference up vector:

# Assuming reference_up_vector is a known unit vector
forward_vector = normalize(your_input_vector)
right_vector = cross(reference_up_vector, forward_vector)
up_vector = cross(forward_vector, right_vector)

bank = atan2(right_vector.y, up_vector.y)

This code calculates the bank angle based on the reference up vector and the forward vector, and it assumes that both vectors are unit vectors. The bank angle represents the rotation around the forward vector.

Keep in mind that Euler angles can have issues with gimbal lock, and other rotation representations like quaternions are often preferred for 3D rotations in practice.


stevmjon

thanks kev, i am looking at this. in my research i didn't see this particular calculation. the good thing about maths is there is multiple ways to calculate most things.
i have never used chatGPT, but have heard it is very handy. what did you write to find this?

i have also noticed that if you have a heading, pitch, bank setting for an object, then if you rotate that object from its default position to this rotated position by heading then pitch then bank, that new position it is actually incorrect.
it seems if you rotate from the default position by bank then pitch then heading it is then correct. this is if you rotate around the world axis.

i was using the order of HPB and wondered why the object was spinning weird, lol.

i shall continue with my calculations and use BPH and post when i have figured it out. i thought up this reverse order just today.
i have never needed to rotate an object via your point of view until recently. it is not as straight forward as i thought it would be.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

i got this to work kev, using your routine above.

it took this long to get it to work because at first it didn't work. the object was always skewed off angle when rotating all axis, but worked when rotating on only any one axis.
so i made a routine that drew the look vector , up vector , right vector on screen and for some reason this worked correctly.
it ended up being i was drawing the object itself in the wrong order, but drew the vectors in the correct order. i am glad i did this to compare.
so a quick modification and it works well. i wish i picked up on the earlier...user error, lol.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.