Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Vectors and Rotation in 3D

Ardith Mifflin
Mecha Fiend
Join date: 5 Jun 2004
Posts: 1,416
06-26-2005 13:20
Say I have a rod of known length which rotates arbitrarily about one of its ends. How can I determine the position of the other end relative to the fixed end? In two dimensions, it's easy because we only have one angle to concern ourselves with and elementary trigonometry is all that stands between us and our point. However, in three dimensions we have to concern ourselves with the rotation about each of the axes. Some Google research revealed a rather complicated matrix method that may work splendidly in Matlab, but seems like it would be a nightmare to implement in SL.

Has anyone already written code to do this?
Ardith Mifflin
Mecha Fiend
Join date: 5 Jun 2004
Posts: 1,416
06-26-2005 13:30
Well, I guess I could have done a little research in the forum before posting this. The thread at /54/dc/43127/1.html seems to provide some good hints and code to get me on my way.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
06-26-2005 16:52
CODE

vector endPoint(vector pos, rotation rot, float length)
{
return pos + <0.0, 0.0, length> * rot;
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
06-27-2005 09:39
Well, Strife's given you the code to do that, but an explanation of why it works might help. First, the code takes the rod's rotation and multiplies by a vector of length 1 in the Z direction:

CODE

<0, 0, 1> * rot


Rotations in SL are a way of describing what has to be done to a vector pointing straight up to make it point in the same direction as the object in question. Multiplying will perform the actual rotation on a vector and return the resulting vector, so this will return a vector of length one in the direction of the rod. Since you wanted to deal with any known length, just plug the known length in instead of the 1. After that, just add on the starting position:

CODE

pos + <0, 0, 1> * rot;


If pos is the start of the rod, then (pos + <0, 0, 1> * rot) will be the location of the end of the rod. I imagine the complicated matrix stuff you found is all contained inside the part that rotates the unit vector.
Ardith Mifflin
Mecha Fiend
Join date: 5 Jun 2004
Posts: 1,416
06-28-2005 07:59
Thanks to both of you for providing some great help. I'm glad it was infinitely easier than I thought it was going to be. I guess quaternions are helpful little things.