Notifications
Clear all

[Closed] Rotation based on normalized vector?

Hi everyone,

I’ve been banging my head against a wall for the last few hours trying to solve this with my limited grasp of manipulating vectors – any help would be hugely appreciated!

I have an object which I want to “aim” along its direction of movement – similar to the ‘follow’ option in a path constraint controller. I have a vector defined by subtracting the position of the object on the current frame from its position on the next frame. I have then divided this vector by its length to get a unit vector – for example [-0.670856,-0.741587,0]. I now need to rotate the object to look towards its position on the next frame.

Phew…!

Hope that makes sense to someone out there…

Thanks in advance

Duncs

6 Replies
 PEN

Best way is to now build a transform matrix from this and an up vector.

Some thing like this.


 x=normalize (p2-p1)
 z=[0,0,1]
 y=normalize (cross z x)
 (matrix3 x y z p1)
 

Paul, many thanks for the swift response – I must admit, a lot of the reading I’ve done that has got me to this point has come from the tutorials and articles on your site. It’s an awsome resource so thanks again…

I can kind of half get the transform matrix working – although I’d be lying if I said I understood what its actually doing. This is what I have so far but I suspect I’m not applying the transformation correctly as it seems to move my object as well as rotating it…

x=normalize (p2-p1)
z=[0,0,1]
y=normalize (cross z x)
myMatrix = (matrix3 x y z p1)
$.rotation = myMatrix as eulerangles

 PEN

A couple things here. One $.rotation is a quat value so you can just do myMatrix.rotation and two you will need to do it in the space of the object that you are applying it to so that it doesn’t move. Try…


 in coordsys (transmatrix $.pos) $.rotation=theMatrix.rotation
 

Thanks, I really appreciate you taking the time to reply…

It’s getting closer. The object no longer moves away from it’s keyframed trajectory but the rotations I’m getting aren’t quite right. I’m going to have a further dig around in the Maxscript documentation to try and get my head around transform matrices and matrix3 values to see if I can uncover the problem.

Also, I’ve attached the simple test scene I’m working on. I’m running the following code with Cone01 selected:

animate on
for i = 0 to 35 do
(
p1 = at time i $.pos
p2 = at time (i+1) $.pos
x=normalize (p2-p1)
z=[0,0,1]
y=normalize (cross z x)
myMatrix = (matrix3 x y z p1)
at time i in coordsys (transmatrix $.pos) $.rotation=myMatrix.rotation
)

the scene is here: www.mojofuel.com/download/aimTest_01.zip

Once again thanks for your input

Duncs

PEN, you’re a lifesaver! It works… I just needed to invert the rotation section of the matrix and now I have predictable results. I also changed the axis order when constructing myMatrix so that Y would be the ‘follow’ axis. I’m going to build this into my character rig now along with a rolling function – will post some animation tests soon. The code below is what I’m currently using.

animate on
for i = 0 to 50 do
(
p1 = at time i $.pos
p2 = at time (i+1) $.pos
if p1 != p2 do
(
x=normalize (p2-p1)
z=[0,0,-1]
y=normalize (cross z x)
myMatrix = (matrix3 y x z p1)
at time i in coordsys (transmatrix $.pos) $.rotation=inverse(myMatrix.rotation)
)
)

I don’t think I’d have ever arrived at this without your help so thanks again…

Duncs:thumbsup:

 PEN

Ok, I missed something as well. With what i first showed we don’t end up with an orthangonalized matrix, or in simpler terms it isn’t at right angles or you could say that it is skewed. In the example below I set the up vector to the world Z and then re calc it so that it is at a right angle to the x and y. This will orthagonalize the matrix. There is also a function that will do this for you how ever I have usualy perfered to do it my self so that I have control over the axis that is changed.

And yes, sorry you need to invert the rotation. Hey I was writting it all off the top of my head. Sorry, I should have mentioned that.


 x=normalize (p2-p1)
z=[0,0,1]
y=normalize (cross z x)
z=normalize (cross x y)
(matrix3 x y z p1)