Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

table and chair script

DancesWithRobots Soyer
Registered User
Join date: 7 Apr 2006
Posts: 701
09-08-2008 06:43
I've seen these tables that rez an extra seat as people sit in them, in fact I've got a few different types that came with assorted multi tool devices I've collected over time.

But I've never seen a script that does that that was open enough that I can could study it. Could someone shine a light or suggest a wiki article or a web tutorial somewhere?
_____________________
"Two lives I have.
One life I live. One life I dream.
In dreams I remember the better in me."
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
09-08-2008 11:40
the essential idea is that the table and chair have seperate scripts... although the table should be linked with a single chair...

chair get sat in, it sends a message to the table to rez another chair, the table decides what position to rez the chair in (based on an internal list).

cleanup for the chairs could work most simply by having it listen for messages when it's NOT being sat in, if it hears a message that another chair is empty, then it removes itself... this should always leave you 1 open chair, and prevent the table rezzing an extra chair when it isn't needed (it should be listening for this message too) there should also be a message sent by a chair that removes itself, so that the table can add that spot back into it's list of available rez spots.
_____________________
|
| . "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...
| -
DancesWithRobots Soyer
Registered User
Join date: 7 Apr 2006
Posts: 701
09-08-2008 11:43
Hmm. predefined list. And I was sure I was going to have to deal with quaternions.
_____________________
"Two lives I have.
One life I live. One life I dream.
In dreams I remember the better in me."
Winter Ventura
Eclectic Randomness
Join date: 18 Jul 2006
Posts: 2,579
09-08-2008 12:37
Most versions of these tables, seem to use division of a sort. So that

when there's 1 chair, it's at 12,
2 chairs, they're at 12 and 6.
3 chairs, they're at 12, 4, and 8.
4 chairs.. 12, 3, 6, and 9

This way, each chair is evenly spaced, and the empty chair is ususally at 12.

Building such a system with predefined strided lists of coordinates and rotations would be possible, but it would be fairly heavy on script memory. Doing the math on an "as needed" basis is probably more efficient.

The real problem seems to be (at least for me) doing the offset rotational division. It's not just a matter of getting the precise spacial positions of the chairs, but also getting them to rotate and face the center. Plus there's the additional hassle of what to do when someone gets up. A lot of these "tables" will seemingly randomly shuffle the chairs, which has always been kind of jarring.

I really wanted to build one of these myself, but it took me 7 years to get through Algebra 1 with a C. The rezzing I understand, the messaging I understand, Letting each chair know which "number" it is.. I understand that too. Moving the chairs in a really neat way, I could handle that too.. but getting them to the right places, I'm totally at a loss.

If anyone can post THAT part of it.. I think we could start to assemble this project.
_____________________

● Inworld Store: http://slurl.eclectic-randomness.com
● Website: http://www.eclectic-randomness.com
● Twitter: @WinterVentura
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
09-08-2008 16:40
Get a copy of the MLPV2.1c release (on SLX, L$1) and pull out the ~props script. That one has the math in it. It's a little more complicated than it needs to be because I didn't know about llRezAtRoot when I wrote it -- so the part about finding the delta is no longer needed.

I'm making a scene rezzer script (so folks can build a number of scenes of copy/mod objects and recall any scene by menu). It has a simpler version of the positioning. Next time I'm ingame I'll pull out the math.

