Created
August 14, 2015 16:52
-
-
Save JokerMartini/9a689c5de7282f82aaa9 to your computer and use it in GitHub Desktop.
Maxscript: This snippet demonstrates how to rotate a node around a given vector, 'X' degrees. This example rotates a sphere so it aligns with another objects vector based on the same origin.
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
fn GetVectorsAngle v1 v2 = | |
( | |
theAngle = acos(dot (normalize v1) (normalize v2)) | |
) | |
fn PlaceAlongVector pos:[0,0,0] vec:[0,0,1] offset:0 = | |
( | |
pos + (normalize (vec)) * offset | |
) | |
fn GetRightTrianglePoint posA:[0,0,0] posB:[0,0,0] = | |
( | |
/* Find the length to create a right traingle */ | |
-- get angle difference between 2 vectors | |
difVector = posA - posB-- vector between plane and ball | |
objVector = pln.transform.row3 -- vector using normal of plane | |
dist = distance posB posA | |
calcAng = GetVectorsAngle difVector objVector | |
-- calculate the length of side to create a right angle | |
len = cos(calcAng) * dist | |
-- place point which would create a rightAngle at the sphere | |
cornerPos = PlaceAlongVector pos:posB vec:objVector offset:len | |
cornerPos | |
) | |
/* Test Scene Setup */ | |
clearlistener() | |
format "\n" | |
delete objects | |
pln = plane width:5 length:5 pos:[0,10,10] wirecolor:red; rotate pln (angleaxis 30 [1,0,0]) | |
ballA = sphere pos:[15,35,0] wirecolor:blue radius:2 | |
ballB = sphere pos:[32,18,7] wirecolor:green radius:1 | |
/* END Scene Setup */ | |
-- calcualte corner positions along axis of rotation | |
ballApt = GetRightTrianglePoint posA:ballA.pos posB:pln.pos | |
point pos:p1 size:2 wirecolor:blue | |
ballBpt = GetRightTrianglePoint posA:ballB.pos posB:pln.pos | |
point pos:p2 size:2 wirecolor:(random yellow green) | |
-- now calcualte the angle need to rotate the blue ball so it aligns to the red | |
vectorA = p1 - ballA.pos | |
vectorB = p2 - ballB.pos | |
ang = GetVectorsAngle vectorA vectorB | |
-- crs = cross vectorA vectorB | |
/* Mohamed METHOD */ | |
-- Step 1 - the point to rotate around | |
pt = pln.pos | |
-- Step 2 - the vector axis to rotate around | |
vectorAxis = normalize (pln.pos - ballApt) | |
-- Step 3 - getting input matrix | |
inputMatrix = ballA.transform | |
-- Step 4 - to rotate around another point P, you need to translate(-P) , then rotate(r) , then translate(P) | |
-- you have a vector to rotate around, and an angle, (v, a) | |
translate inputMatrix -pt | |
rotate inputMatrix (quat ang vectorAxis) | |
translate inputMatrix pt | |
-- Step 5- now apply this matrix | |
resultMatrix = inputMatrix | |
sphere transform:resultMatrix wirecolor:black radius:2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment