Quaternion.lookAt

I have written a method that eases handling of rotation a lot, if people like it it could be put into Quaternion to enable everyone to use it like this:

someSpatial.getLocalRotation().lookAt( destination, up );



the method looks like this:


    /**
     * <code>lookAt</code> is a convienence method for auto-setting the
     * quaternion based on a direction and an up vector. It computes
     * the rotation to transform the x-axis to point into 'direction'
     * and the y-axis to 'up'.
     *
     * @param direction
     *            where to look at in terms of local coordinates
     * @param up
     *            a vector indicating the local up direction.
     *            (typically {0, 1, 0} in jME.)
     */
    public void lookAt(Vector3f direction, Vector3f up ) {
        tmpYaxis.set( direction ).normalizeLocal();
        tmpZaxis.set( up ).crossLocal( direction ).normalizeLocal();
        tmpXaxis.set( direction ).crossLocal( tmpZaxis ).normalizeLocal();
        fromAxes( tmpXaxis, tmpYaxis, tmpZaxis );
    }



What do you think?

got my vote…however insignificant that might be

Don't we already have Spatial.lookAt(…) ?

This has my vote as well, it allows for more general usage.

I think it's good too, but maybe it should be named "pointAt"? IMO it has a better ring to it when dealing with stuff like spaceships and the like.

renanse said:

Don't we already have Spatial.lookAt(...) ?

um, well, if I had found it, I could have omitted the testing 
but as Mojo said - it can be used in more cases if it's in Quaternion - Spatial.lookAt could simply use the one in Quaternion then
Per said:

I think it's good too, but maybe it should be named "pointAt"?

Well, a Quaternion cannot "look at" something, it can't "point at" something, either. But I'd keep lookAt as name, as it's named like that in Camera and Spatial and does quite (or exactly) the same.

You seem to be assuming that all games use one direction as the front of a model. One game may need to have the object rotated so the unrotated +x accesses points to the new vector while anther may need the