Difference between revisions of "LlLookAt"

From Second Life Wiki
Jump to navigation Jump to search
m (LSL llLookAt moved to LlLookAt: removing prefix)
m (<lsl> tag to <source>)
 
(26 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{LSL_Function
{{LSL_Function
|inject-2={{Issues/SVC-1903}}
{{LSL Function/damping|damping}}
{{LSL Function/strength|strength}}
{{LSL Function/position|target|region=*}}
|func_id=105|func_sleep=0.0|func_energy=10.0
|func_id=105|func_sleep=0.0|func_energy=10.0
|func=llLookAt|p1_type=vector|p1_name=target|p2_type=float|p2_name=strength|p3_type=float|p3_name=damping
|func=llLookAt|p1_type=vector|p1_name=target|p2_type=float|p2_name=strength|p3_type=float|p3_name=damping
|func_footnote
|func_footnote=To change the position in the same manner use [[llMoveToTarget]].
|func_desc=Cause object name to point its forward axis towards '''target'''
|func_desc=Cause object to point its {{LSLP|up}} axis (positive z) towards {{LSLP|target}}, while keeping its {{LSLP|forward}} axis (positive x) below the horizon.
 
Continues to track {{LSLP|target}} until [[llStopLookAt]] is called.
|return_text
|return_text
|spec
|spec=If the object isn't physical, the settings don't seem to have any effect except the force must be > 0. For physical objects, the strength seems to be something like viscosity, not the rotating strength, so the weaker it is the faster the rotation happens. The damping value controls how fast the rotation damps out. Low values relative to the strength make it bouncy, often overshooting the target, high values sluggish. The strength and damping values seem to have no relation to the mass of the object.
|caveats
 
|caveats=*There is no guarantee that the host objects will wind up pointing at the target. Depending on the shape of the object, the strength and the damping, it may well settle out at a different rotation pointing in a different direction if the damping stops the rotation before the final position is reached.
*If the prim is not the root, then {{LSLP|target}} will need correction for the root prim's rotation (see example below).
*If the object is an attachment, then {{LSLP|target}} will need correction for the wearer's rotation.
*If the host object is physical and not symmetrical it may cause a recoil effect where the object winds up drifting away from it's original position as well as making the final rotation it settles on less accurate.
|constants
|constants
|examples
|examples=
|helpers
<source lang="lsl2">//Causes Object to look at nearest Avatar.
|also_functions=*{{LSLG|llRotLookAt}}
default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 0.2);
    }
 
    sensor(integer total_number)
    {
        llLookAt( llDetectedPos(0) + <0.0, 0.0, 1.0>, 3.0, 1.0 );
    }
}</source>
 
<source lang="lsl2">
// Same as above, but for use inside a child prim or the root of an attachment.
// Make the child or attachment look at nearest Avatar.
 
default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 0.2);
    }
    sensor(integer total_number)
    {
        vector p = llGetPos();
        llLookAt(p + (llDetectedPos(0) + <0.0, 0.0, 1.0> - p) / llGetRootRotation(), 3.0, 1.0);
    }
}
</source>
|helpers=
* If you want a (mostly) smooth, one time, constant rate of motion, (using the {{LSLP|x}} axis) in a non-physical object try this instead...
<source lang="lsl2">//-- rotate objects x axis toward vPosTarget (local offset), at vFltRate (in radians per second)
//-- vFltRate < ~0.00000003rad/sec, (~0.00002deg/sec) will result in errors (and is just too slow anyway)
//-- vFltRate >= (PI * 5.0)rad/sec, (900deg/sec) will result in a single snap move to vRotTarget
uSteppedLookAt( vector vPosTarget, float vFltRate ){
rotation vRotTarget = llRotBetween( <1.0, 0.0, 0.0>,  vPosTarget );
if ((integer)(vFltRate = llAcos( (vPosTarget = llVecNorm( vPosTarget )) *
                                (<1.0, 0.0, 0.0> * llGetLocalRot()) ) / (vFltRate / 5.0))){
rotation vRotStep = llAxisAngle2Rot( llRot2Axis( vRotTarget / llGetLocalRot() ),
                    (1.0 / vFltRate) * llRot2Angle( vRotTarget / llGetLocalRot() ) );
vFltRate = (integer)vFltRate;
do{
llSetLocalRot( vRotStep * llGetLocalRot() );
}while( --vFltRate );
}
llSetLocalRot( vRotTarget );
} //-- for fixed time on any rotation try llKeyframeMotion</source>
 
* If you want to use the LookAt function on a linked object...
<source lang="lsl2">
LinkedLookAt( vector Target){
    rotation rotvec = llRotBetween(<0,1,0>,llVecNorm((Target - llGetPos())));
    rotation rotbet = rotvec/llGetRootRotation();
    llSetRot(rotbet);
}
 
