# Rotating Geometry towards a point

Hello.

I want to create method that rotates one geometry to face a point, but with a maximum rotation angle. For example:

If I have one object at (0,0,0) facing down the negative Z axis and a point i want to look at at (1,0,0) then the first call to this method should rotate the first object to look along an axis which would look something like (0.1, 0, -0.9).

In order to get the first object to actually face the second one would actually have to call this method several times. I know this is not performance optimal and all, but it is what I want. Also, I know that one should probably consider what should be the “up” axis for the camera, but in this case it does not matter. So basically I want a method that takes a Geometriy and a Vector3f as arguments and rotates the first to face the second slightly.

Anyone have a good solution for this?

last time many people ask about rotation towards the point for example:

http://hub.jmonkeyengine.org/groups/general-2/forum/topic/rotate-camera-with-fixed-lookat/

Yes you must know upVector fo lookAt method for a geometry you can just hold it in your extended class. Camera have its own upVector and for it you can do:

[java]

camera.setLocation(loc);

Quaternion leftRotation = new Quaternion();

leftRotation.fromAngleAxis((-FastMath.HALF_PI / 20) * rotation, Vector3f.UNIT_Y);

Vector3f rotationResult = loc.add(leftRotation.mult(new Vector3f(0f, 30f, 30f)));

camera.setLocation(rotationResult);

camera.lookAt(loc, Vector3f.UNIT_Y);

[/java]

then you just must know max rotation and minimum :). and if you want to rotate in other vector direction then change the code

Thanks for the fast reply, but this is not really what I am after. Atleast I don’t think so.

My object must be able to rotate to face the point around any arbitrary axis, so that using indidvidual code snippets for X Y and Z will not do the trick. Also, I really don’t want to do keep track of the up vector because I can take any vector for that, as long as the object faces the right way.

I know that I can get a rotation axis using the cross product of my current direction vector and the vector that points towards this point. So I feel that somehow I should be able to rotate my object around this axis to achieve the proper effect. I have tried creating a quaternion using said axis and a rotation angle but this only seems to work for some special vectors, and others instead send me off in the opposite direction, or produce something that feels very much like a gimbal lock. So more input would be great. Here is the code that I am pefroming now. It works for some points and directions and not for others. Maybe it can further help explaning what I am trying to do

[java]public void turn(Vector3f target) {

target = target.subtractLocal(geometry.getLocalTranslation()).normalizeLocal();

Vector3f direction = this.playerRotation.mult(Vector3f.UNIT_Z.negate()).normalizeLocal();

Vector3f rotationAxis = target.cross(direction).normalizeLocal();

Quaternion q = new Quaternion().fromAngleAxis(0.01f, rotationAxis);

this.playerRotation.multLocal(q);

this.geometry.setLocalRotation(playerRotation);

}[/java]

Use direction vectors and create Quaternions using lookAt when you need them, its much easier.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies