Sign in to follow this  
Followers 0
Curious Hazelnut

Calculating roll and pitch, how is it done?

10 posts in this topic

My trigonometry skills are somewhat weak and I want to make a HUD to use with the new premium gift (an airplane). My problem is I can't figure out how to properly get the roll (rotation around local X axis) and pitch (rotation around local Y axis).

From what I've read, I'm pretty sure that the compass direction (called yaw if I've read the pages on rotations and quaternians correctly) is the Z component of the rotation.

So... can anyone explain to me the mathematics I need to perform to get proper pitch and roll values for my artificial horizon? In the end I need a float that is the appropriate angle (preferably in radians).

 

Share this post


Link to post
Share on other sites

If these are computed against vertical it is relatively simple:

float roll = llRot2Angle( llRotBetween( < 0.0, 0.0, 1.0 >, llRot2Left( llGetRot())));float pitch = llRot2Angle( llRotBetween( < 0.0, 0.0, 1.0 >, llRot2Fwd( llGetRot())));

Angles in radians.
You may want to scale, reverse the order of the arguments and add offsets

You may also find that the rotation of the airplane should be rotated by a constant, to get the axes right.
( it may not have x forward, y left and z upward)

1 person likes this

Share this post


Link to post
Share on other sites

Thanks, when upright, those do appear to be functioning correctly. Adjusting roll and pitch at the same time do appear to be affecting eachother. BUT the roll and pitch never seam to go negative with those calculations and they when flying upside down.


For testing, I'm sitting a prim facing along the X axis and editing the prim manually to change position and see what effect it has on the roll,pitch, and compass directions.

The premium gift airplane appears to be unrotated but thanks for reminding me. If I want this to be generic I'll need to add a menu to let people deal with rotated vehicles.

Share this post


Link to post
Share on other sites

I see the problem, but I am not sure I have any solution.
Here is a similar but more straight forward calculation:

float roll = llAcos( < 0.0, 0.0, 1.0 >*llRot2Left( llGetRot()));float pitch = llAcos( < 0.0, 0.0, 1.0 >*llRot2Fwd( llGetRot()));

 It may not be any better. I don't know, I don't have the time right now to make further investigations

Share this post


Link to post
Share on other sites

I gave the problem a little more thought and believe the complete solution is:

float roll = llAcos( < 0.0, 0.0, 1.0 >*llRot2Left( llGetRot()));
if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) roll = TWO_PI - roll;

float pitch = llAcos( < 0.0, 0.0, 1.0 >*llRot2Fwd( llGetRot()));
if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) pitch = TWO_PI - pitch;

The same thing in other words:

vector F = llRot2Fwd( llGetRot());vector L = llRot2Left( llGetRot());vector U = llRot2Up( llGetRot());float roll = llAcos( L.z );if ( U.z < 0.0 ) roll = TWO_PI - roll;float pitch = llAcos( F.z );if ( U.z < 0.0 ) pitch = TWO_PI - pitch;

 :smileysurprised::):smileyvery-happy:

1 person likes this

Share this post


Link to post
Share on other sites

That is working :matte-motes-big-grin:. It seams to offset the roll and pitch by 90 degrees to the prim rotation in testing. BUT I do understand enough about rotations to fix that for the display. Thanks!


Dora Gustafson wrote:

I gave the problem a little more thought and believe the complete solution is:

float roll = llAcos( < 0.0, 0.0, 1.0 >*llRot2Left( llGetRot()));

if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) roll = TWO_PI - roll;

 

float pitch = llAcos( < 0.0, 0.0, 1.0 >*llRot2Fwd( llGetRot()));

if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) pitch = TWO_PI - pitch;

The same thing in other words:

vector F = llRot2Fwd( llGetRot());vector L = llRot2Left( llGetRot());vector U = llRot2Up( llGetRot());float roll = llAcos( L.z );if ( U.z < 0.0 ) roll = TWO_PI - roll;float pitch = llAcos( F.z );if ( U.z < 0.0 ) pitch = TWO_PI - pitch;

 

Share this post


Link to post
Share on other sites

(I'm sorry for the necropost, but I since this thread is closest to answering my question, I thought it would be better to respond to it than start a new thread.)

I've tested the script above, but it doesn't return the correct values for pitch and roll. There seems to be something missing here.

Here are some examples after putting this script into a cube:

* Rotating the cube to <0,0,0>.
      Pitch: 1.570796
      Roll: 1.570796

* Rotating cube to <0,-20,0> ( or <0, 337.5, 0>)
     Pitch: 1.178097
     Roll: 1.570796

* Rotating cube to <-20,-20,0> ( or <337.5, 337.5, 0>
    Pitch: 1.209432
    Roll: 1.963495

So my question is (and I apologize for my lack of math skills): What are those numbers? Are they correct? If so, how do I use this to get pitch and roll values in degrees?

Thanks very much in advance from this confused aircraft maker!

 

Cubey

 

 

Share this post


Link to post
Share on other sites

Radians.   All angles are in radians. 

360 degrees == 2 * PI radians

Use DEG_TO_RAD   or RAD_TO_DEG if you don't want to just use radians.

Share this post


Link to post
Share on other sites

I suspected as much, so multiplied each by RAD_TO_DEG, but the results are still incorrect. For example, rotating the cube to <0, 270, 0> produces a roll value of 282.6501. Should be 270.

Share this post


Link to post
Share on other sites

That's odd.  It works for me.  Pitch is your rotation around X. Roll is the rotation around Y.  Z makes no difference.

default{    touch_start(integer total_number)    {        float roll = llAcos( < 0.0, 0.0, 1.0 >*llRot2Left( llGetRot()));        if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) roll = TWO_PI - roll;                float pitch = llAcos( < 0.0, 0.0, 1.0 >*llRot2Fwd( llGetRot()));        if ( < 0.0, 0.0, 1.0 >*llRot2Up( llGetRot()) < 0.0 ) pitch = TWO_PI - pitch;                llSay(0, "Roll = " + (string)(roll* RAD_TO_DEG) + "    Pitch = " + (string)(pitch*RAD_TO_DEG));    }}

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
Followers 0