| 1 | package net.minecraft.world.phys;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.util.Util;
|
| 8 |
|
| 9 | public class Vec2 {
|
| 10 | public static final Vec2 ZERO = new Vec2(0.0F, 0.0F);
|
| 11 | public static final Vec2 ONE = new Vec2(1.0F, 1.0F);
|
| 12 | public static final Vec2 UNIT_X = new Vec2(1.0F, 0.0F);
|
| 13 | public static final Vec2 NEG_UNIT_X = new Vec2(-1.0F, 0.0F);
|
| 14 | public static final Vec2 UNIT_Y = new Vec2(0.0F, 1.0F);
|
| 15 | public static final Vec2 NEG_UNIT_Y = new Vec2(0.0F, -1.0F);
|
| 16 | public static final Vec2 MAX = new Vec2(Float.MAX_VALUE, Float.MAX_VALUE);
|
| 17 | public static final Vec2 MIN = new Vec2(Float.MIN_VALUE, Float.MIN_VALUE);
|
| 18 | public static final Codec<Vec2> CODEC = Codec.FLOAT
|
| 19 | .listOf()
|
| 20 | .comapFlatMap(input -> Util.fixedSize((List<Float>)input, 2).map(floats -> new Vec2(floats.get(0), floats.get(1))), vec -> List.of(vec.x, vec.y));
|
| 21 | public final float x;
|
| 22 | public final float y;
|
| 23 |
|
| 24 | public Vec2(final float x, final float y) {
|
| 25 | this.x = x;
|
| 26 | this.y = y;
|
| 27 | }
|
| 28 |
|
| 29 | public Vec2 scale(final float s) {
|
| 30 | return new Vec2(this.x * s, this.y * s);
|
| 31 | }
|
| 32 |
|
| 33 | public float dot(final Vec2 v) {
|
| 34 | return this.x * v.x + this.y * v.y;
|
| 35 | }
|
| 36 |
|
| 37 | public Vec2 add(final Vec2 rhs) {
|
| 38 | return new Vec2(this.x + rhs.x, this.y + rhs.y);
|
| 39 | }
|
| 40 |
|
| 41 | public Vec2 add(final float v) {
|
| 42 | return new Vec2(this.x + v, this.y + v);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public boolean equals(final Object rhs) {
|
| 47 | if (this == rhs) {
|
| 48 | return true;
|
| 49 | } else {
|
| 50 | return !(rhs instanceof Vec2 other) ? false : this.x == other.x && this.y == other.y;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public int hashCode() {
|
| 56 | return Objects.hash(this.x, this.y);
|
| 57 | }
|
| 58 |
|
| 59 | public Vec2 normalized() {
|
| 60 | float dist = Mth.sqrt(this.x * this.x + this.y * this.y);
|
| 61 | return dist < 1.0E-4F ? ZERO : new Vec2(this.x / dist, this.y / dist);
|
| 62 | }
|
| 63 |
|
| 64 | public float length() {
|
| 65 | return Mth.sqrt(this.x * this.x + this.y * this.y);
|
| 66 | }
|
| 67 |
|
| 68 | public float lengthSquared() {
|
| 69 | return this.x * this.x + this.y * this.y;
|
| 70 | }
|
| 71 |
|
| 72 | public float distanceToSqr(final Vec2 p) {
|
| 73 | float xd = p.x - this.x;
|
| 74 | float yd = p.y - this.y;
|
| 75 | return xd * xd + yd * yd;
|
| 76 | }
|
| 77 |
|
| 78 | public Vec2 negated() {
|
| 79 | return new Vec2(-this.x, -this.y);
|
| 80 | }
|
| 81 |
|
| 82 | public Vec2 rotate(final double angleRadians) {
|
| 83 | float cosine = Mth.cos(angleRadians);
|
| 84 | float sine = Mth.sin(angleRadians);
|
| 85 | return new Vec2(this.x * cosine - this.y * sine, this.y * cosine + this.x * sine);
|
| 86 | }
|
| 87 | }
|
| 88 |
|