mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-15 23:26:21 +01:00
Add rotation methods
Adds the `rotateAroundX(double)`, `rotateAroundY(double)` and `rotateAroundZ(double)` methods
This commit is contained in:
parent
1a55644c5e
commit
118159d354
@ -279,6 +279,72 @@ public final class Vec implements Point {
|
||||
z * o.x - o.z * x,
|
||||
x * o.y - o.x * y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the x axis.
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
* @return a new, rotated vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vec rotateAroundX(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double newY = angleCos * y - angleSin * z;
|
||||
double newZ = angleSin * y + angleCos * z;
|
||||
return new Vec(x, newY, newZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the y axis.
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
* @return a new, rotated vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vec rotateAroundY(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double newX = angleCos * x + angleSin * z;
|
||||
double newZ = -angleSin * x + angleCos * z;
|
||||
return new Vec(newX, y, newZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around the z axis
|
||||
* <p>
|
||||
* This piece of math is based on the standard rotation matrix for vectors
|
||||
* in three dimensional space. This matrix can be found here:
|
||||
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
|
||||
* Matrix</a>.
|
||||
*
|
||||
* @param angle the angle to rotate the vector about. This angle is passed
|
||||
* in radians
|
||||
* @return a new, rotated vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vec rotateAroundZ(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double newX = angleCos * x - angleSin * y;
|
||||
double newY = angleSin * x + angleCos * y;
|
||||
return new Vec(newX, newY, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a linear interpolation between this vector with another
|
||||
|
Loading…
Reference in New Issue
Block a user