Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Newbie needs to create random motion of Prims

Alfredo Overland
Registered User
Join date: 19 Mar 2009
Posts: 5
04-20-2009 10:51
Hi all: I'm new so please forgive me if I am asking something too simple. I am a professor of Botany and trying to develop educaitonal materials for teaching. I need to figure out how to write a simple script that will animate a prim (a molecule) with random motion. The motion I need to create should be like a "wiggle in place". The prim should wiggle up and down slightly, should rotate a little here and there, and should sway around its center a little. If the prim is a sphere of 0.1 meter in diameter, the random motion should all occur within about 1 diameter of the sphere. This random motion once initiated by touching, should continue until it is turned off by the user or for about 2 minutes, whichever comes first. Once I figure out how to do this with one sphere, I will make an animation that contains 20 or 30 spheres, all doing their own version of a random vibration in place.
Thanks!
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
04-20-2009 11:15
No matter how you do this, you'll have to be very careful or your demo will generate a huge amount of lag in the sim. A Brownian motion script in 20 or 30 molecules is going to take up a lot of time. You might want to use the Search This Forum function at the top of this forum to look at archived posts with the key word "Brownian" to get a feel for the concern.

Anyway... You ought to be able to write a script that uses llApplyImpulse to give your small sphere a little shove in some direction and then, when it collides with another object, gives it another llApplyImpulse in a randomly chosen direction. Make the sphere physical and put it in an invisible non-phantom box. When the sphere hits a wall of the box, it should take off in a random direction until it hits the box or another sphere. Take a look at .
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Alfredo Overland
Registered User
Join date: 19 Mar 2009
Posts: 5
04-20-2009 11:24
I am aware of the overhead problems you mention. Becasue of that I want to keep the simulation simple. I'm not looking for any collision detection. All I want is for each of the molecules to wiggle in place as I mentioned. There should be no need for a push or shove of the molecule. Each molecule should start to wiggle based on a script, and each molecule should have its own random motion.
Alfredo
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-20-2009 12:06
for a static display it shouldn't be too much of an issue, as long as there aren't too many running at once. something along the lines of a touch start/stop that records it's initial position, and applies small random motions and rotations around that initial position. combining several into a linked object (they need not touch) could probably allow you to take advantage of a single timer loop to reduce overall script time. although I'm not sure overall how fast the motion would run

ETA:
you could also probably cheat and have several of molecules mimic each other in randomly dispersed groups to maintain the overall effect with a little less overhead
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
04-20-2009 12:18
I'm wondering whether there's a way to induce a Brownian-type wiggle by forcing objects within their collision spheres. I can't get in world to try this (stupid little laptop) but I wonder if you could rez a hollow sphere, make it physical and transparent, and then rez a smaller sphere inside it. Make the second sphere physical too. Then shrink the outer sphere until it's small enough that the hollow is smaller than the collision sphere of the smaller one. Would the outer sphere keep trying to repel the inner one and make it rattle around randomly inside? Just wondering ..... I wish I could try it.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
04-20-2009 12:24
Both suggestions above are good, physical will most likely be your best bet if you want it to look smooth. There is a 0.2 second delay with non-physical llSetPos movement, which you may not want. applying the slight impulse could work, or you might look at llMoveToTarget, which may simulate what you would like done. Both physical and non-physical have their up and down sides, for instance, edit a physical object by anyone, will stop it's motion for everyone looking at the object, and for non-physical it's the 0.2 second delay.
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
04-20-2009 12:28
As is said above, llMoveToTarget will be a lot smoother..

If you don't need that, this (totally untested) script might get you started:

CODE

float MAX_TIME = 120.0;
float UPDATE_TIME = 2.0;
float MAX_DISTANCE = 2.0;
integer CHANNEL = 2345;

vector gOrigPosition;
integer gRunning;

