Reply
Member
Posts: 195
0 Kudos

Calculating roll and pitch, how is it done?

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).

 

Posts: 2,023
Registered: ‎10-05-2009

Re: Calculating roll and pitch, how is it done?

Reply to Curious Hazelnut - view message

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)

Member
Posts: 195
0 Kudos

Re: Calculating roll and pitch, how is it done?

[ Edited ]

Reply to Dora Gustafson - view message

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.

Highlighted
Posts: 2,023
Registered: ‎10-05-2009
0 Kudos

Re: Calculating roll and pitch, how is it done?

Reply to Curious Hazelnut - view message

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

Posts: 2,023
Registered: ‎10-05-2009

Re: Calculating roll and pitch, how is it done?

Reply to Dora Gustafson - view message

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;

 Smiley SurprisedSmiley HappySmiley Very Happy

Member
Posts: 195
0 Kudos

Re: Calculating roll and pitch, how is it done?

Reply to Dora Gustafson - view message

That is working . 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;

 

Honored Resident
Posts: 11
0 Kudos

Re: Calculating roll and pitch, how is it done?

[ Edited ]

Reply to Curious Hazelnut - view message

(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

 

 

Posts: 27,255
Registered: ‎10-05-2009
0 Kudos

Re: Calculating roll and pitch, how is it done?

Reply to Cubey Terra - view message

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.

Not as dumb as I look
Honored Resident
Posts: 11
0 Kudos

Re: Calculating roll and pitch, how is it done?

Reply to Rolig Loon - view message

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.

Posts: 27,255
Registered: ‎10-05-2009
0 Kudos

Re: Calculating roll and pitch, how is it done?

[ Edited ]

Reply to Cubey Terra - view message

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));
    }
}

 

Not as dumb as I look