Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Object flying around a central point within a radius

Itchy Stiglitz
Mostly Harmless
Join date: 24 Apr 2006
Posts: 23
05-31-2006 08:37
Has anyone had luck getting an object to fly around a centeral point within a radus?
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
05-31-2006 08:49
I think a little more information is required, are you looking to make something which follows a path on the surface of a sphere (constant radius), or is restricted to move within a sphere (r = 0 to radius)?

If you wish something to spin about an axis you might be best using llTargetOmega (see http://secondlife.com/badgeo/wakka.php?wakka=llTargetOmega ) although this requires that you have the root prim at the centre of rotation and linking distances may be a problem for large radii.

Perhaps you're using a llSetPos movement? If so a check along the lines of:
CODE

vector difference = NextPosition - CentreOfSphere;
if ( llVecMag(difference) <= radius )
{
llSetPos(NextPosition);
}


Post some more details of your problem and we'll try and assist.
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
05-31-2006 09:20
If not llTargetOmega, you're probably going to need to use sines and cosines to get the locations of the circle. I made something like this, I'll post the code when SL comes back online.
Itchy Stiglitz
Mostly Harmless
Join date: 24 Apr 2006
Posts: 23
05-31-2006 09:44
What I have is a cube that flys up and says hi when an av comes near. I would like it to fly around in a circle around it's current location sort of like a loop/hover, to make it seem more playful. So my central point would be it's current location in space or a set distance from it's current point. I could use an invisable sphere as my central point but I wonder if there is a cleaner way?
Mendacio Montgomery
Registered User
Join date: 12 May 2006
Posts: 13
05-31-2006 09:55
Itchy,

Ok here's the math in pseudo-code...

vector origin = <X, Y, Z>; // provide your origin coords here
float radius = R; // provide your radius here
float theta = 0;
float thetaIncr = (2*PI / n); // choose n as the number of steps in the circle

doit()
{
...while (orbiting)
...{
......for (theta = 0; theta < (2*PI); theta += thetaIncr)
......{
.........float x = R * cos(theta);
.........float y = R * sin(theta);

.........vector newPos = origin + <x, y, 0>;

.........travelTo(newPos);
......}
...}
}

travelTo(newPos)
{
...while(objectPos != newPos)
...{
......moveObjectTo(destination); // over time or instantaneously or whatever
...}
}
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
05-31-2006 10:10
or you can use the formula for a sphere, x^2 + y^2 + z^2 = r^2. where r is the radius.. up to you if you want to use geometry or trig.
Cross Lament
Loose-brained Vixen
Join date: 20 Mar 2004
Posts: 1,115
05-31-2006 10:30
I did this sort of thing by choosing an offset from the rotation point (say <1, 0, 0> for a one-meter circle), and just using a timer to multiply that by an incrementing rotation, setting the new position with llMoveToTarget().

Something like this (though I haven't tested it in-world since the grid is down):
CODE
// What axis do you want to rotate around?
vector rotation_axis = < 0, 0, 1 > ;

// Where do you want to be relative to the center of rotation?
vector targetoffset = < 1, 0, 0 > ;

vector targetpos ;
float rotstep ;
float rotcount = 0 ;

default
{
on_rez( integer num )
{
llStopMoveToTarget() ;
llSetTimerEvent( 0.0 ) ;
targetpos = llGetPos() ;
targetpos.z += 2.0 ; // Get me off the ground :)
llSetTimerEvent( 1.0 ) ;
}

state_entry()
{
llSetStatus( STATUS_PHYSICS, TRUE ) ;
rotstep = PI / 6.0 ; // Full rotation every 12 seconds.
}

timer()
{
rotcount += rotstep ;
if( rotcount > TWO_PI ) rotcount = 0.0 ;

llMoveToTarget( targetpos + ( targetoffset * llEuler2Rot( rotation_axis * rotcount ) ), 1.0 ) ;
}
}

This is, of course, a version using Physics. A non-physical version should function almost exactly the same, though. :)
_____________________
- Making everyone's day just a little more surreal -

Teeple Linden: "OK, where did the tentacled thing go while I was playing with my face?"
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
05-31-2006 11:12
Here's my code. It will make the object rotate around the owner.

CODE

float dist = 2;
float angle;
float step;

vector offset(float theta)
{
return <llSin(theta)*dist,llCos(theta)*dist,0>;
}

default
{
on_rez(integer sparam)
{
llResetScript();
}

state_entry()
{
step = TWO_PI / 10;
llSetStatus( STATUS_PHYSICS | STATUS_PHANTOM, TRUE );
llSetStatus( STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z, FALSE );
llSensorRepeat("",llGetOwner(),AGENT,30,PI,0.5);
}

sensor(integer num)
{
llMoveToTarget(llDetectedPos(0)+offset(angle)+<0,0,1>, 1);
angle += step ;
if ( angle > TWO_PI )
angle -= TWO_PI;
}
}
AJ DaSilva
woz ere
Join date: 15 Jun 2005
Posts: 1,993
05-31-2006 13:05
From: nand Nerd
If you wish something to spin about an axis you might be best using llTargetOmega (see http://secondlife.com/badgeo/wakka.php?wakka=llTargetOmega ) although this requires that you have the root prim at the centre of rotation and linking distances may be a problem for large radii.
If I understood what you want properly, this is easily the best way of doing it if you can spare the prim to go in the center.
_____________________
Itchy Stiglitz
Mostly Harmless
Join date: 24 Apr 2006
Posts: 23
06-01-2006 08:15
Thank you all for the help. This goes a long way toward me understanding how LS works. You guys are awesome!