Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Positives and Negatives

Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-17-2008 19:28
Newbie here. Is there a script (or is it even to possible to make) a script for make two pryms attract one and other when they are close by, such like a positive and a negative. If there is a link I would appriciate being pointed to the right direction.
MoxZ Mokeev
Invisible Alpha Texture
Join date: 10 Jan 2008
Posts: 870
09-17-2008 19:33
Hmm...maybe a swarm script with a follower and a collision detection?
Sounds doable. There are some amazing scripters here and I'm sure one will come along soon.
_____________________
:p
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
09-17-2008 21:04
This script looks for nearby objects with the same name as the one the script is in, and attracts the object toward them with an inverse square law of attraction. Note that the script does not take object mass or "charge" or anything into consideration; it simply makes the force a function of the distance between the objects. But you could easily factor mass or other things in. Also, play with the value of ATTRACTIVE_CONSTANT.

The script turns on STATUS_SANDBOX so you don't wind up with objects everywhere. It doesn't do any sanity checks for the amount of force, so after the objects get real close they tend to go wild and scatter. Rez a sphere, change its name, hold down CTRL when you drag the script onto it, make a few copies, then select Tools -> Set Scripts to Running in Selection. Have fun!

EDIT: Fixed indexing slightly, so position isn't always seen as the same as for the closest object.

CODE

float ATTRACTIVE_CONSTANT = 1.0;

float SENSOR_PERIOD = 0.1;

default
{
state_entry()
{
string objName = llGetObjectName();

llSetBuoyancy(1.0);
llSetStatus(STATUS_PHYSICS | STATUS_SANDBOX, TRUE);

llSensorRepeat(objName, NULL_KEY, SCRIPTED, 96.0, PI, SENSOR_PERIOD);
}

sensor(integer nDetected)
{
vector totalForce = ZERO_VECTOR;

integer i;
for (i = 0; i < nDetected; ++i)
{
vector r = llDetectedPos(i)-llGetPos();
float rLen = llVecMag(r);
vector f = ATTRACTIVE_CONSTANT*r/(rLen*rLen*rLen);

totalForce += f;
}

llSetForce(totalForce, FALSE);
}

no_sensor()
{
llSetForce(ZERO_VECTOR, FALSE);
}
}
Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-18-2008 15:38
can this script be modified to make pryms of the same name repel each other and attract different ones and is it possible to control the umm...moving, they stay together for a little while and then fly off. I've tried messing with the code but all I do is mess it up.
Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-19-2008 04:54
"bump"
Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-19-2008 09:22
Ok. well I messed around with the llSetbuoyancy a little and they move a lot less. Is there a way set the force that attracts them stronger?
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
09-19-2008 10:00
Sure. Here you go. Name some objects "Positive Pole" or whatever, as long as it contains the word "Positive", and name some likewise with "Positive" replaced by "Negative" (case is important). You might want to color or texture them differently also, so you can visually tell the difference.

