Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Pitch Gauge

Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
04-28-2005 06:50
Hi everyone,

My new vehicle is almost ready for sale, but there's just one thing I haven't been able to figure out. Since it is a mouselook vehicle, and sometimes you get confused when piloting and end up pointing up in the sky, with no ground frame of reference, I added a graphical pitch gauge.

The problem is the math, of course :-)

Right now, in one of the root prim scripts, I have the following code:

vector rot = llRot2Euler (llGetRot ());
float pitch = rot.y;

Unfortunately, this works fine when the vehicle is facing east, but it is backwards when facing west. I understand what the problem is, but I don't have enough gut feel for how quaternions work to know how to fix it. I want a value, in degrees or radians, that represents the vehicle's pitch with respect to the world axis, independent of heading.

Does anyone have a solution to this?

I will give a free copy of my vehicle (the Marlin XS-1, which has a $500 price tag) to the first person who posts a solution that works...

Thanks,
Jon
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
04-28-2005 07:38
It's not a full solution (and I can't get in world), but if facing east gives you the right pitch I guess you need to look at the z component of the Euler (I don't understand quaternions well enough either).

If that's between +/- pi/2 on the positive side then use the straight formula, if it's outside that range just multiply by -1.

Best bet, probably, cos of the z angle. Mutliply by the sign of the cos - that works out to be the same.

I've not tested this at all btw, the way SL converts quaternions to eulers might mean this doesn't actually work, but the maths is sound enough.
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
04-28-2005 08:17
From: Eloise Pasteur
Best bet, probably, cos of the z angle. Mutliply by the sign of the cos - that works out to be the same.


That fixes the east/west problem, but it still goes flat when facing north and south...

I'm going to try a variation, I'll let you know.

- Jon
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
04-29-2005 06:05
Still nobody with any ideas???

- Jon
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
04-29-2005 13:02
There must be a simple answer to this, but I'm just not seeing it.

I can suggest a kludgy one though -

If you're between ±45º or ±135º around z use the angle around y and the sign (±) of the cosine of the angle around z. From 45 to 135º and -45 to -135º around z use the angle around x and the sign of the sine of the sin around z.

You might have to play with the signs of these and you will probably find it jumps a bit as you swing past the 4 semi-cardinal points but it should at least give you an element of control and feedback.

It's nights like this that make me remember why I never took to 3D geometery! I have a sneaky suspicion that either quaternions give you a dead easy way to calculate it, or you've got to do something with a x product of the vectors.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
04-29-2005 14:06
This was writen by another scripter in this forum a while ago.

CODE

vector getRollPitchHeading(rotation test)
{
vector v=llRot2Fwd(test);
float heading = llAtan2(v.y, v.x);

float run=llSqrt(v.x * v.x + v.y * v.y);
float pitch = -llAtan2(v.z, run);

rotation y=llEuler2Rot(<0, pitch, 0>);
rotation z=llEuler2Rot(<0, 0, heading>);
rotation x=test / z / y;
vector tmp=llRot2Euler(x);
float roll=tmp.x;

return <roll, pitch, heading>;
}


i would make this change, because we know v is a unit vector.
CODE

float run=llSqrt(v.x * v.x + v.y * v.y);
to
float run=llSqrt(1 - v.z * v.z);
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
04-30-2005 10:27
Strife -- you got it...

I've dropped your reward into your inventory. Enjoy!

- Jon