Last active
January 9, 2021 00:24
-
-
Save ModsByMorgue/77065d84a85675f4b4602cc7a366ddb6 to your computer and use it in GitHub Desktop.
jMonkeyEngine Direction Utility Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package mygame; | |
| import com.jme3.math.FastMath; | |
| import com.jme3.math.Quaternion; | |
| import com.jme3.scene.Spatial; | |
| /** | |
| * Converts an object's yaw rotation value into a degree between 0 and 360. | |
| * This is meant to emulate a real-world compass. | |
| * | |
| * An example usage: | |
| * Spatial character = ...; | |
| * String direction = DirectionUtil.getDirectionAsString(DirectionUtil.getRotationAsDegree(character)); | |
| * System.out.println("Character is Facing: " + direction); | |
| */ | |
| public class DirectionUtil { | |
| /** | |
| * @param spatial The spatial, node, or geometry to get the rotation from. | |
| * @return The compass direction using the given degree (between 0 and 360). | |
| */ | |
| public static float getRotationAsDegree(Spatial spatial) { | |
| return quaternionToDegree(spatial.getWorldRotation()); | |
| } | |
| /** | |
| * Gets the Yaw value from a Quanternion and returns a degree. | |
| * | |
| * @param rotation The objects's rotation. | |
| * @return The yaw rotation, as a degree. The value is between 0 and 360. | |
| * @author morgue, Darkchaos, Riccardo, polinc, noncom, pspeed42 | |
| */ | |
| public static float quaternionToDegree(Quaternion rotation) { | |
| // Get pitch, yaw, roll (x, y, z axis) as radians. | |
| float[] angles = rotation.toAngles(null); | |
| // Get the yaw value. | |
| // The value is a radian, which means it's between 0 and 3.2f | |
| float rotDeg = angles[1]; | |
| // Multiply the radian value to get the degree. | |
| rotDeg *= FastMath.RAD_TO_DEG; | |
| // The current value is between -180 and 180. | |
| // Add 180 to the final result for a value between 0 and 360. | |
| rotDeg += 180; | |
| return rotDeg; | |
| } | |
| /** | |
| * Determines the directional name based on the given rotation degree. | |
| * | |
| * @param rotDeg The calculated rotation degree. | |
| * @see #getRotationAsDegree(com.jme3.scene.Spatial) | |
| * @see #getRotationAsDegree(com.jme3.math.Quaternion) | |
| * @return The direction name as a String. | |
| */ | |
| public static String getDirectionAsString(float rotDeg) { | |
| if (rotDeg >= 0 && rotDeg <= 22.5 || rotDeg >= 337.5 && rotDeg <= 360) { | |
| return "North"; | |
| } | |
| if (rotDeg >= 22.5 && rotDeg <= 67.5) { | |
| return "North East"; | |
| } | |
| if (rotDeg >= 67.5 && rotDeg <= 112.5) { | |
| return "East"; | |
| } | |
| if (rotDeg >= 112.5 && rotDeg <= 157.5) { | |
| return "South East"; | |
| } | |
| if (rotDeg >= 157.5 && rotDeg <= 202.5) { | |
| return "South"; | |
| } | |
| if (rotDeg >= 202.5 && rotDeg <= 247.5) { | |
| return "South West"; | |
| } | |
| if (rotDeg >= 247.5 && rotDeg <= 292.5) { | |
| return "West"; | |
| } | |
| if (rotDeg >= 292.5 && rotDeg <= 337.5) { | |
| return "North West"; | |
| } | |
| return "Unknown: " + rotDeg; | |
| } | |
| /** | |
| * Snaps the rotation degree to the nearest directional value. | |
| * @param rotDeg The calculated rotation degree. | |
| * @return The nearest directional value. | |
| */ | |
| public float snapToNearestDirection(float rotDeg) { | |
| if (rotDeg >= 0 && rotDeg <= 22.5 || rotDeg >= 337.5 && rotDeg <= 360) { | |
| return 0; | |
| } | |
| if (rotDeg >= 22.5 && rotDeg <= 67.5) { | |
| return 45; | |
| } | |
| if (rotDeg >= 67.5 && rotDeg <= 112.5) { | |
| return 90; | |
| } | |
| if (rotDeg >= 112.5 && rotDeg <= 157.5) { | |
| return 135; | |
| } | |
| if (rotDeg >= 157.5 && rotDeg <= 202.5) { | |
| return 180; | |
| } | |
| if (rotDeg >= 202.5 && rotDeg <= 247.5) { | |
| return 225; | |
| } | |
| if (rotDeg >= 247.5 && rotDeg <= 292.5) { | |
| return 270; | |
| } | |
| if (rotDeg >= 292.5 && rotDeg <= 337.5) { | |
| return 315; | |
| } | |
| return rotDeg; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment