• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GameGeazer

Help with matrix rotation

3 posts in this topic

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
0

Share this post


Link to post
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
2

Share this post


Link to post
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
0

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0