# Rotate node around vector at non-origin

I’m trying to rotate an object around a point (vector /= 0,0,0), but this doesn’t work as expected.
Please have a look at the following method.
Can this be done more easily?

``````private void rotateAroundPoint(Node object, Vector3f point, Quaternion rotate) {
Vector3f offset = point.subtract(object.getWorldTranslation());

// Move pivot center to rotaion point:
object.move(offset);

// move all children back
List<Spatial> children = object.getChildren();
for (Spatial child : children) {
child.move(offset.negate());
}

object.setLocalRotation(rotate);
// object.rotate(rotate);

// move center back
Vector3f offset2 = object.getLocalTranslation();
object.move(offset2.negate());

// move all children
for (Spatial child : children) {
child.move(offset2);
}
}``````

Well, you don’t really say what it is doing that it’s not supposed to. I’m feeling luck so I will take a couple wild guesses…

First: object.setLocalRotation(rotate);
…will wipe out any existing rotation. Maybe that’s desired.

Moving the children back and forth does nothing because all of their positions were parent relative to begin with.

What is it that you are actually trying to do? It seems like your scene may be organized poorly for it.

This is my Update Method

``````@Override
public void simpleUpdate(float tpf) {

i = i + tpf;
rotate.fromAngleAxis(i, new Vector3f(0, 1, 0));

rotateAroundPoint(pivot, rot_point, rotate);
}``````

What I’m trying to do is to rotating my pivot around a freely chosen rotation point for a while and than chnage that point so my object rotates sround some other point (where the rotaion might have a different radius).

As said, just rotating around a point works as desired.
But the second part of rotateAroundAPoint doesn’t work as desired. I want to move my nodes center back to where it was in the beginning since my node is some object consisting of few spartials which I want to have at the center of that node so that when later translating, I can just move the center.

You haven’t really said why you want to do this seemingly strange thing. Just more detail about what.

If you truly need to do this then you will have to do the math yourself because scene graphs were not meant to work this way. Local positions are always relative to the parent… you can twist the parent any which way and it’s moving the whole reference frame. Just like if you turn your car everything inside of it moves.

Alternately, if you are trying to temporarily decouple parts of the scene graph… anyway, I’m tired of typing and guessing. Either explain what you are really doing or good luck with the math. You will have to figure out where you want things in world space and then translate them back to local space (worldToLocal).

I just want to rotate my node around a point.

Somewhere it said that it’s only possible to rotate arount the center - that’s why I try it that complicated…

@lordflash said: I just want to rotate my node around a point.

Somewhere it said that it’s only possible to rotate arount the center - that’s why I try it that complicated…

Yes, but your scene graph structure seems to fight you… and we don’t know why you are trying to rotate outside of the scene graph structure so we can’t even tell you if there’s a better way.

Thus, your only option is to compute the position yourself.

Check out the math for dummies tutorial, it shows how you can rotate a vector. A vector can represent an arbitrary point in space and by adding the rotated vector A to another vector B you can map the rotation around the point (vector B) by a certain radius (length of vector A).

So is there a better solution than this to rotate around a point?

``````
Vector3f rot_point = new Vector3f(-1f, 0, 0);
float angle = 0;
Quaternion rotation = new Quaternion();

@Override
public void simpleUpdate(float tpf) {

attachCoordinateAxes(new Vector3f(1f, 0, 0));

angle += tpf;
rotation.fromAngleAxis(angle, new Vector3f(0, 1, 0));

rotateAroundPoint(pivot, rot_point, rotation);
}

private void rotateAroundPoint(Node object, Vector3f point, Quaternion rotate) {
Vector3f offset = point.subtract(object.getWorldTranslation());

// Move pivot center to rotaion point:
object.move(offset);

// move all children back
List<Spatial> children = object.getChildren();
for (Spatial child : children) {
child.move(offset.negate());
}

object.setLocalRotation(rotate);
}
``````