| 1 | package net.minecraft.world.phys;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.EnumSet;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.Position;
|
| 9 | import net.minecraft.core.Vec3i;
|
| 10 | import net.minecraft.network.LpVec3;
|
| 11 | import net.minecraft.network.codec.StreamCodec;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import org.joml.Vector3f;
|
| 16 | import org.joml.Vector3fc;
|
| 17 |
|
| 18 | public class Vec3 implements Position {
|
| 19 | public static final Codec<Vec3> CODEC = Codec.DOUBLE
|
| 20 | .listOf()
|
| 21 | .comapFlatMap(
|
| 22 | input -> Util.fixedSize((List<Double>)input, 3).map(doubles -> new Vec3(doubles.get(0), doubles.get(1), doubles.get(2))),
|
| 23 | pos -> List.of(pos.x(), pos.y(), pos.z())
|
| 24 | );
|
| 25 | public static final StreamCodec<ByteBuf, Vec3> STREAM_CODEC = new StreamCodec<ByteBuf, Vec3>() {
|
| 26 | public Vec3 decode(final ByteBuf input) {
|
| 27 | return new Vec3(input.readDouble(), input.readDouble(), input.readDouble());
|
| 28 | }
|
| 29 |
|
| 30 | public void encode(final ByteBuf output, final Vec3 value) {
|
| 31 | output.writeDouble(value.x());
|
| 32 | output.writeDouble(value.y());
|
| 33 | output.writeDouble(value.z());
|
| 34 | }
|
| 35 | };
|
| 36 | public static final StreamCodec<ByteBuf, Vec3> LP_STREAM_CODEC = StreamCodec.of(LpVec3::write, LpVec3::read);
|
| 37 | public static final Vec3 ZERO = new Vec3(0.0, 0.0, 0.0);
|
| 38 | public static final Vec3 X_AXIS = new Vec3(1.0, 0.0, 0.0);
|
| 39 | public static final Vec3 Y_AXIS = new Vec3(0.0, 1.0, 0.0);
|
| 40 | public static final Vec3 Z_AXIS = new Vec3(0.0, 0.0, 1.0);
|
| 41 | public final double x;
|
| 42 | public final double y;
|
| 43 | public final double z;
|
| 44 |
|
| 45 | public static Vec3 atLowerCornerOf(final Vec3i pos) {
|
| 46 | return new Vec3(pos.getX(), pos.getY(), pos.getZ());
|
| 47 | }
|
| 48 |
|
| 49 | public static Vec3 atLowerCornerWithOffset(final Vec3i pos, final double x, final double y, final double z) {
|
| 50 | return new Vec3(pos.getX() + x, pos.getY() + y, pos.getZ() + z);
|
| 51 | }
|
| 52 |
|
| 53 | public static Vec3 atCenterOf(final Vec3i pos) {
|
| 54 | return atLowerCornerWithOffset(pos, 0.5, 0.5, 0.5);
|
| 55 | }
|
| 56 |
|
| 57 | public static Vec3 atCenterOfWithY(final Vec3i pos, final double y) {
|
| 58 | return new Vec3(pos.getX() + 0.5, y, pos.getZ() + 0.5);
|
| 59 | }
|
| 60 |
|
| 61 | public static Vec3 atBottomCenterOf(final Vec3i pos) {
|
| 62 | return atLowerCornerWithOffset(pos, 0.5, 0.0, 0.5);
|
| 63 | }
|
| 64 |
|
| 65 | public static Vec3 upFromBottomCenterOf(final Vec3i pos, final double yOffset) {
|
| 66 | return atLowerCornerWithOffset(pos, 0.5, yOffset, 0.5);
|
| 67 | }
|
| 68 |
|
| 69 | public Vec3(final double x, final double y, final double z) {
|
| 70 | this.x = x;
|
| 71 | this.y = y;
|
| 72 | this.z = z;
|
| 73 | }
|
| 74 |
|
| 75 | public Vec3(final Vector3fc vec) {
|
| 76 | this(vec.x(), vec.y(), vec.z());
|
| 77 | }
|
| 78 |
|
| 79 | public Vec3(final Vec3i vec) {
|
| 80 | this(vec.getX(), vec.getY(), vec.getZ());
|
| 81 | }
|
| 82 |
|
| 83 | public Vec3 vectorTo(final Vec3 vec) {
|
| 84 | return new Vec3(vec.x - this.x, vec.y - this.y, vec.z - this.z);
|
| 85 | }
|
| 86 |
|
| 87 | public Vec3 normalize() {
|
| 88 | double dist = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
| 89 | return dist < 1.0E-5F ? ZERO : new Vec3(this.x / dist, this.y / dist, this.z / dist);
|
| 90 | }
|
| 91 |
|
| 92 | public double dot(final Vec3 vec) {
|
| 93 | return this.x * vec.x + this.y * vec.y + this.z * vec.z;
|
| 94 | }
|
| 95 |
|
| 96 | public Vec3 cross(final Vec3 vec) {
|
| 97 | return new Vec3(this.y * vec.z - this.z * vec.y, this.z * vec.x - this.x * vec.z, this.x * vec.y - this.y * vec.x);
|
| 98 | }
|
| 99 |
|
| 100 | public Vec3 subtract(final Vec3 vec) {
|
| 101 | return this.subtract(vec.x, vec.y, vec.z);
|
| 102 | }
|
| 103 |
|
| 104 | public Vec3 subtract(final double value) {
|
| 105 | return this.subtract(value, value, value);
|
| 106 | }
|
| 107 |
|
| 108 | public Vec3 subtract(final double x, final double y, final double z) {
|
| 109 | return this.add(-x, -y, -z);
|
| 110 | }
|
| 111 |
|
| 112 | public Vec3 add(final double value) {
|
| 113 | return this.add(value, value, value);
|
| 114 | }
|
| 115 |
|
| 116 | public Vec3 add(final Vec3 vec) {
|
| 117 | return this.add(vec.x, vec.y, vec.z);
|
| 118 | }
|
| 119 |
|
| 120 | public Vec3 add(final double x, final double y, final double z) {
|
| 121 | return new Vec3(this.x + x, this.y + y, this.z + z);
|
| 122 | }
|
| 123 |
|
| 124 | public boolean closerThan(final Position pos, final double distance) {
|
| 125 | return this.distanceToSqr(pos.x(), pos.y(), pos.z()) < distance * distance;
|
| 126 | }
|
| 127 |
|
| 128 | public double distanceTo(final Vec3 vec) {
|
| 129 | double xd = vec.x - this.x;
|
| 130 | double yd = vec.y - this.y;
|
| 131 | double zd = vec.z - this.z;
|
| 132 | return Math.sqrt(xd * xd + yd * yd + zd * zd);
|
| 133 | }
|
| 134 |
|
| 135 | public double distanceToSqr(final Vec3 vec) {
|
| 136 | double xd = vec.x - this.x;
|
| 137 | double yd = vec.y - this.y;
|
| 138 | double zd = vec.z - this.z;
|
| 139 | return xd * xd + yd * yd + zd * zd;
|
| 140 | }
|
| 141 |
|
| 142 | public double distanceToSqr(final double x, final double y, final double z) {
|
| 143 | double xd = x - this.x;
|
| 144 | double yd = y - this.y;
|
| 145 | double zd = z - this.z;
|
| 146 | return xd * xd + yd * yd + zd * zd;
|
| 147 | }
|
| 148 |
|
| 149 | public boolean closerThan(final Vec3 vec, final double distanceXZ, final double distanceY) {
|
| 150 | double dx = vec.x() - this.x;
|
| 151 | double dy = vec.y() - this.y;
|
| 152 | double dz = vec.z() - this.z;
|
| 153 | return Mth.lengthSquared(dx, dz) < Mth.square(distanceXZ) && Math.abs(dy) < distanceY;
|
| 154 | }
|
| 155 |
|
| 156 | public Vec3 scale(final double scale) {
|
| 157 | return this.multiply(scale, scale, scale);
|
| 158 | }
|
| 159 |
|
| 160 | public Vec3 reverse() {
|
| 161 | return this.scale(-1.0);
|
| 162 | }
|
| 163 |
|
| 164 | public Vec3 multiply(final Vec3 scale) {
|
| 165 | return this.multiply(scale.x, scale.y, scale.z);
|
| 166 | }
|
| 167 |
|
| 168 | public Vec3 multiply(final double xScale, final double yScale, final double zScale) {
|
| 169 | return new Vec3(this.x * xScale, this.y * yScale, this.z * zScale);
|
| 170 | }
|
| 171 |
|
| 172 | public Vec3 horizontal() {
|
| 173 | return new Vec3(this.x, 0.0, this.z);
|
| 174 | }
|
| 175 |
|
| 176 | public Vec3 offsetRandom(final RandomSource random, final float offset) {
|
| 177 | return this.add((random.nextFloat() - 0.5F) * offset, (random.nextFloat() - 0.5F) * offset, (random.nextFloat() - 0.5F) * offset);
|
| 178 | }
|
| 179 |
|
| 180 | public Vec3 offsetRandomXZ(final RandomSource random, final float offset) {
|
| 181 | return this.add((random.nextFloat() - 0.5F) * offset, 0.0, (random.nextFloat() - 0.5F) * offset);
|
| 182 | }
|
| 183 |
|
| 184 | public double length() {
|
| 185 | return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
| 186 | }
|
| 187 |
|
| 188 | public double lengthSqr() {
|
| 189 | return this.x * this.x + this.y * this.y + this.z * this.z;
|
| 190 | }
|
| 191 |
|
| 192 | public double horizontalDistance() {
|
| 193 | return Math.sqrt(this.x * this.x + this.z * this.z);
|
| 194 | }
|
| 195 |
|
| 196 | public double horizontalDistanceSqr() {
|
| 197 | return this.x * this.x + this.z * this.z;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public boolean equals(final Object o) {
|
| 202 | if (this == o) {
|
| 203 | return true;
|
| 204 | }
|
| 205 |
|
| 206 | if (o instanceof Vec3 vec3) {
|
| 207 | if (Double.compare(vec3.x, this.x) != 0) {
|
| 208 | return false;
|
| 209 | } else {
|
| 210 | return Double.compare(vec3.y, this.y) != 0 ? false : Double.compare(vec3.z, this.z) == 0;
|
| 211 | }
|
| 212 | } else {
|
| 213 | return false;
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public int hashCode() {
|
| 219 | long temp = Double.doubleToLongBits(this.x);
|
| 220 | int result = (int)(temp ^ temp >>> 32);
|
| 221 | temp = Double.doubleToLongBits(this.y);
|
| 222 | result = 31 * result + (int)(temp ^ temp >>> 32);
|
| 223 | temp = Double.doubleToLongBits(this.z);
|
| 224 | return 31 * result + (int)(temp ^ temp >>> 32);
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public String toString() {
|
| 229 | return "(" + this.x + ", " + this.y + ", " + this.z + ")";
|
| 230 | }
|
| 231 |
|
| 232 | public Vec3 lerp(final Vec3 vec, final double a) {
|
| 233 | return new Vec3(Mth.lerp(a, this.x, vec.x), Mth.lerp(a, this.y, vec.y), Mth.lerp(a, this.z, vec.z));
|
| 234 | }
|
| 235 |
|
| 236 | public Vec3 xRot(final float radians) {
|
| 237 | float cos = Mth.cos(radians);
|
| 238 | float sin = Mth.sin(radians);
|
| 239 | double xx = this.x;
|
| 240 | double yy = this.y * cos + this.z * sin;
|
| 241 | double zz = this.z * cos - this.y * sin;
|
| 242 | return new Vec3(xx, yy, zz);
|
| 243 | }
|
| 244 |
|
| 245 | public Vec3 yRot(final float radians) {
|
| 246 | float cos = Mth.cos(radians);
|
| 247 | float sin = Mth.sin(radians);
|
| 248 | double xx = this.x * cos + this.z * sin;
|
| 249 | double yy = this.y;
|
| 250 | double zz = this.z * cos - this.x * sin;
|
| 251 | return new Vec3(xx, yy, zz);
|
| 252 | }
|
| 253 |
|
| 254 | public Vec3 zRot(final float radians) {
|
| 255 | float cos = Mth.cos(radians);
|
| 256 | float sin = Mth.sin(radians);
|
| 257 | double xx = this.x * cos + this.y * sin;
|
| 258 | double yy = this.y * cos - this.x * sin;
|
| 259 | double zz = this.z;
|
| 260 | return new Vec3(xx, yy, zz);
|
| 261 | }
|
| 262 |
|
| 263 | public Vec3 rotateClockwise90() {
|
| 264 | return new Vec3(-this.z, this.y, this.x);
|
| 265 | }
|
| 266 |
|
| 267 | public static Vec3 directionFromRotation(final Vec2 rotation) {
|
| 268 | return directionFromRotation(rotation.x, rotation.y);
|
| 269 | }
|
| 270 |
|
| 271 | public static Vec3 directionFromRotation(final float rotX, final float rotY) {
|
| 272 | float yCos = Mth.cos(-rotY * (float) (Math.PI / 180.0) - (float) Math.PI);
|
| 273 | float ySin = Mth.sin(-rotY * (float) (Math.PI / 180.0) - (float) Math.PI);
|
| 274 | float xCos = -Mth.cos(-rotX * (float) (Math.PI / 180.0));
|
| 275 | float xSin = Mth.sin(-rotX * (float) (Math.PI / 180.0));
|
| 276 | return new Vec3(ySin * xCos, xSin, yCos * xCos);
|
| 277 | }
|
| 278 |
|
| 279 | public Vec2 rotation() {
|
| 280 | float yaw = (float)Math.atan2(-this.x, this.z) * (180.0F / (float)Math.PI);
|
| 281 | float pitch = (float)Math.asin(-this.y / Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z)) * (180.0F / (float)Math.PI);
|
| 282 | return new Vec2(pitch, yaw);
|
| 283 | }
|
| 284 |
|
| 285 | public Vec3 align(final EnumSet<Direction.Axis> axes) {
|
| 286 | double x = axes.contains(Direction.Axis.X) ? Mth.floor(this.x) : this.x;
|
| 287 | double y = axes.contains(Direction.Axis.Y) ? Mth.floor(this.y) : this.y;
|
| 288 | double z = axes.contains(Direction.Axis.Z) ? Mth.floor(this.z) : this.z;
|
| 289 | return new Vec3(x, y, z);
|
| 290 | }
|
| 291 |
|
| 292 | public double get(final Direction.Axis axis) {
|
| 293 | return axis.choose(this.x, this.y, this.z);
|
| 294 | }
|
| 295 |
|
| 296 | public Vec3 with(final Direction.Axis axis, final double value) {
|
| 297 | double x = axis == Direction.Axis.X ? value : this.x;
|
| 298 | double y = axis == Direction.Axis.Y ? value : this.y;
|
| 299 | double z = axis == Direction.Axis.Z ? value : this.z;
|
| 300 | return new Vec3(x, y, z);
|
| 301 | }
|
| 302 |
|
| 303 | public Vec3 relative(final Direction direction, final double distance) {
|
| 304 | Vec3i normal = direction.getUnitVec3i();
|
| 305 | return new Vec3(this.x + distance * normal.getX(), this.y + distance * normal.getY(), this.z + distance * normal.getZ());
|
| 306 | }
|
| 307 |
|
| 308 | @Override
|
| 309 | public final double x() {
|
| 310 | return this.x;
|
| 311 | }
|
| 312 |
|
| 313 | @Override
|
| 314 | public final double y() {
|
| 315 | return this.y;
|
| 316 | }
|
| 317 |
|
| 318 | @Override
|
| 319 | public final double z() {
|
| 320 | return this.z;
|
| 321 | }
|
| 322 |
|
| 323 | public Vector3f toVector3f() {
|
| 324 | return new Vector3f((float)this.x, (float)this.y, (float)this.z);
|
| 325 | }
|
| 326 |
|
| 327 | public Vec3 projectedOn(final Vec3 onto) {
|
| 328 | return onto.lengthSqr() == 0.0 ? onto : onto.scale(this.dot(onto)).scale(1.0 / onto.lengthSqr());
|
| 329 | }
|
| 330 |
|
| 331 | public boolean isFinite() {
|
| 332 | return Double.isFinite(this.x) && Double.isFinite(this.y) && Double.isFinite(this.z);
|
| 333 | }
|
| 334 | }
|
| 335 |
|