I’ve pondered over whether or not to suggest this class for the engine or not, but the more I use rotations the more it makes sense to avoid repeating the same code.
The class optionally forces the input angles to stay between negative and positive TWO_PI so that the angles don’t become ridiculously huge, and provides a convenience method to turn those angles into a quaternion to rotate scene objects. The get and set methods for each axis are also useful for binding to a GUI.
At first it appears to be a bit of “hand-holding” but I find myself repeating this code again and again and it seems a lot tidier and consistent to wrap the functionality into a class of its own. A lot of users get confused on how to “get and set” rotations, too - and I think this makes it a lot easier to understand. It enforces a standard of best practice that makes it difficult to deviate from and avoids them coming into contact with quaternions and getting/setting the values on them.
Please feel free to voice your opinion on the subject.
// constructors... AxisRotation axisRotation = new AxisRotation(); // initialized to ( 0, 0, 0 ) AxisRotation axisRotation = AxisRotation.fromRadians(0, FastMath.HALF_PI, 0); AxisRotation axisRotation = AxisRotation.fromDegrees(0, 90, 0); // getters... float xInRads = axisRotation.getX(); float xInRads = axisRotation.get(Axis.X); float xInDegs = axisRotation.getInDegreesX(); float xInDegs = axisRotation.getInDegrees(Axis.X); // optional: don't force the angles to stay between -FastMath.TWO_PI and FastMath.TWO_PI axisRotation.setTruncateRotation(false); // setters... axisRotation.setX(FastMath.HALF_PI); axisRotation.set(Axis.X, FastMath.HALF_PI); axisRotation.setInDegreesX(90); axisRotation.setInDegrees(Axis.X, 90); // rotators... axisRotation.rotateX(FastMath.HALF_PI); axisRotation.rotate(Axis.X, FastMath.HALF_PI); axisRotation.rotateInDegreesX(90); axisRotation.rotateInDegrees(Axis.X, 90); axisRotation.rotate(0, FastMath.HALF_PI, 0); axisRotation.rotateInDegrees(0, 90, 0); // apply the rotation to a scene object. mySpatial.setLocalRotation(axisRotation.toQuaternion());