Last active
June 9, 2022 19:06
-
-
Save JokerMartini/cda2571f6308c2f93a7a to your computer and use it in GitHub Desktop.
Maxscript: This function demonstrates how to rotate an object on it's local axis so that y axis faces towards another object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
denisTMAX | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delete objects | |
target = sphere pos:[20,20,20] radius:2 | |
n = teapot radius:2 pos:[6,35,0] | |
rotate n (angleaxis -68.2351 [0.808965,0.587747,0.0113632]) | |
-- rotate teapot on local z axis so it points towards the vector (sphere position) | |
theDir = normalize (target.pos - n.pos) --a vector pointing from teapot to sphere position | |
theZ = normalize (n.transform.row3) --the local Z axis | |
theCross = cross theDir theZ --a vector perpendicular to both the Z and the direction vector | |
theDir2 = normalize (cross theZ theCross) --a vector perpendicular to the Z and the cross product = projection of the direction in local XY | |
theNewX = normalize (cross theDir2 theZ) --a vector perpendicular to both the projected direction and Z = the new local X | |
n.transform = matrix3 theNewX theDir2 theZ n.pos --make a matrix of the new X, Y and existing Z, keep the position |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delete objects | |
target = sphere pos:(random [10,10,0] [-10,-10,0]) radius:2 wirecolor:red | |
origins = for i = 1 to 10 collect (teapot pos:(random [30,30,0] [-30,-30,0]) radius:4) | |
fn lookAtFN target others vec:[0,1,0] = | |
( | |
for o in others do | |
( | |
local yAxis = vec*o.transform.rotationPart | |
local pivPos = o.pos | |
local objPos = target.pos | |
local ang = acos(dot(normalize (objPos-pivPos)) (normalize (yAxis))) | |
local rot_box = if pivPos.x > objPos.x then eulerangles 0 0 ang else eulerangles 0 0 -ang | |
rotate o rot_box | |
) | |
) | |
lookAtFN target origins |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment