Mathematics
Games often need small math calculations for movement, randomness, and keeping values in a valid range. Saxion GameApp provides helper methods to make this easier and more readable.
Distance and angles
Sometimes, you want to know how far apart two points are, or what direction one object is relative to another.
Distance between points
Angle from first point to second point
- 0° points to the right (positive X axis)
- Angles increase counter-clockwise
Calculate movement in a direction
Given a starting position, the distance that should be travelled and the angle, calculate what the next point is going to be. The method returns an array with two elements. Element 0 is the new x-coordinate, element 1 is the new y-coordinate.
float[] newPos = GameApp.moveInDirection(x, y, distance, directionDegrees);
float newX = newPos[0];
float newY = newPos[1];
- 0° points to the right (positive X axis)
- Angles increase counter-clockwise
- distance is how far to move from the starting point
Clamping values
Sometimes you want a value to stay within a certain range. For example: - Prevent a character from moving off the screen - Limit health between 0 and maximum health
You can use the clamp() method for this.
What it does:
- Returns value if it is inside [min, max]
- Returns min if value < min
- Returns max if value > max
Example: Ensure that the health of a player never goes below 0 or above 100.
Random values
Games often need random numbers, for example:
- Random enemy positions
- Random loot drops
- Random effects or animations
- Random colors
Saxion GameApp contains various random methods.
Tip: Use these random methods to make your game more dynamic and fun by varying positions, speeds, colors, or enemy types.
Random float
This method will generate numbers in a specified range. The lower bound is inclusive, the upper bound is exclusive. So when executing GameApp.random(0f, 1f), the minimum value is 0 and the maximum value is 0.999999999...
Random int
This method will generate integers in a specified range. The lower bound is inclusive, the upper bound is exclusive.
Example:// 0 = minimum number, 9 = maximum number generated (since upper bound is exclusive)
int menuItem = GameApp.randomInt(0, 10);
Random item from array
You can pick a random element from any array (String[], float[], int[], or objects). The array must not be empty.
Example:
float[] speeds = {1.0f, 2.5f, 3.0f};
float randomSpeed = GameApp.random(speeds);
int[] positions = {0, 50, 100, 150};
int randomPosition = GameApp.random(positions);
String randomColor = GameApp.random(GameApp.getAllColors());
Random item from list
You can also pick a random element from a List. The list must not be empty.
ArrayList<String> enemies = new ArrayList<>();
enemies.add("Orc");
enemies.add("Goblin");
enemies.add("Troll");
String randomEnemy = GameApp.random(enemies);
Other mathematical methods
There are multiple mathematics classes available to you in Java. You can use the Math class from the Java Library. In addition you can use the MathUtils from LibGDX.