Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with rotations in 2D around a particular point, using a scene graph


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 MrMark   Members   -  Reputation: 196

Like
0Likes
Like

Posted 19 May 2012 - 09:59 AM

Hi all

I'm getting myself confused with rotations in 2D when rotating about a point about another point. I'm using a simple tree as my scene graph. Each node knows its position relative to its parent, and there is a root node that is the ancestor of all nodes. So when I move a node all of its children move with it.

Rotations of a node about its parent are simple. But how do I rotate a node about another node ?

What I could find on the internet talks about translating the origin to the node being rotated around, rotating it, then translating back. So based on that, I came up with:

  • From the node to be rotated walk up the tree to the root node building a translation matrix (1)
  • From the origin node walk up the tree to the root node building a translation matrix (2)
  • inverse the origin node's translation (2) matrix
  • Form the origin translation matrix by multiplying matrix (1) by matrix (2), forming matrix (3)
  • Multiply the node to be rotated by matrix (3). It's now a vector relative to the rotation origin
  • apply rotation matrix
  • multiply the node by the inverse of matrix (3), its now a vector relative to its parent again.

But that seems somewhat over complicated, which makes me think I'm either doing it wrong, or have found a convoluted way of doing something simple.


Any help greatly appreciated.

Sponsor:

#2 haegarr   Crossbones+   -  Reputation: 4589

Like
1Likes
Like

Posted 19 May 2012 - 10:53 AM

First of all, using a scene graph is, well, deprecated, but using it and ignoring its natural parenting seems me strange. Why do you want to rotate around grand-parent nodes?

However, here is the solution:

1. Every node has a position relative to its parent node and expressed as translation matrix Ti for the i-th node. Every node has an orientation relative to its parent node and expressed as rotation matrix Ri for the i-th node. Then the local transformation matrix of the i-th node is given by (using row vectors here)
Li := Ri * Ti

2. The belonging global matrix, i.e. the transformation matrix relative to the overall common reference space (a.k.a. "the world") is computed as
Wi := Li * Wi-1 = Li * Li-1 * Li-2 * ... * L1
where Wi-1 denotes the global transformation matrix of the parent node. Please notice the recursive definition.

3. The center of rotation should be given by a node above the current one, e.g. Wj with 0 < j < i. As your internet source correctly stated, you have to make this temporarily the origin. You do this by applying the inverse matrix, because you go from global to local space. Hence
Wi * Wj-1 = Li * Li-1 * ... * Lj+1
would do the trick. Now apply the rotation
Wi * Wj-1 * R
and finally shift the temporary center back to its original position:
Wi' := Wi * Wj-1 * R * Wj

4. If you want to know the local transformation, then do
Li' = Wi' * Wi-1-1

Comparing this with the prescription given in the OP, you'll see the going from the current node up to the grand-parent node in the index sequence i-1 ... j+1 in the formulas above. Of course, you don't really need to compute Wj and Wj-1 to just yield in the desired result Li', because of
Wi' * Wi-1-1 = Wi * Wj-1 * R * Wj * Wi-1-1 = ( Li * Li-1 * ... * Lj+1 ) * R * ( Lj+1-1 * ... * Li-1-1 )
where the parentheses are only used for clarity (i.e. there is no mathematical necessity). However, due to the parenting mechanism you'll have to implement the computation of W anyway.


EDIT: Please check the formulas twice before using it, because mistaking an index is done quickly ;(

Edited by haegarr, 19 May 2012 - 11:22 AM.


#3 MrMark   Members   -  Reputation: 196

Like
0Likes
Like

Posted 20 May 2012 - 02:03 AM

Thanks for an awesomely detailed post haegarr. I was able to follow the maths (which is rare =P).

Quick question, why are scene graphs deprecated ?

#4 haegarr   Crossbones+   -  Reputation: 4589

Like
1Likes
Like

Posted 21 May 2012 - 03:20 PM

Quick question, why are scene graphs deprecated ?

Scene graphs are an example of violating the principle of single responsibility. It is used for logical grouping, assembling models, defining parent-child transformation hierarchies, doing spatial partitioning, ... all at once.

However, a single structure cannot do all this without enforcing restrictions. E.g. a scene graph naturally introduces a hierarchical bounding volume scheme. But what if you want to use a binary space partitioning scheme instead? Just an example. You'll find dozen of threads about this theme in GDnet as well as some articles on the internet.

I don't say that scene graphs are senseless, but IMHO using a single structure for each kind of job is the way to go today.

Edited by haegarr, 21 May 2012 - 03:22 PM.


#5 ProgrammerDX   Members   -  Reputation: 175

Like
1Likes
Like

Posted 21 May 2012 - 04:07 PM

Yes Scene Graphs are frustrating when you try to force everything inside the Scene Graph design.. it's just annoying and stupid to follow such goal.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS