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