Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Converting vector normal to Cardan angles

Drene Skytower
Registered User
Join date: 16 Sep 2008
Posts: 1
09-16-2008 12:07
I'm trying to write a script that changes a normalized vector to to (x,y,z) Cardan angles, but I can't find anywhere on the internet a good way to do this.

EDIT:
Alright now I came up with some suitable code that answers my own question :)

// drop this in a prim to make the prim face you
default
{
state_entry()
{
// Look for owner within 50 meters in 360 degree arc every 1 seconds.
llSensorRepeat("", llGetOwner(), AGENT, 50.0, PI, 1.0);
}

sensor(integer total_number)
{
// Get position of detected owner
vector pos = llDetectedPos(0) + <0,0,1>;

vector origin = llGetPos();

// normalizing the vector simplifies the trig
vector axis = llVecNorm(<pos.x - origin.x, pos.y - origin.y, pos.z - origin.z>;);

vector sight;
// X Angle
if(axis.x > 0.0)
sight.x = 0.0;
else
sight.x = PI;

// Y Angle
sight.y = llAsin(axis.z);
if(axis.x > 0.0)
{
sight.y = (2 * PI) - sight.y;
}
else
{
sight.y += PI;
if(sight.y > 2 * PI)
sight.y -= 2 * PI;
}

// Z Angle
sight.z = llAtan2(axis.y, axis.x);
if(axis.x < 0.0)
{
sight.z += PI;
if(sight.z > 2 * PI)
sight.z -= 2 * PI;
}

// you can rotate the "face" of the prim looking at you by this many radians
float faceRotation = 0.0;

// add rotations using '*' in the order of X, then Y, then Z
llSetRot( llEuler2Rot(<sight.x + faceRotation, 0, 0>;) * llEuler2Rot(<0, sight.y, 0>;) * llEuler2Rot(<0, 0, sight.z>;));
}
}
Pedro McMillan
SLOODLE Developer
Join date: 28 Jul 2007
Posts: 231
09-16-2008 17:36
Not sure I understand what you mean by "Cardan angles". Do you mean a "polar vector" (i.e. angle and magnitude)?

EDIT: aha, it's synonymous for "Euler angles" (that's the terminology used in LSL). Still not quite sure what you're actually trying to achieve though... can you give an example of how you might use it?