Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Working with vector coordinates

Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
03-15-2005 17:57
Please help. I suck at math. ^^;

I have a start vector (a prim's current position) and an end vector (where I want the prim to go). I need some logic that will move the prim in the right direction in 60 meter increments until it reaches the destination. I'm using llMoveToTarget, thus the 60 meter limitation.

Can someone help me create some logic to do that? It needs to be smart enough to work regardless of the start and end positions (within the boundary of a sim, I suppose).
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-15-2005 18:42
CODE
vector end = <0,0,200>; // Set end here
float speed = 3; // 3m moves every 0.5 seconds

while(llVecMag(llGetPos() - end) > 10)
// While we're more than 10m from target
{
llMoveToTarget(llGetPos() + (speed * llVecNorm(end - llGetPos())),0.3);
llSleep(0.5);
}
// We're close enough now
llMoveToTarget(end,0.3);

That's a rough way to go about it. Note I just wrote that out-of-world; it might have a bug or two. :)
_____________________
---
Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
03-15-2005 23:53
Thanks Jeffery. It works great!
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
03-16-2005 04:35
From: Jeffrey Gomez
CODE

while(llVecMag(llGetPos() - end) > 10)

Isn't this the same as llVecDist(llGetPos(), end) ?
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-16-2005 14:15
Yes Eggy, and so is:

CODE
vector end = <0,0,120>;
vector here = llGetPos();

vector test = end - here;
float mag = llSqrt(llPow(test.x,2) + llPow(test.y,2) + llPow(test.z,2));

Moral of the story: LL functions usually save work no matter which way you go about using them. ;)
_____________________
---
Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
03-19-2005 12:23
Okay here's my second question:

Now that I have my prim moving in the right direction, I'd like to rotate the object on its Z axis to face the destination before it takes off. Furthermore, I need to make sure the SitTarget of the Avatar sitting on the prim is facing the same direction.

Any suggestions?
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
03-19-2005 14:00
From: Harris Hare
Okay here's my second question:

Now that I have my prim moving in the right direction, I'd like to rotate the object on its Z axis to face the destination before it takes off. Furthermore, I need to make sure the SitTarget of the Avatar sitting on the prim is facing the same direction.

Any suggestions?


I have a sensor script posted in the script library that uses this function:
CODE

// AXIS_* constants, represent the unit vector 1 unit on the specified axis.
vector AXIS_UP = <0,0,1>; // Z axis.
vector AXIS_LEFT = <0,1,0>; // Y axis.
vector AXIS_FWD = <1,0,0>; // X axis.

// 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.
rotation getRotToPointAxisAt(vector axis, vector target)
{
return llGetRot() * llRotBetween(axis * llGetRot(), target - llGetPos());
}


I need to make it more obvious, it seems as though lots of people need this :)
Just call llSetRot(getRotToPointAxisAt(AXIS_UP, targetPosition)); to set the object's z-axis pointed at the target.

The sit target might pose a problem though... do you want the *user's face* facing the target, or the user's head pointing at the target?
==Chris
_____________________
October 3rd is the Day Against DRM (Digital Restrictions Management), learn more at http://www.defectivebydesign.org/what_is_drm
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-19-2005 15:03
Chris, you dunce. No excuse - you even have that script on this page: :D

http://secondlife.com/badgeo/wakka.php?wakka=llLookAt

And, because llLookAt rotates along a predictable axis (with Z pointing toward the target, I believe), it gives a nice, static sit target too.
_____________________
---
Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
03-19-2005 20:21
From: Jeffrey Gomez
Chris, you dunce. No excuse - you even have that script on this page: :D
Hey..cut me some slack! =)

I had seen that script but had some trouble making it work for me. For one, when an avatar sat on my prim using llLookAt() it tended to screw up the AV's camera after they got off. Second, it didn't rotation only on the Z axis. Here's what I ended up doing:

vector tempend = target; // create a temp vector equal to the target vector
vector temppos = llGetPos(); // create a temp vector equal to the current pos
tempend.z = temppos.z; // make the z value equal
rotation temprot = llRotBetween(<1,0,0>, tempend - llGetPos());
llSetRot(temprot);
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-19-2005 21:20
From: Harris Hare
Hey..cut me some slack! =)

The other Chris. :p

And I use llLookAt for my trolley script, which I set up in a couple sims. It appeared to work fine with the camera, but I could be mistaken. :o
_____________________
---
Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
03-21-2005 13:38
From: Jeffrey Gomez
I use llLookAt for my trolley script, which I set up in a couple sims. It appeared to work fine with the camera, but I could be mistaken. :o
You may not notice the effects if the z rotation of the avatar stays the same, such as sitting on a seat parallel to the ground. But if you have a prim looking upwards or downwards at an odd angle using llLookAt() and an avatar sits then unsits the the prim, their camera continues to try to rotate on that same strange angle until they sit on something level again.
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-21-2005 22:32
From: Harris Hare
You may not notice the effects if the z rotation of the avatar stays the same, such as sitting on a seat parallel to the ground. But if you have a prim looking upwards or downwards at an odd angle using llLookAt() and an avatar sits then unsits the the prim, their camera continues to try to rotate on that same strange angle until they sit on something level again.

Yes, and this is a bug. You can fix it by going into and out of Mouselook.
_____________________
---