single axis aim constraints

david | tutorials | Tuesday, December 9th, 2008

You can use an aim constraint to make one object face another, but what if you also want that object to only pivot around a single axis. An example of when you might want this behavior is a candle flame made of a simple texture mapped plane. To create the illusion of a 3d flame you need it to face the camera, but you only want it to rotate around its y-axis so that the flame stays vertical even if the camera moves up higher than the candle.

To do this you could simply make an aim constraint and select only the y-axis to constrain, but you would discover that it doesnt work they way you might expect.

Here is a very simple example to show the problem and solution. To make it really obvious I'm using a pointer shaped object and I will show how to use an aim constraint to make its z-axis point to the camera, but only rotate about the y-axis. Here is a perspective view of my scene.

I select the camera, shift-select the pointer and create the aim constraint with the following options.

So I have constrained only the y-axis. If I move the camera around, the pointer object follows it, but it always fails at some point as you can see in the next picture, which is a top view.

Here the pointer is about 90 degrees out of alignment.

If I change the rotate order of the pointer object to YZX, I can correct this problem.

Which gives me this

But there is still a problem. If I translate my camera upwards in the y-axis, then the aligment of my pointer starts to drift again. The next picture is similar to the one above, but the camera is much higher now.

My solution is to not use a single axis aim constraint. Instead I constrain all axes with the aim constraint but I change what they are aiming at.

In the next picture, a side view, I have gone back to my scene with no constraints. To get things set up correctly I rotated my pointer to point along the z-axis to where I placed my camera. Directly under my camera I placed a sphere. Notice how the sphere is sitting at the same height as the pointer - this is important.

Next I select the sphere and shift-select the pointer and create an aim constraint with the following options.

Then I select the camera and shift-select the sphere and create a point constraint with the following options.

Notice that I have only constrained the x and z axis leaving the height of the sphere unconstrained.

Now when I move the camera the sphere follows along but it always stays at the same height. This alows the pointer object to aim at the sphere and to rotate only about the y-axis, even though all 3 rotation axes are aim constrained. Here is that top view again with the correct result this time.

It is intersting to look at the xyz rotations that end up being used on the pointer object. You may be surprised to see non-zero values for x and z.

In my example I have a fractional rotate x result because I placed my sphere height by eye. Its close enough though, and helps to show why a single aim constraint doesn't work properly.

Now if in my original example, my candle was moving around things would be more complicated and I would need to find a way to keep the sphere at the correct height - point constrain translate-y to the candle maybe.

5 Comments »

  1. You can do this with a short expression that takes the acos of the dot product of two vectors. Because it is a 2D rotation on the X-Z plane, you use 2d vectors and leave Y at 0. Assuming I had a cube shape pointing down the Z axis aimed at a camera:

    ---code in next comment because wordpress formatting is messing it up---

    -shawn

    Comment by yourdaftpunk — December 12, 2008 @ 10:36 am

  2. WordPress killed the code:
    http://www.djx.com.au/mel/angleBetweenTwoVectors.txt

    Comment by yourdaftpunk — December 12, 2008 @ 10:44 am

  3. Thanks for showing us another way. I'll try that out.

    And sorry wordpress didn't like your code. I'll see if I can rectify that somehow.

    Comment by david — December 12, 2008 @ 7:42 pm

  4. Have you tried setting the rotation order to XZY? Having Y at the top of the rotation order list seems to force all the transforms to apply only to Y. I have an example file with an arrow pointing at a sphere that I'm moving around the scene in my persp camera and all is good.

    Great site btw! :) You're tool for freezing out the rotation axis is coming in handy.

    Comment by verbal007 — May 21, 2009 @ 11:39 am

  5. No I'd never tried that. And it works just as you said. Much easier than my solution.

    Quick summary: A single axis aim constraint works the way we want if the constrained axis comes first in the rotation order (which means last in the way it appears in the attribute editor). So to aim constrain just the Y axis, set the rotation order to XZY.

    Thanks for the tip!

    Comment by david — May 22, 2009 @ 12:12 am

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress | Based on a theme by Roy Tanck