|
Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
|
11-22-2006 03:22
Is it unadvisable to fire a sensor every 0.1 secs? I'm a LSL newbie using some code from the KanEd tutorials to make an object orbit around a point when an avatar triggers a sensor. However, that tutorial used a for loop to force the update of llSetPos(). I want it to happen continuously while an avatar is within range of the sensor. After playing with a while loop, which made rather chunky motion, I tried putting it in the sensor function, which gives smooth motion providing the sensor is firing every 0.1 secs. This seems like an excellent solution along the lines of ActionScript's onEnterFrame, but I wan to make sure that its not going to cause performance problems down the track. Also, if I want to have a set of objects orbiting concentrically all at different speeds and different distances from the centre, would it be best to link them all and control them from this script in the root prim? If so, how would I do that? integer NOBODY = TRUE; vector startPoint; vector rotationCenter;
default{ state_entry(){ llSensorRepeat("",NULL_KEY,AGENT,15,PI,0.1); startPoint = llGetPos(); rotationCenter = startPoint + < 3, 0, 0 >; } sensor(integer total_number){ if(NOBODY){ NOBODY = FALSE; llSay(0, "Avatar Detected Using Sensor"); }
rotation Z_15 = llEuler2Rot( < 0, 0, 3 * DEG_TO_RAD > ); vector currentPosition = llGetPos(); vector currentOffset = currentPosition - rotationCenter; vector rotatedOffset = currentOffset * Z_15; vector newPosition = rotationCenter + rotatedOffset; llSetPos( newPosition ); } no_sensor(){ if(!NOBODY){ NOBODY = TRUE; llSay(0, "No Avatar In Range Of Sensor"); llStopSound(); llSetPos(startPoint); } } }
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
11-22-2006 03:31
I'd be inclined to just use llTargetOmega within a linked set and leave it running rather than bother with sensors.
|
|
Adam Ramona
Registered User
Join date: 5 Jan 2005
Posts: 56
|
11-22-2006 03:45
But can I offset the centre of rotation using llTargetOmega? From the wiki, I suspect not? I'm trying to create a set of objects all orbiting around the same centre point.
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
11-22-2006 03:54
From: Adam Ramona But can I offset the centre of rotation using llTargetOmega? From the wiki, I suspect not? I'm trying to create a set of objects all orbiting around the same centre point. Thats why I said use a linked set, but yes for multiple speeds would require multiple colocated root items. If you want to stick to your current llSetPos idea, Rather than using a repeating sensor try using Volume Detect with a timer. I think it is slightly (Anyone got the figures to hand?) less laggy than using a sensor.
|
|
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
|
11-22-2006 08:38
From: Adam Ramona Is it unadvisable to fire a sensor every 0.1 secs? It can be quite reckless and not the nicest thing to do, indeed  If the concern is on motion appearing smooth, a sort of work-around could be to fire the sensor at slower rate, and interpolate position of the marker (or whatever it is) between readouts. This will introduce slight 'lag' in the position of marker if the AV is moving around, but that doesn't necessarily have to be a bad thing depending on effect you're after. // pseudocode float move_phase = 0.0; vector pos_last; vector pos_target; vector pos_start; key target_key = NULL_KEY; float tick_rate = 0.2;
default{
state_entry() {
llSensorRepeat( "", target_key, AGENT, 15, PI, 2.5 ); // idle mode scan frequency pos_last = pos_target = pos_start = llGetPos(); }
sensor( integer Contacts ) {
pos_last = llGetPos(); pos_target = llDetectedPos(0); target_key = llDetectedKey(0); move_phase = 0.0; llSetTimerEvent( tick_rate ); // clumsy to put it here like that but oh well }
no_sensor() {
pos_last = llGetPos(); pos_target = pos_start; target_key = NULL_KEY; move_phase = 0.0; }
timer() {
vector current = (pos_target * move_phase) + (pos_last * ( 1.0 - move_phase )); // do whatever you want with the calculated current position, then... move_phase += tick_rate; if( move_phase == 1.0 ) { // time to look for target again llSensor( "", target_key, AGENT, 15, PI ); } else if( move_phase > 1.0 ) { // cap the interpolation move_phase = 1.0; if( (pos_target == pos_start) && (target_key == NULL_KEY) ) { // enter again the idle mode, if applicable llSensorRepeat( "", target_key, AGENT, 15, PI, 2.5 ); llSetTimerEvent( 0.0 ); } } } }
edit: also on second thought, llSetPos() has built-in delay of 0.2 second, so if you're using it in the main script there isn't much reason to fire sensor or do anything at rate higher than that, it'll just add backlog to event queue...
|
|
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
|
About for loops
11-22-2006 09:13
One possible reason the for loop appears jerkey is that ... well, it isn't smooth.
Each time you enter the for loop, get the time, and then adjust the object to the position it would be at that time.
You can slow the loop down a little with a sleep.
|