Node rotation problem

Hello,



I've been experimenting with a new all-purpose camera Class for my game. Camera rotation on a circle works fine, but when i come to rotating a node with my camera there is this bug, i cant figure out. There is a node, that should imitate a players head, rotating along with the camera as it circles around him.

The node rotates fine with my camera in 2 axes, but there is a third axe where it rotates too,  meaning that if i continue rotating the head will turn upside down. I'd really appretiate som help, please…



Here is the code for it:

public void updateTarget() {
      Vector3f rightTemp=new Vector3f();
      Vector3f difTemp=new Vector3f();
      
      difTemp.set(camera.getDirection());
      target.getLocalRotation().getRotationColumn(0, rightTemp);
      
      rightTemp.normalizeLocal();
      difTemp.normalizeLocal();
      float angle=rightTemp.angleBetween(difTemp);
      Vector3f rotAxis=rightTemp.crossLocal(difTemp).normalizeLocal();
      q.fromAngleAxis(angle, rotAxis);
      q.mult(target.getLocalRotation(),q2);
      target.setLocalRotation(q2);
   }

A little update:



I figured out the problem, now i just need a solvation.

The problem is that the rotation is not around the center of the node. The new question is, how to rotate around a center point?

I found rotation was off on my game until i got one of our modelers to place the 0,0 coordinate in the dead center of the model. Might be a good first start.

how to rotate around a center point? that is also my question, when I rotate an object make a move frome one axis but not from the center  so looks so strange when I turn arround an object.



can somebody show me with a source code how to rotate from the center some object?

re: the original post, it looks like that code is tracking the camera's orientation rather than it's relative position?



re: replies about rotation in general, take a look at this:



import com.jme.app.SimpleGame;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;

public class RotationEx extends SimpleGame {

   Quaternion q = null;
   Sphere s1 = null;
   Sphere s2 = null;
   Box b = null;
   Node n = null;
   
   @Override
   protected void simpleInitGame() {
      s1 = new Sphere("Sphere1", 10, 10, 2.0f);
      s2 = new Sphere("Sphere2", 10, 10, 2.0f);
      s1.setLocalTranslation(-10.0f, 0, 0);
      s2.setLocalTranslation(5.0f, 0, 0);
   
      b = new Box("Box", new Vector3f( -5.0f, -1.0f, -0.5f), new Vector3f( 0.0f, 1.0f, 0.5f));
      b.setLocalTranslation( 0, 5.0f, 0);
      q = new Quaternion();
      
      n = new Node ("centreOfRotation");
      n.attachChild(s2);
      rootNode.attachChild(s1);
      rootNode.attachChild(b);
      rootNode.attachChild(n);
      wireState.setEnabled(true);
   }
   
   protected void simpleUpdate() {
      q.fromAngleNormalAxis(1.0f * tpf, new Vector3f(0,1.0f,0) );
      n.getLocalRotation().multLocal(q);
      s1.getLocalRotation().multLocal(q);
      s2.getLocalRotation().multLocal(q);
      b.getLocalRotation().multLocal(q);
   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      RotationEx demo = new RotationEx();
      demo.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
      demo.start();
   }

}



Basically, just like with anything else, you have to tell the computer where the center of rotation is.
Coordinates 0,0,0 are how you do that - that is always the center of rotation.
Now, where you want that to be depends what the object is.
Example: A ball, you will probably want to rotate around it's physical centre.
With a bat however, that wouldn't be much use. You usually swing a bat around it's grip - so that's where you would have your origin (0,0,0).

In the example code: There are two spheres. Both are spinning around their centers.
This is accomplished by applying rotation directly to them.
One sphere is also orbiting around a central point. This is because it is attached to node n.
Node n is also spinning. Because the sphere is positioned 10 units away from n, it swings around that point.

Lastly, there is a box. The box is defined like so:


b = new Box("Box", new Vector3f( -5.0f, -1.0f, -0.5f), new Vector3f( 0.0f, 1.0f, 0.5f));



The x coordinates of it's corners are -5 and 0. That means the shape is entirely on the left side of the center point.
Just the right edge touches the centre point, which is why it swings around it's end.
If it were defined like this:


b = new Box("Box", new Vector3f( -2.5f, -1.0f, -0.5f), new Vector3f( 2.5f, 1.0f, 0.5f));



Then it would rotate around it's center, as the points are evenly spaced on either side of (0,0,0).

In short, like foobar says, if rotation isn't working the way you want, move the points around (0,0,0) to define what your object should rotate around. You can do this in the code for primitives, or in your modeller for loaded models.

Hope that helps.