I'm still confused by quaternions. Trying to make a script for MLP that rotates all the poses (to compensated for a differently oriented MLP prim), and I can't get it to work and my head hurts! :(
Sayrah Parx
Registered User
Join date: 16 Jul 2008
Posts: 17
03-14-2009 21:33
My table tells the chairs where to go and then how to rotate. First I started out with not even caring about where they go relative to the table's rotation, so I'll show how I did that later.

So first it helps to have a vector that stores llGetPos() so you can do things with one part of it using a dot, like "x = pos.x".

Then it helps to think of the spacing as like a pie where each slice is the same size, and that the chairs are along the lines where you cut. So you can have a float named "percent" that you set equal to 1.0 divided by the number of chairs. When I started, I just had "chair count" equal to a number that I put in, but eventually you can make your script adjust it.

To find out where the first chair goes, you don't even need the percent. You do need to know how far away you want your chairs to be from the center of your table, so you can make a float to store this "radius". Then you can make two floats to store the chair's X and Y position. The chair's X is going to be the table's X plus llSin(TWO_PI) * radius, and the chair's Y is going to be the table's Y plus llCos(TWO_PI) * radius. What's funny is that at this point it's basically the same as just moving the chair along the X axis, with the distance from the table being the radius. So it's really easy to make a table that can position one, two or four chairs. You really only need to deal with circles when you have three chairs or more than four chairs. So what we have right now is:

integer chair_count = 5;
vector pos = llGetPos();
vector rot = llRot2Euler(llGetRot());
float percent = 1.0 / chair_count;
float x;
float y;

x = pos.x + (llSin(TWO_PI) * radius);
y = pos.y + (llCos(TWO_PI) * radius);

So how do you find out where the second chair goes, if you have 5 chairs? First, you have to understand that "all the way around the table" is TWO_PI. So 20% of the way around a table is just 0.2 * TWO_PI. So the position for the second chair of five would be:

x = pos.x + (percent * llSin(TWO_PI) * radius);
y = pos.y + (percent * llCos(TWO_PI) * radius);

Now all you have to do is make a loop around this. So you can do something like:

integer count;

for (count = 0; count < chair_count; count++)
{
x = pos.x + ((percent * count) * llSin(TWO_PI) * radius);
y = pos.y + ((percent * count) * llCos(TWO_PI) * radius);
}

and then if you want to adjust for the rotation of the table, you can do:

x = pos.x + (llSin(((percent * count) + (rot.z / TWO_PI)) * TWO_PI) * radius);
y = pos.y + (llCos(((percent * count) + (rot.z / TWO_PI)) * TWO_PI) * radius);

So now if you rotate the table 180 degrees, for example, your first chair will start on the opposite side.

Then for the rotation, somehow I came up with this:

llEuler2Rot(<180, 180, 270 - ((rot.z * 360) / TWO_PI) - ((360 / chair_count) * count)> * DEG_TO_RAD)

I really like using llEuler2Rot and llRot2Euler to switch between vectors and rotations, because it's so hard to understand what exactly is going on with quaternions. So you can either put that in the same loop or do it in another loop later, and of course keep track of your chairs and message them somehow so they actually move and rotate.
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
03-14-2009 23:53
While there's more than one way to skin a cat, Sayrah's got my vote for the most logical way to do the multi chair thingy. Good job on the explanation Sayrah. Sin and Cos are the best way to draw a circle, even when you're 'drawing' it with chairs. :)

Also, the Euler/Quaterion conversions are quite handy as no one that I know has the slightest clue how quats work :)
_____________________
My tutes
http://www.youtube.com/johanlaurasia
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
03-15-2009 01:15
the following example rezzes a set number of chairs at equal spacing around the table, and equal distance and facing from the table starting at object relative east (change base offset for a different start position, change number of chairs to whatever)
CODE

float baseNumChairs = 5; //-- for example, float because we'll need fractional angles
vector baseOffset = <1, 0, 0>; //-- distance chair is rezzed from center of table
rotation baseInterval = llEuler2Rot( <0, 0, TWO_PI / baseNumChairs> ); inteval in degrees for the next chair

integer counter = baseNumChairs;
currentRot = llGetRot();
currentOffset = baseOffset * currentRot;
do{
llRezAtRoot( "chair", curerntOffset, ZERO_VECTOR, currentRot, counter );
currentOffset *= baseInterval;
currentRot *= baseInterval;
}while (--counter);

not neccesary to know the inner workings of quaternions, only the formula.
formula is [offset * llGetRot()] then [lastClalcdOffset * interval]
chair rotation works the same way.

in case anyone is curious, that's the basis of the loop_rez tool up there
_____________________
|
| . "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...
| -