Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Need help with vector (3d) math

Sanderman Cyclone
Registered User
Join date: 29 Jun 2006
Posts: 8
01-20-2007 09:27
Hi I'm scripting a tool that will make a face prim between 3 world coordinates. The idea is to rez a cube in the middle(though not exactly in the average of the vectors as you'll see why) and rezize, taper, sheer and rotate the cube so it fills this triangle. It will be a tool to do basic vertex modelling in secondlife at a very primitive level, and it would be very handy for certain builds.

I have attached an image that shows a 2d version of the face, imagine it present inworld at a random position and rotation.

I have a few questions for those who are good in math:

1. how to calculate the position vector of point F
2. how to calculate the desired rotation so the face is aligned with these three points.
3. how to calculate the desired sheer.

I know in 2d I could use pythagoras and the cosinus rule and stuff, but I don't know how to do this in 3d, or maybe I could still use these but then I would still have problem with the rotation.

I wrote a script that can be put in prims at the points A,B and C and these all know each others positions and distances relative to each other, so only the calculations still need to be done. But this is where I got stuck. :o

I would greatly appreciate some help, but maybe I'm too ambitious. :confused:
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
01-20-2007 12:03
I'm not sure about the math, but I know that Seifert Surface did this for his Parameteric Surface Rezzer. Not only does he do the math to plot a triangle prim as you show, he considers all three possible orientations to see which one is most precise given the limits on primitive parameters.
Sanderman Cyclone
Registered User
Join date: 29 Jun 2006
Posts: 8
01-20-2007 12:35
Thanks. I did a search and I think I could incorporate some of his work into the tool I'm making. (with his permission of course)

It didn't occur to me that I was trying to reinvent the wheel. ;)
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
01-20-2007 13:07
Ok, assuming you're starting with the 3 vertex vectors, A, B and C, pre-calculate the lengths of the sides ( floats AB, BC and AC ) and the angles at each vertex ( floats aA, aB, aC ).

CODE

float AB = llDistanceBetween( A, B );
float BC = llDistanceBetween( B, C );
float AC = llDistanceBetween( A, C );
float aA = llAngleBetween( B - A, C - A );
float aB = llAngleBetween( A - B, C - B );
float aC = llAngelBetween( A - C, B - C );

I will assume, for the sake of consistency, that your diagram uses the prim's local X (width) and Z (height) axes, pointing to the right and up, repectively.


From: someone
1. how to calculate the position vector of point F

Find the midpoint of AB (E, in your diagram) just as you would find the average of two numbers, by adding the vectors together and dividing by 2.

CODE

vector E = ( A + B ) / 2;

Then add half of vector DC. AD is the length of AC times the cosine of angle A:

CODE

float AD = AC * llCos( aA );
vector D = A + ( B - A ) * ( AD / AB );
vector F = E + ( C - D ) / 2;

Note, you will want to first confirm that aA and aB are both <= 90 degrees (PI_BY_TWO radians), since these angles cannot exceed a right angle by sheering a prim. In other words, if there is an obtuse angle, it will have to be angle C, for the purposes of this example.

Also note, point F will fall on the plane of this polygon, but will not be the center of the prim. You will need to offset it by half the prim's Y scale. More on that below.


From: someone
2. how to calculate the desired rotation so the face is aligned with these three points.

llAxes2Rot is perfect for this. Input 3 vectors which point in the prim's desired local X, Y and Z directions, and it returns the appropriate rotation.

The X (fwd) vector would be B - A. The Z (up) vector would be C - D, and the Y (left) vector is the cross product of these two.

CODE

vector Yaxis = llVecNorm( (B - A) % (C - D) );
rotation R = llAxes2Rot( B - A, YAxis, C - D );

I first calculated the normalized Y-axis so that this can be used to calculate the center of the prim from point F.

CODE

vector PrimScale = llGetScale();
vector Center = F + Yaxis * PrimScale.y;



From: someone
3. how to calculate the desired sheer.

Sheer is the amount of horizontal offset applied to the top (+Z) face in terms of the base's dimensions. In this case, the ratio of the lengths AD to AB.

CODE

float Sheer = ( AD / AB ) - 0.5;

0.5 is subtracted to translate the range from the "0 to 1" to "-0.5 to +0.5".

Note, however, that the sheer of a prim is represented by a low-precision number, apparently rounded off to the nearest-hundredth (though it's probably more like the nearest-128th, I haven't done any tests to be certain), so point C will almost always be slightly off, depending on where it happens to fall between A and B.

Note that all of the above isn't an optimal method, but it should generally work.
Sanderman Cyclone
Registered User
Join date: 29 Jun 2006
Posts: 8
01-21-2007 07:26
Whoah, thanks for the info, this will help me on the way a lot when I want to write my triangle creation script.
Right now I'm using one by Seifert Surface as Lex suggested, but I think I will write my own, if only just to get the hang of it. :)
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
01-21-2007 10:26
From: Sanderman Cyclone
Whoah, thanks for the info
No prob. Oh, and where I wrote llDistanceBetween above, make that llVecDist. I was confusing it with llAngleBetween. <d'oh!>