Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Pendulum script drift problem

Tobias Convair
Registered User
Join date: 17 Nov 2007
Posts: 30
04-09-2008 17:42
I am building clocks with a scripted pendulum swing. I have used the script below, originally posted by Void... I very much like the motion of the swing. I have been able to link the pendulum to the clock by using a script in the root prim which keeps the whole stationary. The problem is that the pendulum tends to drift significantly over time, which because I intend to sell these clocks is not acceptable. Can anyone tell me why this drift takes place and give any suggestions to remedy the problem?

The swing script:


//llSetLocalRot() sets the rotation of the prim it's in (acts just like llSetRot when used in the root), and depending on the math used it can be a global axis rotation, or a local axis rotation.

//assuming a playground swing I use a root at the top point and something like this

vector gVecAngle = <0.0, 10.0, 0.0>; //-- angle and axis from center to swing limit
rotation gRotAngle;

default{
state_entry(){
gRotAngle = llEuler2Rot( gVecAngle * DEG_TO_RAD );
}

touch_start( integer vNull ){
state sMoving;
}
}

state sMoving{
state_entry(){
llSetTimerEvent( 0.1 );
llSetRot( gRotAngle * llGetRot() ); //-- swing out to edge
gRotAngle.s *= -1; //-- reverse the rotation
//-- alternate single step swing, replaces previous line
// gRotAngle.s *= -0.5; //-- doubles the swing angle and reverses it
}

timer(){
//-- remove first llSetRot statment for alternate single step swing
llSetRot( gRotAngle * llGetRot() ); //-- swing in to center
llSetRot( gRotAngle * llGetRot() ); //-- swing out to other edge
gRotAngle.s *= -1; //-- reverse the rotation
}

touch_start( integer vNull ){
llSetTimerEvent( 0.0 );
state default;
}

state_exit(){
//-- alternate extra step for single step swing
// gRotAngle.s *= 2; //-- halves the swing angle
llSetRot( gRotAngle * llGetRot() ); //-- swing in to center
}
}
Ravenhurst Xeno
Consiracy with no purpose
Join date: 20 Jan 2007
Posts: 147
04-09-2008 18:20
The easiest way to overcome the drift is to save the starting position and every X number of swings reset the pendulum to the starting position.

Here is a (messy) example of a script i did to do a something similar:



CODE

integer seconds=2;


integer gSpinRate = 1; // radians per second
float gTurnTime;
integer gDirection = 1; // osillator
integer gNbrOfTurns = 0; // Reset every so often
vector gStartRot = <0,0,0>; // starting rotation
rotation gBaseRot;
float gSpinLimit; // rotation arc in radians
vector gRotAxis = <1,0,0>;

default
{

on_rez(integer params){llResetScript();}
state_entry()
{

gSpinLimit = PI; // set how many radians it
// will rotate through here

//
// This is a simple rotation. Must rotate around one
// and only one axis
if( gRotAxis.x + gRotAxis.y + gRotAxis.z != 1 ) {
llOwnerSay("Invalid rotation axis "+(string)gRotAxis);
}

//
// Offset allowed only on the axis of rotattion
gStartRot = <
gStartRot.x * gRotAxis.x,
gStartRot.y * gRotAxis.y,
gStartRot.z * gRotAxis.z
>;
gBaseRot = llEuler2Rot(
<
gStartRot.x * DEG_TO_RAD,
gStartRot.y * DEG_TO_RAD,
gStartRot.z * DEG_TO_RAD
>
);
//
// gTurnTime is how long it should take to go through 180 degrees
gTurnTime = gSpinLimit / gSpinRate;
llSetRot( gBaseRot );
llSetTimerEvent(gTurnTime);

}


timer(){

llTargetOmega( gRotAxis, gSpinRate * gDirection , 1);
gDirection *= -1;

//
// correct for clock drift.
// 100 is just a guess of how often to reset
if( ++gNbrOfTurns > 100 ) {
llSetTimerEvent(0);
gNbrOfTurns = 0;
llSetRot( gBaseRot );
llSleep(0.1);
llSetTimerEvent(gTurnTime);
}

}
}


Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
04-09-2008 18:23
Try it in a Havok 4 sim, shouldn't be any drift there!
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Ruthven Willenov
Darkness in your light
Join date: 16 Jan 2008
Posts: 965
05-10-2008 23:04
Woot! just what i was looking for to make a swing attached to the frame, thanks!