[Closed] Xform matrix from vectors like lookat?
I’m trying to build a transform matrix from 2 vectors exactly like a lookat controller works. I need one vector to define 2 axis’, and then another vector to be used like an upnode.
I know how to do this:
(
p1 = getNodeByName "Point001"
p2 = getNodeByName "Point002"
p3 = getNodeByName "Point003"
x = normalize (p2.transform.row4 - p1.transform.row4
y = normalize (p2.transform.row4 - (p1.transform.row4 + p3.transform.row4))
z = normalize (cross x y)
tm = matrix3 x y z p1.transform.row4
orthogonalize tm
p = point()
p.transform = tm
)
This gets me close to what I need, but I know that the 2 vectors didn’t start out perpendicular and the orthogonalize seems to average it out to get rid of the skew. But I need the transform to stay pointing down the x vector.
I’ve figured it out one way. It might not be the best way. I’ll wait for Dennis to let me know. But basically, my idea is to recalculate the y that was causing the skew by crossing the x and z and not bothering to orthogonalize.
(
p1 = getNodeByName "Point001"
p2 = getNodeByName "Point002"
p3 = getNodeByName "Point003"
x = normalize (p2.transform.row4 - p1.transform.row4
y = normalize (p2.transform.row4 - (p1.transform.row4 + p3.transform.row4))
z = normalize (cross x y)
y = normalize (cross z x)
tm = matrix3 x y z p1.transform.row4
p = point()
p.transform = tm
)
i showed several times on this forum how to make max matrix3 using three points. here is it again with some comments:
fn threePointMatrix3 p0 p1 p2 =
(
front = normalize (p1 - p0)
dir = normalize (p2 - p0)
side = normalize (cross dir front)
up = normalize (cross front side)
matrix3 front side up p0
)
point transform:(threePointMatrix3 [0,0,0] [1,0,0] [0,0,1]) box:off cross:off axistripod:on wirecolor:yellow
we have three points:
#1 center of our system
#2 lies in direction of FRONT
#3 lies in direction of UP
steps:
#1 make a normalized vector for our matrix3 X direction (row1)
#2 make a normalized vector for DIR direction (not a real Z! because temporal DIR vector can be not orthogonal to X vector)
#3 make a normalized vector for our matrix3 Y direction (row2). its cross product X and UP. the order of cross operation is important, because we want to get RIGHT matrix as used in max system
#4 make a normalized vector for our matrix3 Z direction (row3). it’s the real UP that orthogonal to other two axis.
#5 make the RIGHT ORTHOGONAL MATRIX3
the normalization on step #4 is not necessary. because we multiply two normalized orthogonal vectors and the result has to be normalized as well.
what are two problems here? both these problems we always see in for example LookAt controller behavior.
#1 what do you do if our front and up vectors are parallel to each other initially?
(in this case we see spinning a node in its lookat direction)
#2 how to make side vector if angle between front and up vectors more than 180 deg?
(in this case we see flipping a node against its lookat direction)