Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Wandering prims don't look where they're going?

Paul Wardark
Wait, what?
Join date: 10 Jan 2009
Posts: 383
07-12-2009 01:00
I've got a couple scripts for physical objects. One follows my camera, one follows me, and one roams randomly. I really like the random roaming one. But I'm having this problem.

The script I use to make the object wander around, it doesn't TURN the prim to face which way it's going. So when I put it in my drone, my drone won't turn as it moves. It continues to look whatever direction it's facing when rezzed- or worse, spins wildly if it's bumped. Makes it look really stupid. Anyone know a fix for this? Here's the script, by the way.

CODE


float radius = 10.0; //distance from starting point to wonder
float timeout = 7.0; //time between movement changes
float max_dist = 7.0; //max distance to move on x/y axis per timeout

vector start; //starting location, found on start/rez

//randomly move somewhere within the defined circle
random_move(){
vector pos; //new position
float dist; //distance from center

//calculate the new position
do{
//start with the current position
pos = llGetPos();
//randomly update x/y cords
pos.x += llFrand(max_dist * 2)-max_dist;
pos.y += llFrand(max_dist * 2)-max_dist;
//pos.z += llFrand(max_dist * 2)-max_dist;
//calculate dist = sqrt( diff(x)^2 + diff(y)^2 )
dist = ((pos.x - start.x) * (pos.x - start.x)) +
((pos.y - start.y) * (pos.y - start.y)) +
//((pos.z - start.z) * (pos.z - start.z));
dist = llSqrt(dist);

//if we are outside of the circles radius try again
}while(dist > radius);

//move to the new point
llMoveToTarget(pos,timeout);
}

default{
state_entry(){
//get starting point and init timer
start = llGetPos();
llSetTimerEvent(timeout);
//move the object
random_move();
}
on_rez(integer i){
//get starting point and init timer
start = llGetPos();
llSetTimerEvent(timeout);
//move the object
random_move();
}
timer(){
//move the object
random_move();
}
}

_____________________
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
Look where you are going
07-12-2009 02:16
It can be done in many ways, this is one of the more simple.
I added one line to your function:
CODE

random_move(){
vector pos; //new position
float dist; //distance from center

//calculate the new position
do{
//start with the current position
pos = llGetPos();
//randomly update x/y cords
pos.x += llFrand(max_dist * 2)-max_dist;
pos.y += llFrand(max_dist * 2)-max_dist;
//pos.z += llFrand(max_dist * 2)-max_dist;
//calculate dist = sqrt( diff(x)^2 + diff(y)^2 )
dist = ((pos.x - start.x) * (pos.x - start.x)) +
((pos.y - start.y) * (pos.y - start.y)) +
//((pos.z - start.z) * (pos.z - start.z));
dist = llSqrt(dist);

//if we are outside of the circles radius try again
}while(dist > radius);

// turn to new point
llSetRot( llRotBetween( < 1.0, 0.0, 0.0 >, llVecNorm(pos-llGetPos())));
//move to the new point
llMoveToTarget(pos,timeout);
}
The argument for llRotBetween will turn 'Fwd', the positive X-axis, against the position.
Use: < 0.0, 1.0, 0.0 > for the Y-axis and
use: < 0.0, 0.0, 1.0 > for the Z-axis.

If your object is tumbling around all axis when it bumps, use:
llSetStatus( STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE );
to allow rotation around the Z-axis only.
_____________________
From Studio Dora
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
Why not use llVecDist
07-12-2009 02:52
CODE

random_move(){
vector pos; //new position
float dist; //distance from center

//calculate the new position
do{
//start with the current position
pos = llGetPos();
//randomly update x/y cords
pos.x += llFrand(max_dist * 2)-max_dist;
pos.y += llFrand(max_dist * 2)-max_dist;
//pos.z += llFrand(max_dist * 2)-max_dist;
//calculate dist = sqrt( diff(x)^2 + diff(y)^2 )
//if we are outside of the circles radius try again
}while( llVecDist( pos, start ) > radius);

// turn to new point
llSetRot( llRotBetween( < 1.0, 0.0, 0.0 >, llVecNorm(pos-llGetPos())));
//move to the new point
llMoveToTarget(pos,timeout);
}
Just a thought and it has nothing to do with turning the object:)
_____________________
From Studio Dora
Paul Wardark
Wait, what?
Join date: 10 Jan 2009
Posts: 383
07-12-2009 03:35
Maybe I'm doing something wrong, but I put in your modified code, and it doesn't seem to make a difference. The thing still isn't facing the direction it's traveling in. Still just swings around randomly.
_____________________
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
07-12-2009 04:00
From: Paul Wardark
Maybe I'm doing something wrong, but I put in your modified code, and it doesn't seem to make a difference. The thing still isn't facing the direction it's traveling in. Still just swings around randomly.
That is because llSetRot does not work on physical objects:)
Try this instead:
llRotLookAt( llRotBetween( < 1.0, 0.0, 0.0 >, llVecNorm(pos-llGetPos())), 0.5, 0.5);
_____________________
From Studio Dora
Paul Wardark
Wait, what?
Join date: 10 Jan 2009
Posts: 383
07-12-2009 11:14
Hey, that seemed to get it working, now that I've toyed with it a little. Awesome.
_____________________
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
07-12-2009 11:29
From: Paul Wardark
That seemed to do the trick, which is awesome, except adding the rotation limit suddenly makes it stop working again.
If by rotation limits you mean:
llSetStatus( STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE );
Forget it:)
I have the script running on my building site without that and there are no problems.
The line:
llRotLookAt( llRotBetween( < 1.0, 0.0, 0.0 >, llVecNorm(pos-llGetPos())), 0.5, 0.5);
not only turn the prim in the moving direction it also stops any rotation it might get from bumping into other objects.
_____________________
From Studio Dora