PDA

View Full Version : Object orientation to vector


SuperRune
05-14-2007, 09:14 PM
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

marktsang
05-14-2007, 09:25 PM
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

SuperRune
05-14-2007, 09:34 PM
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 :)

Bobo
05-14-2007, 10:50 PM
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!

SuperRune
05-14-2007, 10:53 PM
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 :thumbsup:

Rune

SuperRune
05-14-2007, 10:55 PM
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!

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

Bobo
05-14-2007, 11:04 PM
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

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!

SuperRune
05-14-2007, 11:10 PM
Thanks again Bobo - I will try out your code (and take a proper look at Matrices with your DVD)!

Cheers,
Rune

CGTalk Moderation
05-14-2007, 11:10 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.