- Forums
- :
- Creation Forum
- :
- LSL Scripting
- :
- Re: Calculating roll and pitch, how is it done?

- Start Article
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page

# Calculating roll and pitch, how is it done?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-16-2012 09:02 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Curious Hazelnut - view message

11-16-2012 11:19 AM

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)

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

[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Dora Gustafson - view message

11-16-2012 01:18 PM - edited 11-16-2012 01:25 PM

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.

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Curious Hazelnut - view message

11-17-2012 02:16 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Dora Gustafson - view message

11-17-2012 01:43 PM

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;

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Dora Gustafson - view message

11-19-2012 02:30 PM

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;

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

[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Curious Hazelnut - view message

02-03-2015 10:16 AM - edited 02-03-2015 10:17 AM

(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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Cubey Terra - view message

02-03-2015 10:23 AM

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.

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Rolig Loon - view message

02-03-2015 10:29 AM

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.

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

[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Reply to Cubey Terra - view message

02-03-2015 09:30 PM - edited 02-03-2015 09:32 PM

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