Notifications
Clear all

[Closed] Replicating the align in orientation tool

I’m having trouble replicating the functionality of the align tool. Position x y z is easy but what of orientation in the axes? I’ve tried matrix3 methods but end up just squishing the object that’s being aligned. I want to orient an object along another’s axis, while leaving it’s own relative axes orientaion intact. This is proving unexpectedly difficult. Please help. Thanks in advance.

7 Replies

Squishing the object? That makes it sound like you are messing with the object’s scale as opposed to rotation. If rotation matching is what you are looking for, it’s time to bust out an old linear algebra book.

One useful tool for rotation based scripts is to use $.dir, which is that node’s local z-axis normalized direction vector. An object that hasn’t been rotated would have a $.dir of [0,0,1], meaning it’s “up” is the same as the world’s “up”, which in 3ds Max is positive Z.

If you want to be a cheater and take the easy way out, you can simply do this:

$Box1.transform = $Box2.transform

And voila, you’ve recreated the align tool for position and rotation!

The question is, how much functionality do you need and if you willing to study some linear algebra and make use of Maxscript’s vector math like dot, cross, and normalize.

If you want the object to be in the same position and rotation as another object, yet retain it’s original local rotation, perhaps something like this?

temp = $Box1.transform
$Box1.transform = $Box2.transform
$Box1.pivot = temp

Thanks marth but no, this doesn’t work . Setting one objects transform to another’s means all the axes coincide that’s all. I just want one object to point in the z direction of the other . Yes I did linear algebra in school. That’s the easy part. What u find difficult is implementing it in mxs. I mean surely there’s an easy way to orient objects without dung endless cross dot stuff. I’m not sure what corresponds to a rotation in a matrix 3 object for example . I find the code confusing nit the math. The math is easy.

Not to be a smartass, but that seems to be a math question regarding how transform matrices work.

The first 3 lines of the matrix represent the vectors of the object’s X, Y and Z axis – that there is your rotation. (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0]) means X is pointing in X direction, Y – in Y, Z – in Z. Thing is, if you set just one line, like (matrix3 [1,0,0] [0,1,0] [0,0.707,0.707] [0,0,0]), you’ll get a sheared transform. Which is usually worse than non-uniform scale. There’s an orthogonalize <matrix3> function, but it’ll probably average the vectors and you’d loose the perfect alignment.

Their lengths is the scale. (matrix3 [2,0,0] [0,2,0] [0,0,2] [0,0,0]) – same object with the scale of 200% in all axis.

The 4th line is the position offset.

There’s probably an easier way, but atm I can only imagine setting your target axis directly, then constructing the other axis through cross porducts. Don’t forget to keep the lengths.

Here’s a quick and dirty example of aligning by Z:

undo on 
(
	sourceNode = $Teapot001
	targetNode = $Teapot002
	sourceTm = sourceNode.transform
	targetTm = targetNode.transform
	
	len1 = length sourceTm.row1
	len2 = length sourceTm.row2
	len3 = length sourceTm.row3
	
	sourceTm.row3 = len3 * normalize targetTm.row3  -- set Z directly
	sourceTm.row2 = len2 * normalize (cross sourceTm.row3 sourceTm.row1) -- construct Y from new Z and old X
	sourceTm.row1 = len1 * normalize (cross sourceTm.row2 sourceTm.row3) -- construct X from new Y and new Z
	
	sourceNode.transform = sourceTm
)

Are you trying to point objA’s z axis towards objB or are you trying to point objA’s z axis the same direction as objB’s z axis?

I just want one object to point in the z direction of the other
I think he wants objA’s Z to be in the same direction as objB’s, but wants some automagical way of finding what X and Y should be without using cross or normalize… which is how you would calculate them.

If you change an object’s Z, at least one other axis must change too. They are all at right angles to each other…

without dung endless cross dot stuff
Zhalktis’s code works fine for me and is pretty simple. I don’t know what part about it is endless…

Thanks everyone – wow – a reply from lithuania! Impressive.
Thanks Zhaltkis – very useful.
I got hold of Bobo’s ‘the matrix explained’ and there was a chapter that dealt EXACTLY with what I needed to know. Thanks Bo Bo. Although I’ve heard bad things about CG academy. Did they pay you Bobo?
I would recommend bobos series to anyone but don’t buy from CG academy, they’re crooks according to Paul Neale…I bought it years ago but the material is still valid, I never thought I’d need to watch all of it. How wrong I was,…I’m happy to send a link to anyone and remit some money to bobo via paypal if he’s good with that.

here is how i would do it (and i don’t know another way)
you have two objects and their transform matrices A and B.
you want to align let’s say object’s B x-axis to object’s A… so we have to rotate the matrix B somehow…

first is to get the space for rotation: is a matrix C made with cross product of A’s and B’s x-axis

the angle F of rotation about C z-axis is a dot product of A’s and B’s x-axis

and it’s almost it. rotate matrix C on angle F

and multiply matrix B on matrix C.

DONE