Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

"Look At" Scripting Help

Ody Naglo
Registered User
Join date: 1 Apr 2008
Posts: 31
08-27-2008 12:34
Im currently using this script below on my Gun Turret. I want it to look at the closest Avatar. The Turret keeps twitching downwards into the ground once in a wile. no clue why its doing it... help =)



// AXIS_* constants, represent the unit vector 1 unit on the specified axis.
vector AXIS_UP = <0,0,0>;
vector AXIS_LEFT = <5,0,-.50>;
vector AXIS_FWD = <0,0,0>;

// getRotToPointAxisAt()
// Gets the rotation to point the specified axis at the specified position.
// @param axis The axis to point. Easiest to just use an AXIS_* constant.
// @param target The target, in region-local coordinates, to point the axis at.
// @return The rotation necessary to point axis at target.
// Created by Ope Rand, modifyed by Christopher Omega
rotation getRotToPointAxisAt(vector axis, vector target)
{
return llGetRot() * llRotBetween(axis * llGetRot(), target - llGetPos());
}


// Strength and damping are values used to control how llRotLookAt and llLookAt move, these values are tunable.
float strength = 1.0;
float damping = 1.0;

default
{
state_entry()
{
llSay(0, "Ready For Operation";);

}

link_message(integer sender, integer num, string msg, key id)
{

if (num == 89769)
{
llSensorRepeat( "", "",AGENT,96, PI,0.2);
}


if (num == 98769)
{
llSensorRemove();
llSetText("",<1,1,1>,1);

}

}

sensor(integer sense)
{

vector target = llDetectedPos(0); // A vector to look at.

// These two lines are equivalent, and point the up (Z) axis at the target:
llRotLookAt(getRotToPointAxisAt(AXIS_UP, target), strength, damping);
llLookAt(target, strength, damping);

// This line points the fwd (X) axis at the target:
llRotLookAt(getRotToPointAxisAt(AXIS_FWD, target), strength, damping);

// This line points the left (Y) axis at the target:
llRotLookAt(getRotToPointAxisAt(AXIS_LEFT, target), strength, damping);
llSetText("Tracking:" + llDetectedName(0),<1,1,1>,1);

}

no_sensor()
{
}





}
arton Rotaru
Registered User
Join date: 27 Aug 2007
Posts: 43
08-27-2008 16:20
Comment out the Line:

llLookAt(target, strength, damping);

LookAt will always track the Z axis towards target.
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
08-27-2008 16:37
Nice catch and welcome to the scripting forums arton!
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
arton Rotaru
Registered User
Join date: 27 Aug 2007
Posts: 43
08-27-2008 16:58
Thank you Jesse! :-)
Ody Naglo
Registered User
Join date: 1 Apr 2008
Posts: 31
08-27-2008 17:56
Sorry that didnt work all it did was diable an axis and now when it turns around it slowly goes upside down the more times u go around it makes it worse.


All 3 axis need to stay moving =)
Ody Naglo
Registered User
Join date: 1 Apr 2008
Posts: 31
08-27-2008 21:24
I fixed it i had to Comment out


// llRotLookAt(getRotToPointAxisAt(AXIS_UP, target), strength, damping);
&
// llRotLookAt(getRotToPointAxisAt(AXIS_FWD, target), strength, damping);

Thanks for trying 2 help =)
arton Rotaru
Registered User
Join date: 27 Aug 2007
Posts: 43
08-27-2008 22:54
Ok, tested it inworld. There are still all three axis. The difference between LooAt And RotLookAt are that LookAt points it's Z axis and RotLookAt it's X axis towards the taget. So i guessed the LookAt would move it downwards, if you point the root prim Z axis up.
But in this case, it looks like LookAt is needed to let it stay upside up.

For me it worked just fine inworld.
Thickbrick Sleaford
Baffled
Join date: 28 Dec 2006
Posts: 26
08-28-2008 09:42
The reason the script acts weirdly is that there are too many lookAt lines, trying to do conflicting things, and the AXIS_* variables were set to non-unit vector values.

Anyway, here's a way to get around the difference between llLookAt and llRotLookAt.

This script will always keep the object with the same (Z+) side facing up (or at least above the horizon) and the same (X+) side facing the target.

Also, does anybody know if there's a difference between llSetRot and llRotLookAt for non-physical objects? I can't see any difference, but the wiki says there is one.

CODE

//cache this global variable to not have to compute it again and again
rotation rot90OnZ;
// Strength and damping are values used to control how llRotLookAt and llLookAt move, these values are tunable.
float strength = 1.0;
float damping = 1.0;

default
{
state_entry()
{
//first save a rotation we'll be usign later:
rot90OnZ = llEuler2Rot(<0,0,PI_BY_TWO>);
llSay(0, "Ready For Operation");
llSensorRepeat( "", "",AGENT,96, PI,0.2);
}

sensor(integer sense)
{
vector target = llDetectedPos(0); // A vector to look at.
target = target - llGetPos(); //make it local
//the direction of the target, normalized:
vector wantedFwd = llVecNorm(target);
//taking the horizontal component of the target vector,
//and rotating it 90 degrees around Z to get a horizontal
//vector pointing pependicular to the target:
vector wantedLeft=llVecNorm(<target.x, target.y, 0.0>) * rot90OnZ;
//create a global rotation that would satisfy these 2 vectors
rotation rot = llAxes2Rot(wantedFwd, wantedLeft, wantedFwd % wantedLeft);
//make the object look at the rotation:
llRotLookAt(rot, strength, damping);
//llSetRot(rot);
}
}

Ody Naglo
Registered User
Join date: 1 Apr 2008
Posts: 31
08-28-2008 13:16
From: Thickbrick Sleaford
The reason the script acts weirdly is that there are too many lookAt lines, trying to do conflicting things, and the AXIS_* variables were set to non-unit vector values.

Anyway, here's a way to get around the difference between llLookAt and llRotLookAt.

This script will always keep the object with the same (Z+) side facing up (or at least above the horizon) and the same (X+) side facing the target.

Also, does anybody know if there's a difference between llSetRot and llRotLookAt for non-physical objects? I can't see any difference, but the wiki says there is one.

CODE

//cache this global variable to not have to compute it again and again
rotation rot90OnZ;
// Strength and damping are values used to control how llRotLookAt and llLookAt move, these values are tunable.
float strength = 1.0;
float damping = 1.0;

default
{
state_entry()
{
//first save a rotation we'll be usign later:
rot90OnZ = llEuler2Rot(<0,0,PI_BY_TWO>);
llSay(0, "Ready For Operation");
llSensorRepeat( "", "",AGENT,96, PI,0.2);
}

sensor(integer sense)
{
vector target = llDetectedPos(0); // A vector to look at.
target = target - llGetPos(); //make it local
//the direction of the target, normalized:
vector wantedFwd = llVecNorm(target);
//taking the horizontal component of the target vector,
//and rotating it 90 degrees around Z to get a horizontal
//vector pointing pependicular to the target:
vector wantedLeft=llVecNorm(<target.x, target.y, 0.0>) * rot90OnZ;
//create a global rotation that would satisfy these 2 vectors
rotation rot = llAxes2Rot(wantedFwd, wantedLeft, wantedFwd % wantedLeft);
//make the object look at the rotation:
llRotLookAt(rot, strength, damping);
//llSetRot(rot);
}
}




You Rock!! you just ended my 4 days of trying to get this script to work I could hug you right now =) I sent you a gift in game its one of the items I build =P


I sent you something also arton Rotaru for trying =)