mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
make Vector3 immutable by default
This commit is contained in:
@@ -42,16 +42,18 @@ public class BiomeFunction implements Function<String> {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||
z.apply(implementationArguments, variableMap).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
BiomeProvider grid = arguments.getWorld().getBiomeProvider();
|
||||
|
||||
return grid.getBiome(arguments.getOrigin()
|
||||
.toVector3()
|
||||
.mutable()
|
||||
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ()))), arguments.getWorld().getSeed()).getID();
|
||||
y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ()))).immutable(), arguments.getWorld().getSeed()).getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -77,9 +77,9 @@ public class BlockFunction implements Function<Void> {
|
||||
|
||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||
try {
|
||||
Vector3 set = Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
Vector3.Mutable set = Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, variableMap).doubleValue(),
|
||||
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
|
||||
BlockState current = arguments.getWorld().getBlockState(set);
|
||||
if(overwrite.apply(implementationArguments, variableMap) || current.isAir()) {
|
||||
if(arguments.isWaterlog() && current.has(Properties.WATERLOGGED) && current.getBlockType().isWater()) {
|
||||
|
||||
@@ -37,17 +37,19 @@ public class CheckBlockFunction implements Function<String> {
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||
|
||||
|
||||
z.apply(implementationArguments, variableMap).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
String data = arguments.getWorld()
|
||||
.getBlockState(arguments.getOrigin()
|
||||
.toVector3()
|
||||
.mutable()
|
||||
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, variableMap)
|
||||
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
|
||||
y.apply(implementationArguments, variableMap)
|
||||
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
|
||||
.getAsString();
|
||||
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties
|
||||
else return data;
|
||||
|
||||
@@ -50,7 +50,7 @@ public class EntityFunction implements Function<Void> {
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||
|
||||
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).add(arguments.getOrigin()).add(0.5, 0, 0.5), data);
|
||||
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).mutable().add(arguments.getOrigin()).add(0.5, 0, 0.5).immutable(), data);
|
||||
platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class GetMarkFunction implements Function<String> {
|
||||
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||
|
||||
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
|
||||
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()));
|
||||
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable());
|
||||
return mark == null ? "" : mark;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,9 +59,10 @@ public class LootFunction implements Function<Void> {
|
||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
|
||||
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
|
||||
|
||||
|
||||
z.apply(implementationArguments, variableMap).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
String id = data.apply(implementationArguments, variableMap);
|
||||
|
||||
|
||||
@@ -70,8 +71,8 @@ public class LootFunction implements Function<Void> {
|
||||
Vector3 apply = Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, variableMap)
|
||||
.intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
||||
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();
|
||||
|
||||
try {
|
||||
BlockEntity data = arguments.getWorld().getBlockEntity(apply);
|
||||
if(!(data instanceof Container container)) {
|
||||
|
||||
@@ -51,8 +51,8 @@ public class PullFunction implements Function<Void> {
|
||||
BlockState rot = data.clone();
|
||||
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
|
||||
|
||||
Vector3 mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
||||
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
|
||||
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
|
||||
if(!arguments.getWorld().getBlockState(mutable).isAir()) {
|
||||
arguments.getWorld().setBlockState(mutable, rot);
|
||||
|
||||
@@ -45,7 +45,7 @@ public class SetMarkFunction implements Function<Void> {
|
||||
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
|
||||
FastMath.floorToInt(
|
||||
y.apply(implementationArguments, variableMap).doubleValue()),
|
||||
FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()), mark.apply(implementationArguments, variableMap));
|
||||
FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable(), mark.apply(implementationArguments, variableMap));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ public class StateFunction implements Function<Void> {
|
||||
|
||||
|
||||
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();
|
||||
try {
|
||||
BlockEntity state = arguments.getWorld().getBlockEntity(origin);
|
||||
state.applyState(data.apply(implementationArguments, variableMap));
|
||||
|
||||
@@ -50,9 +50,9 @@ public class CheckFunction implements Function<String> {
|
||||
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
Vector3 location = arguments.getOrigin().toVector3().clone().add(
|
||||
Vector3 location = arguments.getOrigin().toVector3Mutable().add(
|
||||
Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
|
||||
FastMath.roundToInt(xz.getZ())));
|
||||
FastMath.roundToInt(xz.getZ()))).immutable();
|
||||
|
||||
return apply(location, arguments.getWorld());
|
||||
}
|
||||
|
||||
@@ -13,10 +13,18 @@ public interface ReadableWorld extends World {
|
||||
return getBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
}
|
||||
|
||||
default BlockState getBlockState(Vector3.Mutable position) {
|
||||
return getBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
}
|
||||
|
||||
default BlockState getBlockState(Vector3Int position) {
|
||||
return getBlockState(position.getX(), position.getY(), position.getZ());
|
||||
}
|
||||
|
||||
default BlockState getBlockState(Vector3Int.Mutable position) {
|
||||
return getBlockState(position.getX(), position.getY(), position.getZ());
|
||||
}
|
||||
|
||||
BlockEntity getBlockEntity(int x, int y, int z);
|
||||
|
||||
default BlockEntity getBlockEntity(Vector3 position) {
|
||||
|
||||
@@ -12,18 +12,34 @@ public interface WritableWorld extends ReadableWorld {
|
||||
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3.Mutable position, BlockState data, boolean physics) {
|
||||
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3Int position, BlockState data, boolean physics) {
|
||||
setBlockState(position.getX(), position.getY(), position.getZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3Int.Mutable position, BlockState data, boolean physics) {
|
||||
setBlockState(position.getX(), position.getY(), position.getZ(), data, physics);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3 position, BlockState data) {
|
||||
setBlockState(position, data, false);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3.Mutable position, BlockState data) {
|
||||
setBlockState(position, data, false);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3Int position, BlockState data) {
|
||||
setBlockState(position, data, false);
|
||||
}
|
||||
|
||||
default void setBlockState(Vector3Int.Mutable position, BlockState data) {
|
||||
setBlockState(position, data, false);
|
||||
}
|
||||
|
||||
default void setBlockState(int x, int y, int z, BlockState data) {
|
||||
setBlockState(x, y, z, data, false);
|
||||
}
|
||||
|
||||
@@ -15,10 +15,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
|
||||
|
||||
public class Vector3 implements Cloneable {
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
public class Vector3 {
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
|
||||
private Vector3(double x, double y, double z) {
|
||||
this.x = x;
|
||||
@@ -30,40 +30,6 @@ public class Vector3 implements Cloneable {
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3 multiply(double m) {
|
||||
x *= m;
|
||||
y *= m;
|
||||
z *= m;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 add(double x, double y, double z) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 add(Vector3 other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 add(Vector3Int other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 add(Vector2 other) {
|
||||
this.x += other.getX();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
@@ -76,75 +42,6 @@ public class Vector3 implements Cloneable {
|
||||
return FastMath.invSqrtQuick(lengthSquared());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vector3 rotateAroundX(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double y = angleCos * getY() - angleSin * getZ();
|
||||
double z = angleSin * getY() + angleCos * getZ();
|
||||
return setY(y).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vector3 rotateAroundY(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double x = angleCos * getX() + angleSin * getZ();
|
||||
double z = -angleSin * getX() + angleCos * getZ();
|
||||
return setX(x).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Vector3 rotateAroundZ(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double x = angleCos * getX() - angleSin * getY();
|
||||
double y = angleSin * getX() + angleCos * getY();
|
||||
return setX(x).setY(y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the distance between this vector and another. The value of this
|
||||
* method is not cached and uses a costly square-root function, so do not
|
||||
@@ -171,74 +68,6 @@ public class Vector3 implements Cloneable {
|
||||
return FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* This method will always make sure the provided axis is a unit vector, to
|
||||
* not modify the length of the vector when rotating.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around. If the passed vector is
|
||||
* not of length 1, it gets copied and normalized before using it for the
|
||||
* rotation. Please use {@link Vector3#normalize()} on the instance before
|
||||
* passing it to this method
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Vector3 rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.clone().normalize(), angle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* Note that the vector length will change accordingly to the axis vector
|
||||
* length. If the provided axis is not a unit vector, the rotated vector
|
||||
* will not have its previous length. The scaled length of the resulting
|
||||
* vector will be related to the axis vector.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around.
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Vector3 rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
double x = getX(), y = getY(), z = getZ();
|
||||
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
|
||||
|
||||
double cosTheta = Math.cos(angle);
|
||||
double sinTheta = Math.sin(angle);
|
||||
double dotProduct = this.dot(axis);
|
||||
|
||||
double xPrime = x2 * dotProduct * (1d - cosTheta)
|
||||
+ x * cosTheta
|
||||
+ (-z2 * y + y2 * z) * sinTheta;
|
||||
double yPrime = y2 * dotProduct * (1d - cosTheta)
|
||||
+ y * cosTheta
|
||||
+ (z2 * x - x2 * z) * sinTheta;
|
||||
double zPrime = z2 * dotProduct * (1d - cosTheta)
|
||||
+ z * cosTheta
|
||||
+ (-y2 * x + x2 * y) * sinTheta;
|
||||
|
||||
return setX(xPrime).setY(yPrime).setZ(zPrime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of this vector with another. The dot product
|
||||
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
|
||||
@@ -251,50 +80,20 @@ public class Vector3 implements Cloneable {
|
||||
return x * other.getX() + y * other.getY() + z * other.getZ();
|
||||
}
|
||||
|
||||
public Vector3 normalize() {
|
||||
return this.multiply(this.inverseLength());
|
||||
}
|
||||
|
||||
public Vector3 subtract(int x, int y, int z) {
|
||||
this.x -= x;
|
||||
this.y -= y;
|
||||
this.z -= z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3 subtract(Vector3 end) {
|
||||
x -= end.getX();
|
||||
y -= end.getY();
|
||||
z -= end.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Vector3 setZ(double z) {
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Vector3 setX(double x) {
|
||||
this.x = x;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public Vector3 setY(double y) {
|
||||
this.y = y;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public int getBlockX() {
|
||||
return FastMath.floorToInt(x);
|
||||
@@ -345,11 +144,287 @@ public class Vector3 implements Cloneable {
|
||||
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
|
||||
}
|
||||
|
||||
public Vector3 clone() {
|
||||
try {
|
||||
return (Vector3) super.clone();
|
||||
} catch(CloneNotSupportedException e) {
|
||||
throw new Error(e);
|
||||
public Mutable mutable() {
|
||||
return new Mutable(x, y, z);
|
||||
}
|
||||
|
||||
public static class Mutable {
|
||||
private double x, y, z;
|
||||
|
||||
private Mutable(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Mutable of(double x, double y, double z) {
|
||||
return new Mutable(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3 immutable() {
|
||||
return Vector3.of(x, y, z);
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Mutable setZ(double z) {
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public Mutable setX(double x) {
|
||||
this.x = x;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public Mutable setY(double y) {
|
||||
this.y = y;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return FastMath.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
public double inverseLength() {
|
||||
return FastMath.invSqrtQuick(lengthSquared());
|
||||
}
|
||||
|
||||
public Mutable normalize() {
|
||||
return this.multiply(this.inverseLength());
|
||||
}
|
||||
|
||||
public Mutable subtract(int x, int y, int z) {
|
||||
this.x -= x;
|
||||
this.y -= y;
|
||||
this.z -= z;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the dot product of this vector with another. The dot product
|
||||
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
|
||||
*
|
||||
* @param other The other vector
|
||||
*
|
||||
* @return dot product
|
||||
*/
|
||||
public double dot(@NotNull Vector3 other) {
|
||||
return x * other.getX() + y * other.getY() + z * other.getZ();
|
||||
}
|
||||
|
||||
public Mutable subtract(Vector3 end) {
|
||||
x -= end.getX();
|
||||
y -= end.getY();
|
||||
z -= end.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable multiply(double m) {
|
||||
x *= m;
|
||||
y *= m;
|
||||
z *= m;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(double x, double y, double z) {
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector3 other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector3Int other) {
|
||||
this.x += other.getX();
|
||||
this.y += other.getY();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Mutable add(Vector2 other) {
|
||||
this.x += other.getX();
|
||||
this.z += other.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* This method will always make sure the provided axis is a unit vector, to
|
||||
* not modify the length of the vector when rotating.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around. If the passed vector is
|
||||
* not of length 1, it gets copied and normalized before using it for the
|
||||
* rotation. Please use {@link Mutable#normalize()} on the instance before
|
||||
* passing it to this method
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.mutable().normalize().immutable(), angle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
|
||||
*
|
||||
* <p>
|
||||
* Rotation will follow the general Right-Hand-Rule, which means rotation
|
||||
* will be counterclockwise when the axis is pointing towards the observer.
|
||||
* <p>
|
||||
* Note that the vector length will change accordingly to the axis vector
|
||||
* length. If the provided axis is not a unit vector, the rotated vector
|
||||
* will not have its previous length. The scaled length of the resulting
|
||||
* vector will be related to the axis vector.
|
||||
*
|
||||
* @param axis the axis to rotate the vector around.
|
||||
* @param angle the angle to rotate the vector around the axis
|
||||
*
|
||||
* @return the same vector
|
||||
*
|
||||
* @throws IllegalArgumentException if the provided axis vector instance is
|
||||
* null
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
|
||||
double x = getX(), y = getY(), z = getZ();
|
||||
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
|
||||
|
||||
double cosTheta = Math.cos(angle);
|
||||
double sinTheta = Math.sin(angle);
|
||||
double dotProduct = this.dot(axis);
|
||||
|
||||
double xPrime = x2 * dotProduct * (1d - cosTheta)
|
||||
+ x * cosTheta
|
||||
+ (-z2 * y + y2 * z) * sinTheta;
|
||||
double yPrime = y2 * dotProduct * (1d - cosTheta)
|
||||
+ y * cosTheta
|
||||
+ (z2 * x - x2 * z) * sinTheta;
|
||||
double zPrime = z2 * dotProduct * (1d - cosTheta)
|
||||
+ z * cosTheta
|
||||
+ (-y2 * x + x2 * y) * sinTheta;
|
||||
|
||||
return setX(xPrime).setY(yPrime).setZ(zPrime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundX(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double y = angleCos * getY() - angleSin * getZ();
|
||||
double z = angleSin * getY() + angleCos * getZ();
|
||||
return setY(y).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundY(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double x = angleCos * getX() + angleSin * getZ();
|
||||
double z = -angleSin * getX() + angleCos * getZ();
|
||||
return setX(x).setZ(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the same vector
|
||||
*/
|
||||
@NotNull
|
||||
public Mutable rotateAroundZ(double angle) {
|
||||
double angleCos = Math.cos(angle);
|
||||
double angleSin = Math.sin(angle);
|
||||
|
||||
double x = angleCos * getX() - angleSin * getY();
|
||||
double y = angleSin * getX() + angleCos * getY();
|
||||
return setX(x).setY(y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 13;
|
||||
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
||||
return hash;
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return FastMath.floorToInt(x);
|
||||
}
|
||||
|
||||
public int getBlockY() {
|
||||
return FastMath.floorToInt(y);
|
||||
}
|
||||
|
||||
public int getBlockZ() {
|
||||
return FastMath.floorToInt(z);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,10 @@ public class Vector3Int {
|
||||
return Vector3.of(x, y, z);
|
||||
}
|
||||
|
||||
public Vector3.Mutable toVector3Mutable() {
|
||||
return Vector3.Mutable.of(x, y, z);
|
||||
}
|
||||
|
||||
public static class Mutable {
|
||||
private int x, y, z;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user