Notifications
Clear all

[Closed] Object orientation to vector

Hi!

I am doing some vector math in maxscript using objects as vectors. So far I have a little function that “converts” the object orientation to a vector. It looks like this:

fn objToVector object =
(
	startPos = object.pos
	in coordsys object (tempPoint = point pos:[0,0,1])
	endPos = tempPoint.pos
	delete tempPoint
	
	vector = endPos - startPos
	
	print ( object.name + ": " +  vector as string )
	return vector
)

But this seems a bit cumbersome to me. Is there a faster way to return a vector oriented to the z axis of an object?

Thanks,
Rune

7 Replies

obj.transform.row3 – this is the z row of the objects transform matrix which is in effect a vector (normalized if the object is not non-uniformly scaled)

mark

Thanks for that fast answer, I will try that straight away!

I haven’t managed to get my head around matrices yet. There’s seems to be some mental block that prevents me from learning them – or perhaps I haven’t found a book that explains it in a way that I can understand

See my signature!

Btw, each node has a .dir property which is the Z axis (.row3) of the matrix.
Very easy to both get and set. Set .dir to a vector and your object will turn to that direction!

For example, if you have a Sphere and a Teapot, the vector from Teapot to Sphere is

theVector = $Sphere01.pos – $Teapot01.pos
$Teapot01.dir = theVector –turn the teapot to point at the sphere with its lid!

Hehe! I’ve been thinking about that for some time now – I’m planning on ordering that DVD when my next Turbo Squid payment ticks in

Rune

Cool – I’m playing around with that now. When you align things like that, is it possible to set some kind of up vector?

Rune

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Not with the DIR, but yes, the DVD shows that, too

You can build your own matrix easily – there is a related example in the MXS Reference that orients a UV gizmo using an up vector.

For example, the following will try to keep the Y axis of the Teapot as close to the world UP vector as possible:


theSphere = $Sphere01
theTeapot = $Teapot01
theZ = normalize (theSphere.pos - theTeapot.pos) --the vector to the object
theUp = [0,0,1] --the up vector in world space
theX = normalize (cross theZ theUp) --the x based on up and z vector
theY = normalize (cross theX theZ) --the y is based on the new x and the z vector
theTeapot.transform = matrix3 theX theY theZ theTeapot.pos --build a matrix!

Thanks again Bobo – I will try out your code (and take a proper look at Matrices with your DVD)!

Cheers,
Rune