1

In JavaFX the rotateProperty of a Node provides me with its rotation in degree relative to its parent. Is there a way to get the absolute rotation in degree, either relative to the Scene or the Screen?

For example, is there a way to calculate the angle from the Transform object of a Node i can get from getLocalToSceneTransform()?

Mirko Fetter
  • 186
  • 1
  • 13
  • Hi, welcome to Stack Overflow. Please show us what you have tried and post the error/problem that you are experiencing. – Stefan May 19 '14 at 15:55
  • So far i tried Node.getRotate() which provides me with the relative rotation to its parent. A current quick and dirty solution is to recursively call getParent() and accumulate the values of getRotate() until i receive getParent()=null and accordingly reach the root of the scene node. However, i am not sure how robust this solution is. As there are methods like localToScene(), to get the bounds for a node in an absolute manner, i wondered if there is a more elegant way. – Mirko Fetter May 19 '14 at 16:25

2 Answers2

3

So, i did the math myself, and for my case i either get the rotation in Radians via:

double xx = myNode.getLocalToSceneTransform().getMxx();
double xy = myNode.getLocalToSceneTransform().getMxy();
double angle = Math.atan2(-xy, xx);

or

double yx = myNode.getLocalToSceneTransform().getMyx();
double yy = myNode.getLocalToSceneTransform().getMyy();
double angle = Math.atan2(yx, yy);

In both cases this can be converted to 360-degrees:

angle = Math.toDegrees(angle);
angle = angle < 0 ? angle + 360 : angle;
Mirko Fetter
  • 186
  • 1
  • 13
0

The question isn't really well defined, since different Nodes in the Scene graph are potentially rotated about different axes.

The getLocalToSceneTransform() method will return a Transform representing the transformation from the local coordinate system for the node to the coordinate system for the Scene. This is an affine transformation; you can extract a 3x4 matrix representation of it relative to the x- y- and z-axes if you like.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks James_D. To better define it: I am only interested in the 2D rotation as it is obtained from `Node.getRotate()`. Your suggestion of using the `Transform` object from `getLocalToSceneTransform()` is already one suggestion i made in my question - I still do not know how to get/calculate the angle from this object. Trying to cast it to `Rotate` did not work. – Mirko Fetter May 20 '14 at 09:40
  • A rotation in the xy-plane..so, presumably around the z-Axis? And according to the [Java 8 API Doc](http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#rotateProperty) "the pivot point about which the rotation occurs is the center of the untransformed `layoutBounds`" for the `Node.rotate()` operation. Thanks – Mirko Fetter May 21 '14 at 11:41
  • You may need to learn (or think about) some linear algebra and geometry. The point is that the accumulated rotations are about different points for each node in the scene graph hierarchy. So it's not even clear to me that accumulating all these rotations even results in just a rotation; it may be some more general affine transformation. – James_D May 21 '14 at 11:49