Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

A life for beginers

Olympia Rebus
Muse of Chaos
Join date: 22 Feb 2004
Posts: 1,831
06-27-2005 10:30
What's the secret to keeping the fish right side up?

I've fiddled with stuff like this,
llSetStatus(STATUS_ROTATE_Z, FALSE)
but I'm not sure this is the proper way to keep them upright.
Part of the problem is I think there should be some rotation (turning left, right, pitching up and down, tilting side to side), just not rolling over like they have swim bladder problems.
Any suggestions?

Thanks in advance
_____________________
Zak Escher
Builder and Scripter
Join date: 3 Aug 2003
Posts: 181
06-27-2005 15:43
Here is code I have come up with for swarming creatures. The code has been pieced together from various sources, mainly the discussion between Surina and Olympia. I have added some code from another swarm script (to make the creatures look in the direction they are moving) and some of my own code (mainly the code for finding the swarm center).

The creatures seem to stay together and not fly apart like my previous attempts.

CODE
float ver_num = 0.7;
string obj_name = "aLife";
float fStrength = 1.0;

default
{
state_entry()
{
llSetColor(<1,1,1>, ALL_SIDES);

llSetStatus(STATUS_SANDBOX, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
llCollisionSound("", 0.0);

llSetObjectName(obj_name);
llSetObjectDesc((string)ver_num);
llSetBuoyancy (1.0);
llSensorRepeat(llGetObjectName(), NULL_KEY, ACTIVE|SCRIPTED, 30, PI, 1.0);
}

on_rez(integer start_param)
{
llResetScript();
}

sensor(integer num_detected)
{
llSetColor(<0,1,0>, ALL_SIDES);

vector vSwarm_Center;
vector vSwarm_Vel;
integer iSwarm_Pop;
integer i;

for (i=0; i < num_detected; i++)
{
vSwarm_Center += llDetectedPos(i);
vSwarm_Vel += llDetectedVel(i);
iSwarm_Pop++;
}

vSwarm_Center = vSwarm_Center / iSwarm_Pop;
vSwarm_Vel = vSwarm_Vel / iSwarm_Pop;

float fMass = llGetMass();
vector vTarget = vSwarm_Center + vSwarm_Vel;
vector vMypos = llGetPos() + llGetVel();
vector vDesiredHeading = vTarget - vMypos;
float fDist = llVecDist(vTarget,vMypos);

vector vImpulse = llVecNorm(vDesiredHeading);
vector vRepulse = vImpulse / fDist;

llApplyImpulse((vImpulse - vRepulse) * fStrength * fMass, FALSE);
llLookAt(vMypos, fMass * 2.8, fMass * 50000);
}

no_sensor()
{
llSetColor(<0,0,1>, ALL_SIDES);
}
}


I have one question for the group. How can I use this basic script and implement a feeding behavior? (ie if the creature sees food, it goes after it)
_____________________
Zak Escher
Unity Shapes
http://slurl.com/secondlife/Hatteras%20Island/125/46/31
http://unityshapes.blogspot.com/
See what I have for sale at SLExchange
Surina Skallagrimson
Queen of Amazon Nations
Join date: 19 Jun 2003
Posts: 941
06-28-2005 03:31
From: Zak Escher
I have one question for the group. How can I use this basic script and implement a feeding behavior? (ie if the creature sees food, it goes after it)

I seperate different tasks into different scripts. So far each fish has:-

Shoaling/swarming/flocking.
Feeding (including reproduction)
Predator evasion.
Lifespan control
Death.

The feeding script is a simplified version of shoaling/swarming/flocking (from hereon reffered to as shoaling, as mine are fish). A sensor ping looks for the food prims and, if found, and impulse is applied towards the food. The strength of the impulse is dependent on the need to feed (hunger) and the desire to feed (greed).
Hunger increases with time and is reduced upon feeding (collision with designated food prim). Greed is fixed and is passed from parent to child during breeding. As it is passed it is changed by (integer)llFrand(3)-1 so it could go down or up or stay the same.

When the fish is hungry enough, the impulse strength will defeat the shoaling. Likewise for predator evasion, evasion strength is a function of danger level (how close the predator is) and will defeat shoaling and feeding if the predator is close enough...

However if a fish is almost starved, the feeding instinct will still defeat predator evasion.


Observed results
A predator was given a 'home' location directly over the food source with a limited wander distance. The main shoal tended to circle the food just out of range of the predator. As the predator approached they would back off. Occasionally while the predator was 'chasing' the main group a lone fish would dart at the food from the other side.


The concept to realise here is that by using several scripts you're effectivly parrallel processing, so your creature can be 'thinking' of several things at once with the most important thoughts (Food, predation, shoaling) taking control.
_____________________
--------------------------------------------------------
Surina Skallagrimson
Queen of Amazon Nation
Rizal Sports Mentor

--------------------------------------------------------
Philip Linden: "we are not in the game business."
Adam Savage: "I reject your reality and substitue my own."
Zak Escher
Builder and Scripter
Join date: 3 Aug 2003
Posts: 181
06-28-2005 05:11
Thanks for the tip about the feeding behavior. I had sort of gotten feeding to work in the swarming/shoaling script, but the script was getting complicated. Seperating the behaviors into separate scripts makes sense.
_____________________
Zak Escher
Unity Shapes
http://slurl.com/secondlife/Hatteras%20Island/125/46/31
http://unityshapes.blogspot.com/
See what I have for sale at SLExchange
Olympia Rebus
Muse of Chaos
Join date: 22 Feb 2004
Posts: 1,831
06-28-2005 08:11
Thanks for including it here, Zak.
I look forward to studying it and tinkering with it. :D
From: Zak Escher
Here is code I have come up with for swarming creatures. The code has been pieced together from various sources, mainly the discussion between Surina and Olympia. I have added some code from another swarm script (to make the creatures look in the direction they are moving) and some of my own code (mainly the code for finding the swarm center).

The creatures seem to stay together and not fly apart like my previous attempts.

CODE
float ver_num = 0.7;
string obj_name = "aLife";
float fStrength = 1.0;

default
{
state_entry()
{
llSetColor(<1,1,1>, ALL_SIDES);

llSetStatus(STATUS_SANDBOX, FALSE);
llSetStatus(STATUS_PHYSICS, TRUE);
llCollisionSound("", 0.0);

llSetObjectName(obj_name);
llSetObjectDesc((string)ver_num);
llSetBuoyancy (1.0);
llSensorRepeat(llGetObjectName(), NULL_KEY, ACTIVE|SCRIPTED, 30, PI, 1.0);
}

on_rez(integer start_param)
{
llResetScript();
}

sensor(integer num_detected)
{
llSetColor(<0,1,0>, ALL_SIDES);

vector vSwarm_Center;
vector vSwarm_Vel;
integer iSwarm_Pop;
integer i;

for (i=0; i < num_detected; i++)
{
vSwarm_Center += llDetectedPos(i);
vSwarm_Vel += llDetectedVel(i);
iSwarm_Pop++;
}

vSwarm_Center = vSwarm_Center / iSwarm_Pop;
vSwarm_Vel = vSwarm_Vel / iSwarm_Pop;

float fMass = llGetMass();
vector vTarget = vSwarm_Center + vSwarm_Vel;
vector vMypos = llGetPos() + llGetVel();
vector vDesiredHeading = vTarget - vMypos;
float fDist = llVecDist(vTarget,vMypos);

vector vImpulse = llVecNorm(vDesiredHeading);
vector vRepulse = vImpulse / fDist;

llApplyImpulse((vImpulse - vRepulse) * fStrength * fMass, FALSE);
llLookAt(vMypos, fMass * 2.8, fMass * 50000);
}

no_sensor()
{
llSetColor(<0,0,1>, ALL_SIDES);
}
}


I have one question for the group. How can I use this basic script and implement a feeding behavior? (ie if the creature sees food, it goes after it)
_____________________
Johnny Noir
Registered User
Join date: 5 Jan 2004
Posts: 28
06-28-2005 11:38
The point about multiple scripts is interesting. My current model (which is almost ready for being loosed on the wild, finally) uses states to accomplish something similar but not quite the same. I'm wondering if there are sufficient advantages of the multiple-script method to shift my code to that...
1 2