I'm afraid the flying apart bit isn't a trivial problem, as it has to do with computational error and timer period and such. You might want to constrain the objects in other ways (e.g. a box or a second script to give them a bit of an initial velocity so they don't just accelerate straight toward or away from each other, etc.). Accounting for those things is doable, but will make the script a lot more complex and is beyond the scope of what I am going to develop for this forum at present. Hopefully this'll be a good starting point for you and/or others to go from. Good luck!

CODE

string POSITIVE_NAME_PART = "Positive";
string NEGATIVE_NAME_PART = "Negative";

float ATTRACTIVE_CONSTANT = 1.0;
float REPULSIVE_CONSTANT = 1.0;

float EPSILON = 0.001;
float MIN_RADIUS_SQ = 0.0625;

float SENSOR_PERIOD = 0.1;

string repulsiveObjectName;
string attractiveObjectName;

// Returns empty string if 'search' is not found in 'str'. Otherwise,
// replaces the first occurance of 'search' in 'str' with 'replace' and returns
// the result.
string replaceSubString(string str, string search, string replace)
{
if (str == "" || search == "")
{
return "";
}

integer start = llSubStringIndex(str, search);
if (start < 0)
{
return "";
}

integer end = start+llStringLength(search)-1;

return llInsertString(llDeleteSubString(str, start, end), start, replace);
}

default
{
state_entry()
{
repulsiveObjectName = llGetObjectName();

attractiveObjectName =
replaceSubString(
repulsiveObjectName, POSITIVE_NAME_PART, NEGATIVE_NAME_PART);
if (attractiveObjectName == "")
{
attractiveObjectName =
replaceSubString(
repulsiveObjectName, NEGATIVE_NAME_PART, POSITIVE_NAME_PART);

if (attractiveObjectName == "")
{
llOwnerSay(
"Neither '"+POSITIVE_NAME_PART+"' nor '"+NEGATIVE_NAME_PART+
"' found in object name.");
return;
}
}

llSetBuoyancy(1.0);
llSetStatus(STATUS_PHYSICS | STATUS_SANDBOX, TRUE);

llSensorRepeat("", NULL_KEY, SCRIPTED, 96.0, PI, SENSOR_PERIOD);
}

sensor(integer nDetected)
{
vector totalForce = ZERO_VECTOR;

integer i;
for (i = 0; i < nDetected; ++i)
{
string objName = llDetectedName(i);
if (objName == repulsiveObjectName || objName == attractiveObjectName)
{
float multiplier;
if (objName == repulsiveObjectName)
{
multiplier = -REPULSIVE_CONSTANT;
} else
{
multiplier = ATTRACTIVE_CONSTANT;
}

vector r = llDetectedPos(i)-llGetPos();
vector rUnit = llVecNorm(r);
float rMagSq = r*r;
vector force;
if (rMagSq < EPSILON)
{
force = ZERO_VECTOR;
} else if (rMagSq < MIN_RADIUS_SQ)
{
force = multiplier*rUnit/MIN_RADIUS_SQ;
} else
{
force = multiplier*rUnit/rMagSq;
}

totalForce += force;
}
}

llSetForce(totalForce, FALSE);
}

no_sensor()
{
llSetForce(ZERO_VECTOR, FALSE);
}
}
Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-20-2008 10:48
I was wondering if it was possible to mess with this code to make it react to multiple prym names? I am planning to make a Periodic Table and I don't know if that's even possible.
Dekka Raymaker
thinking very hard
Join date: 4 Feb 2007
Posts: 3,898
09-20-2008 12:55
something like this?

http://slusage.com/chemistry.asp

quote:

"3D Periodic Table
I was sponsored by the American Chemical Society to build a 3D periodic table in Second Life. You can pick one up from the freebie wall in the HQ on ACS Island SLurl."

on the web page there is a link to the SL site.
_____________________
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
09-20-2008 15:19
From: Zeta Eizenstark
I was wondering if it was possible to mess with this code to make it react to multiple prym names? I am planning to make a Periodic Table and I don't know if that's even possible.

Of course it is. Ask more specific questions and we'd be happy to help you do it. Speaking for myself, I've posted as much of a full script as I am going to on this topic, but I'd be happy to give you snippits and tips that might guide you in the right direction if you provide some more information about the areas that you're having difficulty with.
Zeta Eizenstark
Registered User
Join date: 20 Aug 2008
Posts: 79
09-20-2008 16:21
It's perfect, but what I want to do is for students to be able to make a copy of the element, turn on the script and see if the elements that they chose are able to combine with each other. I like how the script can detect the names and that's what I like. Can we make take different names, not just "Positive" and "Negative." Thanks to everyone that has been helping me.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
09-22-2008 16:52
From: Zeta Eizenstark
Can we make take different names, not just "Positive" and "Negative." Thanks to everyone that has been helping me.


Yep. You sure can. :)