Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Object FACING an avatar

Munsta Levasseur
Registered User
Join date: 15 Jun 2008
Posts: 2
06-17-2008 21:08
I have seen some object that face a player but sadly I couldn't figure out to do it..

It's either a simple one line command which i dont think it is since i checked them all or a math problems..

Anyway.. Does anyone could tell me how to do it or refer me to a post that has it? ( I used the search option but found nothing )

Thanks, Munsta
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-17-2008 21:40
This assumes you want the root prim's x-axis to point at the avatar. It must be called each time you want the object's rotation to be adjusted, so do it in a timer or llSensorRepeat() or listen event or something.

(NOTE: Hasn't yet been compiled; may need minor syntax fixes.)
CODE

pointAt(key avatarOrObject)
{
list details = llGetObjectDetails(avatarOrObject, [ OBJECT_POS ]);
if (llGetListLength(details) < 1)
{
return;
}
vector pos = llList2Vector(details, 0);

vector localX = llVecNorm(pos-llGetPos());
vector localY = llVecNorm(<-localX.y, localX.x, 0.0>); // z x localX
vector localZ = localX % localY;
rotation rot = llAxes2Rot(localX, localY, localZ);

if (llGetStatus(STATUS_PHYSICS))
{
llRotLookAt(rot, 0.2, 1.0);
} else
{
llSetRot(rot);
}
}
Munsta Levasseur
Registered User
Join date: 15 Jun 2008
Posts: 2
06-17-2008 21:49
Woo :) Ilu.. but being more serious I don't yet know how you did it.. but thanks :)

I was already using a sensor repeat thing but i couldnt figure how to calculate the rotation .. thanks a bunch :)
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-17-2008 22:53
I'll explain it line by line.

CODE

list details = llGetObjectDetails(avatarOrObject, [ OBJECT_POS ]);
if (llGetListLength(details) < 1)
{
return;
}
vector pos = llList2Vector(details, 0);


That bit gets the position of the object or avatar with the given key. If the object/avatar doesn't exist or isn't present, 'details' will be empty so the function does nothing and returns.

CODE

vector localX = llVecNorm(pos-llGetPos());


That gets the vector offset from the prim to the position we got earlier, and normalizes it to get a unit vector in that direction. The x-axis is usually used as the "forward" or "looking-at" direction.

CODE

vector localY = llVecNorm(<-localX.y, localX.x, 0.0>); // z x localX


That is just the equivalent of a cross-product. It finds a vector at right angles to both the global z-axis and localX, and it normalizes it again to get another unit vector. The cross product is simple because it is easy to express a cross product with one of the primary axes <1.0, 0.0, 0.0>, <0.0, 1.0, 0.0>, or <0.0, 0.0, 1.0>. You could to an explicit cross product using the % operator instead if you wanted to, but this is (probably) a little more optimal.

CODE

vector localZ = localX % localY;


That finds the third axis that makes up our local coordinate frame. It is a simple cross product, using the % operator this time. Since we know localX and localY are at right angles and are both unit vectors, the result is another unit vector and there is no need to normalize it.

CODE

rotation rot = llAxes2Rot(localX, localY, localZ);


That function gives a rotation given three unit vectors, all at right angles to each other (a coordinate frame). It is one of the most useful rotation-related functions there is. Learn to love it.

CODE

if (llGetStatus(STATUS_PHYSICS))
{
llRotLookAt(rot, 0.2, 1.0);
} else
{
llSetRot(rot);
}


That simply rotates the object to the desired rotation, using either physical movement or non-physical movement, depending on whether the object is physical or not.