# Help with matrix rotation

This topic is 1673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I'm currently working on a project that requires me to create a bunch of objects, rotate them locally, then revolve them all around a point.

I have a parent Node that contains all the objects I create so my idea was to create each object, rotate it to my desired orientation, translate it, add it to the parent node, and then rotate the parent node and have it recursively rotate the children.

My problem is that with multiple rotations everything starts to go haywire. I've spent a couple of days on this and haven't made much progress so if someone could point me in the right direction that would be wonderful!

I have a Matrix for the model and a Vector3 to keep track of the translation

	protected Matrix4f model = new Matrix4f();
protected Vector3 translation = new Vector3();


This is the code I'm using to rotate the objects.

public SceneNode rotate(float rot, float rx, float ry, float rz) {
for (Node<SceneNode> node : this.getSubnodes()) {
((SceneNode) node).rotate(rot, rx, ry, rz);
}
model.rotate(rot, rx, ry, rz);
return this;
}

public SceneNode rotateLocal(float rot, float rx, float ry, float rz) {

for (Node<SceneNode> node : this.getSubnodes()) {
((SceneNode) node).rotateLocal(rot, rx, ry, rz);
}
float x = getX();
float y = getY();
float z = getZ();

model.translate(-x, -y, -z);
model.rotate(rot, rx, ry, rz);
model.translate(x, y, z);
return this;
}


this is the code I'm using to translate the objects

	public SceneNode translate(float x, float y, float z) {
for (Node<SceneNode> node : this.getSubnodes()) {
((SceneNode) node).translate(x, y, z);
}
model.translate(x, y, z);
translation.set(x, y, z);
return this;
}

This is the code where I'm creating the child node(bondObject)and attaching it to the parent(parentNode)
bondObject.rotateLocal(angle, x, y, z);
bondObject.setTranslation(x, y, z);

parentNode.attach(bondObject);


And I want to rotate it every frame like this

parentNode.rotateLocal(1, 1, 1, 0);

Edited by GameGeazer

##### Share on other sites

If I understand your description correctly, your node system would be similar to a merry-go-round or a solar system.

The process would be:

1. For each node, maintain a translation matrix for it's location with respect to its parent node in the rest position - i.e., before any rotations have taken place.

2. Rotate each object/node locally - i.e., about it's own origin. Each time the angle changes, calculate a rotation matrix for the total rotation. Alternatively, maintain a rotation matrix for each node. When a new delta angle is to be applied, node-rotation-matrix = node-rotation-matrix * new-local-angle-rotation-matrix.

3. Starting with the root node, calculate matrices : node-matrix = node-rotation-mat * node-translation-mat

4. Work through each child, calculating a world matrix for each child node = child-rotation-mat * child-translation * parent-world-matrix

That is, each pass through the loop: the idea is to rotate each object locally (as if it were at the origin), translate it relative to it's parent's origin (as if it's parent were at the origin), apply it's parent's rotation, apply it's parent's translation, etc. - in that order.

You'll end up with a new world matrix for each object each pass through the loop.

Hopefully that's somewhat clear. It's late.

Edited by Buckeye

##### Share on other sites

So when I'm rotating I can simply multiply the old_rotation by the new_rotation? The only difference between world and local being I translate local back to the origin before rotating?

I'm a bit confused as to how you tell the difference between a world and local rotation if the translation is in a separate matrix. As  of now I'm storing my world and local rotation separately and multiplying world * translation* local, which seems to work fine but do you know how it's usually done?

4. That's an interesting idea, as of now the child is not in respect to the parent but your method looks very elegant.

Thank you very much that helped a lot.

Edited by GameGeazer

##### Share on other sites

I'm a bit confused as to how you tell the difference between a world and local rotation if the translation is in a separate matrix.

In the method I described, you create a new world matrix for each node each pass through the loop. You can't "reuse" it the next time through because it has the parent's rotation and translation mixed in with it.

I translate local back to the origin before rotating

First, it isn't clear what your translate() and rotate() functions do. But there's no need to translate back to the origin each time because, each loop, you should start with the each node at the origin.

The most common way of handling a system like this is a "hierarchical" construction. You have a root node (scene node) which has children that rotate about it and translate with it. And each child can rotate independently on its own axis.

Consider an Earth and Sun model system in which the Earth rotates on its axis once a day. The Earth rotates about the Sun once a year, and the Sun translates within the galaxy by some amount each century (e.g., 1000 kilometers/100 years = 10K/yr). I'm ignoring the Sun rotating on it's own axis to better simulate what you've described for your system.

NOTE: Below I use the multiplication order you appear to be using.

Initialization:

Create and keep an Earth-translate-mat = translate by 93 million miles along the X-axis. This matrix will never change. It's the starting position of the Earth with respect to the Sun. The Earth's position will be changed later when that translation gets rotated by the Sun's rotation - liking swinging a rock at the end of a 3-foot rope. It's always a 3-foot rope but get's rotated by your hand swinging the rope.

Each time through the loop:

1. Earth-rot-mat = rotate by angle for the time-of-day --> angle = current-hour * 2*Pi / 24hrs

2. Earth-node-mat = Earth-translate-mat * Earth-rot-mat

3. Sun-rot-mat = rotate by angle for the day-of-the-year (2*Pi / 365 days) = current-day * 2 * Pi / 365 days

4. Sun-translate-mat = translate( current-year * 10K / yr )

5. Earth-Render-mat = Sun-translate-mat * Sun-rot-mat * Earth-node-mat

Now render the Earth object with Earth-Render-mat, and render the Sun with Sun-translate-mat.

Hope that helps.

Edited by Buckeye

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×