Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Comparing vectors

TwoSheds Flanagan
Registered User
Join date: 20 Jun 2007
Posts: 13
03-13-2008 07:06
I was asked this last night and am passing the request along so pardon any delays in relays <g>.

How can you compare the location of two objects? The obvious answer is (typed from memory at work so there's a good chance for typos and syntax errors that weren't happening in SL):
From: someone

vector first = <1.0,1.0,1.0>;
vector second = <1.0,2.0,1.0>;
if(first > second)
{
llOwnerSay("Greater";);
}
else if(first < second)
{
llOwnerSay("Less";);
}
else
{
llOwnerSay("equal";);
}


The trouble is that LSL pops up a very persistent syntax error in the IF lines. We both hunted around in the usual places looking for anything that definitively stated that you could or could not use an IF command with vectors and both came up empty.

So is it possible to compare vectors using IF, and if not is there a work around of some sort?

Thanks a ton.
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-13-2008 07:16
Saying one vector is bigger or smaller than another doesn't really mean anything.

It's like asking if one point in space is bigger or less than another point in space. You can tell if they are equal, or not equal, but not bigger or smaller.

So you should be able to say something like:

vector first = <1.0,1.0,1.0>;
vector second = <1.0,2.0,1.0>;
if (first == second)
{
llOwnerSay("Equal";);
}
else
{
llOwnerSay ("Not Equal";);
}

(with the caveat that I'm not able to test this right now)

Alternatively, you can compare the magnitude of the vectors, but depending on what you're doing this may or may not be what you want.
_____________________
TwoSheds Flanagan
Registered User
Join date: 20 Jun 2007
Posts: 13
03-13-2008 07:36
This is for a game of some sort. She's got a central point, call it the first vector, then an object which moves around that point. At the end of the game who ever got it the closest to the central point wins. The trick is figuring out how far the moving object is from the center point at each turn, then comparing it to the previous closest point.

And reading that back it doesn't make a lot of sense to me so if it's murky to you as well I'll see if she can get me more information.
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
03-13-2008 07:48
If she gets the postion of both the moving object and the central object as vectors, the distance between them is given by the llVecDist function. Store the result for each competitor, and whoever has the smallest at the end got the moving point closest to the central point.
_____________________
TwoSheds Flanagan
Registered User
Join date: 20 Jun 2007
Posts: 13
03-13-2008 07:51
I just thought of searching on the word "distance" rather than compare and found that. I'll pass it on and hopefully we can all put this nightmare behind us <g>

Thanks for the help.