default
{
state_entry()
{
gOrigPosition = llGetPos();
gRunning = FALSE;
llListen (CHANNEL, "", llGetOwner(), "");
}

on_rez(integer param)
{
llResetScript();
}

changed (integer mask)
{
if (mask & CHANGED_OWNER)
{
llResetScript();
}
}

listen(integer channel, string name, key id, string message)
{
if (message == "reset")
{
llSetPrimitiveParams([PRIM_POSITION, gOrigPosition, PRIM_ROTATION, ZERO_ROTATION]);
llResetScript();
}
else if (message == "start")
{
gRunning = TRUE;
llResetTime();
llSetTimerEvent(UPDATE_TIME);
}
else if (message == "stop")
{
gRunning = FALSE;
llSetTimerEvent(0.0);
}
}

touch_start(integer count)
{
gRunning = !gRunning;

if (gRunning)
{
llResetTime();
llSetTimerEvent(UPDATE_TIME);
}
else
{
llSetTimerEvent(0.0);
}
}

timer()
{
if (llGetTime() >= MAX_TIME)
{
llSetTimerEvent(0.0);
}
else
{
float max = MAX_DISTANCE;
float half = max / 2.0;

vector pos = gOrigPosition +
<half - llFrand(max), half - llFrand(max), half - llFrand(max)>;

vector rot = <llFrand(TWO_PI), llFrand(TWO_PI), llFrand(TWO_PI)>;

llSetPrimitiveParams([PRIM_POSITION, pos, PRIM_ROTATION, llEuler2Rot(rot)]);
}
}
}


edit:
/me breaks out the red marker.. Hm.. -2 points!
From: Alfredo Overland
I am a professor of Botany and trying to develop educaitonal materials for teaching.
Alfredo Overland
Registered User
Join date: 19 Mar 2009
Posts: 5
04-20-2009 13:05
Excellent, I'll try that script. Since I'm totally new, exactly what part do I copy and paste into a blank script window? I tried it from the [php at the beginning to the php] at the end and it gave me an error. I then tried it without the [php at the beginning and php] at the end and it compiled succesfully but when I try it nothing happens. Sorry for being such a beginnner but I have to start somewhere.
Alfredo
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
04-20-2009 13:06
Don't copy the php tags - they're just for forums formatting.. (which mostly doesn't work)

Tip: you'll get the formatting back if you click the Quote button and copy from that.

edit: did you try touching the prim or chatting "/2345start"?
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
04-20-2009 13:16
Here you go, this works, but I left out a lot (touching to disable the movement and such). But tested this in world, and it moves nicely for a 0.1 sphere, and is in NO WAY optimized, made it for readability. :)

CODE

init()
{
llSetStatus(STATUS_ROTATE_X | STATUS_ROTATE_Y | STATUS_ROTATE_Z, FALSE);
llSetBuoyancy(1.0);
llSetStatus(STATUS_PHYSICS, TRUE);
}

vector RandomPos()
{
vector temp = gStartingPos;
float negative = llFrand(2.0);
if(negative <= 1.0)
{
temp.x = temp.x + llFrand(0.1);
}
else
{
temp.x = temp.x - llFrand(0.1);
}
negative = llFrand(2.0);
if(negative <= 1.0)
{
temp.y = temp.y + llFrand(0.1);
}
else
{
temp.y = temp.y - llFrand(0.1);
}
negative = llFrand(2.0);
if(negative <= 1.0)
{
temp.z = temp.z + llFrand(0.1);
}
else
{
temp.z = temp.z - llFrand(0.1);
}
return temp;
}

vector gTargetPos;
integer gTarget = 0;
vector gStartingPos;

default
{
state_entry()
{
gStartingPos = llGetPos();
}

touch_start(integer num_detected)
{
init();
gTargetPos = RandomPos();
gTarget = llTarget(gTargetPos, 0.05);
llMoveToTarget(gTargetPos, 0.1);
}

at_target(integer tnum, vector targetpos, vector ourpos)
{
llStopMoveToTarget();
llTargetRemove(gTarget);
gTargetPos = RandomPos();
gTarget = llTarget(gTargetPos, 0.05);
llMoveToTarget(gTargetPos, 0.1);
}
}


I'll drop you a line in world too.
Alfredo Overland
Registered User
Join date: 19 Mar 2009
Posts: 5
04-20-2009 14:37
Hi Lazink. I just logged in. Let me try this in SL and see what happens. I think with the previous version that I tried at the University there was a lot of lag between movements.
Alfredo
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
04-20-2009 14:41
From: Alfredo Overland
Hi Lazink. I just logged in. Let me try this in SL and see what happens. I think with the previous version that I tried at the University there was a lot of lag between movements.

That could just be because of the way the non-physical stuff moves..

You might have problems with the 2nd script if the objects aren't phantom and you have a bunch of them in close proximity. It's a little tricky (but not too bad) to fix that because they'll fall thru the floor if you set phantom/physical without having started them moving. You probably also want to turn off physics once the script decides to have them stop moving..
Alfredo Overland
Registered User
Join date: 19 Mar 2009
Posts: 5
04-20-2009 14:48
Lazink:
Im aat my place called MUSL laboratory (227,228,36). Can you go there? I've been on SL since last Thursday so I don't know that much about it, but learning fast. I read a couple of books about it this weekend.
Alfredo