mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-15 22:45:00 +01:00
better rotation mechanics
This commit is contained in:
parent
3bf79290bb
commit
35c47c50b8
10 changed files with 98 additions and 8 deletions
28
src/main/java/core/ExMath.java
Normal file
28
src/main/java/core/ExMath.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package core;
|
||||
|
||||
/**
|
||||
* Extended Math functions for the game engine
|
||||
*/
|
||||
public class ExMath {
|
||||
|
||||
private ExMath(){
|
||||
}
|
||||
|
||||
/**
|
||||
* Lerp an angle in radians
|
||||
*
|
||||
* @param from The start angle
|
||||
* @param to The target angle
|
||||
* @param t The value
|
||||
* @return The new angle
|
||||
*
|
||||
* @see <a href="https://gist.github.com/shaunlebron/8832585"angleLerp</a>
|
||||
*/
|
||||
public static double angleLerp(double from, double to, double t) {
|
||||
double max = Math.PI * 2;
|
||||
double da = (to - from) % max;
|
||||
double shortAngleDist = 2 * da % max - da;
|
||||
|
||||
return from + shortAngleDist * t;
|
||||
}
|
||||
}
|
||||
|
|
@ -133,6 +133,30 @@ public class Vector2D {
|
|||
return new Vector2D(rotatedX + center.x, rotatedY + center.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate a point around another point
|
||||
*
|
||||
* This method can now be trusted
|
||||
*
|
||||
* @param center The center of the rotation
|
||||
* @param value The point to be rotated, absolut to the center
|
||||
* @param rotation The rotation angle in radians
|
||||
* @return The rotated point
|
||||
*/
|
||||
public static Vector2D rotateAround(Vector2D center, Vector2D value, double rotation){
|
||||
return rotateAround(center, value, rotation, COUNTERCLOCKWISE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a unit vector with magnitude 1 and the direction
|
||||
* @param direction The direction of the vector
|
||||
* @return The unit vector
|
||||
*/
|
||||
public static Vector2D getUnitVector(double direction){
|
||||
return rotateAround(new Vector2D(), new Vector2D(0, 1), direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy this object
|
||||
* @return A copy of this object
|
||||
|
|
@ -165,4 +189,8 @@ public class Vector2D {
|
|||
if (Double.compare(vector2D.x, x) != 0) return false;
|
||||
return Double.compare(vector2D.y, y) == 0;
|
||||
}
|
||||
|
||||
public double getRotation() {
|
||||
return Math.atan2(y, x);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue