Search ProofOfProgress Blog

Wednesday, April 6, 2011

Align Object Axis to line and control twist with upnode


Say we want to align the Z-axis of an object GEOM
to a line between objects cp1 and cp2.

Step1: Create a normalized vector:
P01 = cp1.pos; --Original Start Point.
P02 = cp2.pos; --Original End Point.
PVEC = P02-P01; --Point from start point to end point.
PLEN = length(PVEC); --Length of vector.
NPV = PVEC / PLEN; --Normalized PVEC

Step2: Lets say that the orientation of cp1 is also going to be used for
twist control. Specifically that, the twist of the objects aligned to the
line will be controlled by the X-Axis of cp1.

--The initial X-Axis may not be perpendicular to the line cp1-cp2.
--Take the cross between the bent X and the Z-Axis (NPV) to get Yaxis.
bentX = (Normalize cp1.transform.row1);
Yaxis = Normalize(cross NPV bentX);

--Use the good Y-axis and Z-Axis (NPV) to get a perpendicular Xaxis.
Xaxis = Normalize(cross NPV Yaxis);
GEOM.Transform = Matrix3 Xaxis Yaxis NPV P01 --Objects new transform.
-- x y z position

Take it Further:
To take this further, you could slide the object up and down the line
by using point-normal form.

Example: Put aligned object in MIDDLE of the line:
midPosition = cp1 + NPV*(PLEN/2)


Example: slide aligned object from start of line to end of line:
for i = 0 to 1 do( --0 to 1 as in Zero to 100%.
istr = i as string;
midPosition = cp1 + NPV*(PLEN*istr)
)--[x]

No comments:

Post a Comment