Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Determin Rotation between 4 vectors

Eyes Shutt
Registered User
Join date: 25 Feb 2007
Posts: 3
01-29-2009 08:05
I have been trying to solve a rotation math problem, so thought I would drop it in here, see if anyone can help.

I have 3 or 4 vector that are in the shape of a box. I am trying to come up with the math to match the rotation a prim would have to be in to fit that box.

I already figured out I can use llRotBetween to get the rotations from vector a and b and then again for a and c, but now how can I combine those rotations to give me a complete rotation.

Here is an example of what I have so far:

//these vectors make up a small square at a weird rotations
vector a = <182.01290, 212.11170, 681.55100>;
vector b = <181.53110, 210.14280, 682.44300>;
vector c = <179.85940, 212.03060, 680.20900>;
vector d = <179.37770, 210.06180, 681.10100>;

rotation Rotation1 = RotBetween(<1,0,0>, llVecNorm(a - c));
rotation Rotation2 = RotBetween(<1,0,0>, llVecNorm(a - b));

If I set the rotation of a prim to Rotation1 the corners for a to c line up great, but have no correlation to a to b, and visversa.

Is there anyway to combine the rotations? Or am I going about it all wrong?

Anything would help.
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
01-29-2009 08:33
In what sense do the 3 or 4 vectors make a box? Do you mean a square in a plane?
_____________________
So many monkeys, so little Shakespeare.
Eyes Shutt
Registered User
Join date: 25 Feb 2007
Posts: 3
Correct
01-29-2009 08:39
Yes, that would be a better way to say it. Those 4 vectors make a square in a plane.
Claudius Sewell
Registered User
Join date: 8 Sep 2007
Posts: 8
01-29-2009 09:51
This should work:

vector fwd = llVecNorm(a - c);
vector left =llVecNorm(a - b);

rotation Rot = llAxes2Rot(fwd, left, fwd % left);

llSetRot(Rot);
Eyes Shutt
Registered User
Join date: 25 Feb 2007
Posts: 3
Absolutly Perfect
01-29-2009 10:05
That is it!... Thanks so much Claudius...
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
01-29-2009 11:46
Assuming that a is the corner between b and c.
_____________________
So many monkeys, so little Shakespeare.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
01-29-2009 12:23
From: Claudius Sewell
This should work:

vector fwd = llVecNorm(a - c);
vector left =llVecNorm(a - b);

rotation Rot = llAxes2Rot(fwd, left, fwd % left);

llSetRot(Rot);


Note that you MIGHT need to do some normalization here. If it isn't a right angle that they form, and/or if you have a fourth point you want to use to make sure the axes are oriented right, you might need to choose a priority between them and do something like:

CODE

vector u = llVecNorm(b-a);
vector v = c-a;
vector w = llVecNorm(u%v);
v = llVecNorm(w%u);

// Skip this conditional completely if you aren't using a point 'd'
if (w*(d-a) < 0.0)
{
vector tmp = u;
u = v;
v = tmp;
w = -w;
}

rotation rot = llAxes2Rot(u, v, w);
Claudius Sewell
Registered User
Join date: 8 Sep 2007
Posts: 8
01-30-2009 02:05
From: Hewee Zetkin


vector u = llVecNorm(b-a);
vector v = c-a;
vector w = llVecNorm(u%v);
v = llVecNorm(w%u);

// Skip this conditional completely if you aren't using a point 'd'
if (w*(d-a) < 0.0)
{
vector tmp = u;
u = v;
v = tmp;
w = -w;
}

rotation rot = llAxes2Rot(u, v, w);


Ah yes, that's a more general way I was looking for, ty Hewee.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
01-30-2009 11:02
From: Claudius Sewell
Ah yes, that's a more general way I was looking for, ty Hewee.


NP. May not be necessary anyway, at least some/most of the time. And even that'll choke if any three of the points are colinear. So some error checking might be a good idea too. Exercise for the reader. :)