Fix llLookAt the right way

avinationmerge
Melanie 2012-01-23 13:48:16 +01:00
parent 02572ab1d3
commit 4e0bb49394
1 changed files with 24 additions and 19 deletions

View File

@ -3040,34 +3040,39 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
public void llLookAt(LSL_Vector target, double strength, double damping)
{
m_host.AddScriptLPS(1);
// Determine where we are looking from
LSL_Vector from = llGetPos();
// Work out the normalised vector from the source to the target
LSL_Vector delta = llVecNorm(target - from);
LSL_Vector angle = new LSL_Vector(0,0,0);
// Get the normalized vector to the target
LSL_Vector d1 = llVecNorm(target - llGetPos());
// Calculate the yaw
// subtracting PI_BY_TWO is required to compensate for the odd SL co-ordinate system
angle.x = llAtan2(delta.z, delta.y) - ScriptBaseClass.PI_BY_TWO;
// Get the bearing (yaw)
LSL_Vector a1 = new LSL_Vector(0,0,0);
a1.z = llAtan2(d1.y, d1.x);
// Calculate pitch
angle.y = llAtan2(delta.x, llSqrt((delta.y * delta.y) + (delta.z * delta.z)));
// Get the elevation (pitch)
LSL_Vector a2 = new LSL_Vector(0,0,0);
a2.y= -llAtan2(d1.z, llSqrt((d1.x * d1.x) + (d1.y * d1.y)));
// we need to convert from a vector describing
// the angles of rotation in radians into rotation value
LSL_Rotation r1 = llEuler2Rot(a1);
LSL_Rotation r2 = llEuler2Rot(a2);
LSL_Rotation r3 = new LSL_Rotation(0.000000, 0.707107, 0.000000, 0.707107);
LSL_Rotation rot = llEuler2Rot(angle);
// Per discussion with Melanie, for non-physical objects llLookAt appears to simply
// set the rotation of the object, copy that behavior
if (strength == 0 || m_host.PhysActor == null || !m_host.PhysActor.IsPhysical)
if (m_host.PhysActor == null || !m_host.PhysActor.IsPhysical)
{
llSetRot(rot);
// Do nothing if either value is 0 (this has been checked in SL)
if (strength <= 0.0 || damping <= 0.0)
return;
llSetRot(r3 * r2 * r1);
}
else
{
m_host.StartLookAt(Rot2Quaternion(rot), (float)strength, (float)damping);
if (strength == 0)
{
llSetRot(r3 * r2 * r1);
return;
}
m_host.StartLookAt(Rot2Quaternion(r3 * r2 * r1), (float)strength, (float)damping);
}
}