Member
Posts: 195

# 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: 1,895
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())));```

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

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

Posts: 1,895
Registered: ‎10-05-2009

# 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: 1,895
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;```

Member
Posts: 195

# 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;```