default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 1.0);
    }
 
    sensor(integer total_number)
    {
        vector p = llDetectedPos(0);
        LinkedLookAt(p);
    }
}
</source>
|also_functions=
{{LSL DefineRow||[[llRotLookAt]]}}
{{LSL DefineRow||[[llStopLookAt]]}}
|also_events
|also_events
|also_tests
|also_tests
Line 17: Line 101:
|permission
|permission
|negative_index
|negative_index
|sort=LookAt
|cat1=Physics
|cat1=Physics
|cat2=Damping
|cat2=Target
|cat3
|cat3=Rotation
|cat4
|cat4
}}
}}

Latest revision as of 11:21, 22 January 2015

Summary

Function: llLookAt( vector target, float strength, float damping );
0.0 Forced Delay
10.0 Energy

Cause object to point its up axis (positive z) towards target, while keeping its forward axis (positive x) below the horizon.

Continues to track target until llStopLookAt is called.

• vector target position in region coordinates
• float strength
• float damping seconds to critically damp in

To change the position in the same manner use llMoveToTarget.

Specification

If the object isn't physical, the settings don't seem to have any effect except the force must be > 0. For physical objects, the strength seems to be something like viscosity, not the rotating strength, so the weaker it is the faster the rotation happens. The damping value controls how fast the rotation damps out. Low values relative to the strength make it bouncy, often overshooting the target, high values sluggish. The strength and damping values seem to have no relation to the mass of the object.

Caveats

  • There is no guarantee that the host objects will wind up pointing at the target. Depending on the shape of the object, the strength and the damping, it may well settle out at a different rotation pointing in a different direction if the damping stops the rotation before the final position is reached.
  • If the prim is not the root, then target will need correction for the root prim's rotation (see example below).
  • If the object is an attachment, then target will need correction for the wearer's rotation.
  • If the host object is physical and not symmetrical it may cause a recoil effect where the object winds up drifting away from it's original position as well as making the final rotation it settles on less accurate.

Examples

//Causes Object to look at nearest Avatar.
default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 0.2);
    }

    sensor(integer total_number)
    {
        llLookAt( llDetectedPos(0) + <0.0, 0.0, 1.0>, 3.0, 1.0 );
    }
}
// Same as above, but for use inside a child prim or the root of an attachment.
// Make the child or attachment look at nearest Avatar.

default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 0.2);
    }
 
    sensor(integer total_number)
    {
        vector p = llGetPos();
        llLookAt(p + (llDetectedPos(0) + <0.0, 0.0, 1.0> - p) / llGetRootRotation(), 3.0, 1.0);
    }
}

Useful Snippets

  • If you want a (mostly) smooth, one time, constant rate of motion, (using the x axis) in a non-physical object try this instead...
//-- rotate objects x axis toward vPosTarget (local offset), at vFltRate (in radians per second)
//-- vFltRate < ~0.00000003rad/sec, (~0.00002deg/sec) will result in errors (and is just too slow anyway)
//-- vFltRate >= (PI * 5.0)rad/sec, (900deg/sec) will result in a single snap move to vRotTarget
uSteppedLookAt( vector vPosTarget, float vFltRate ){
	rotation vRotTarget = llRotBetween( <1.0, 0.0, 0.0>,  vPosTarget );
	if ((integer)(vFltRate = llAcos( (vPosTarget = llVecNorm( vPosTarget )) *
	                                 (<1.0, 0.0, 0.0> * llGetLocalRot()) ) / (vFltRate / 5.0))){
		rotation vRotStep = llAxisAngle2Rot( llRot2Axis( vRotTarget / llGetLocalRot() ),
		                    (1.0 / vFltRate) * llRot2Angle( vRotTarget / llGetLocalRot() ) );
		vFltRate = (integer)vFltRate;
		do{
			llSetLocalRot( vRotStep * llGetLocalRot() );
		}while( --vFltRate );
	}
	llSetLocalRot( vRotTarget );
} //-- for fixed time on any rotation try llKeyframeMotion
  • If you want to use the LookAt function on a linked object...
LinkedLookAt( vector Target){
    rotation rotvec = llRotBetween(<0,1,0>,llVecNorm((Target - llGetPos())));
    rotation rotbet = rotvec/llGetRootRotation();
    llSetRot(rotbet);
}

default
{
    state_entry()
    {
        llSensorRepeat("", "", AGENT, 20.0, PI, 1.0);
    }

    sensor(integer total_number)
    {
        vector p = llDetectedPos(0);
        LinkedLookAt(p);
    }
}

See Also

Functions

•  llRotLookAt
•  llStopLookAt

Deep Notes

Signature

function void llLookAt( vector target, float strength, float damping );