Skip to content

Commit 24ccb02

Browse files
committed
Renamed WorldMouse to ProjectedCoordinates and Cursor to ClipPosition, added unsafe Identity types
1 parent 9dc3d36 commit 24ccb02

5 files changed

Lines changed: 123 additions & 93 deletions

File tree

src/main/java/dev/latvian/mods/klib/data/DataTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import dev.latvian.mods.klib.color.Gradient;
1818
import dev.latvian.mods.klib.interpolation.Interpolation;
1919
import dev.latvian.mods.klib.interpolation.InterpolationType;
20+
import dev.latvian.mods.klib.math.ClipPosition;
2021
import dev.latvian.mods.klib.math.InterpolatedDouble;
2122
import dev.latvian.mods.klib.math.InterpolatedFloat;
2223
import dev.latvian.mods.klib.math.Line;
@@ -144,5 +145,6 @@ static void register() {
144145
DataType.register(KLibMod.id("line"), Line.DATA_TYPE);
145146
DataType.register(KLibMod.id("interpolated_float"), InterpolatedFloat.DATA_TYPE);
146147
DataType.register(KLibMod.id("interpolated_double"), InterpolatedDouble.DATA_TYPE);
148+
DataType.register(KLibMod.id("clip_position"), ClipPosition.DATA_TYPE);
147149
}
148150
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.latvian.mods.klib.math;
2+
3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import dev.latvian.mods.klib.codec.CompositeStreamCodec;
6+
import dev.latvian.mods.klib.codec.MCCodecs;
7+
import dev.latvian.mods.klib.codec.MCStreamCodecs;
8+
import dev.latvian.mods.klib.data.DataType;
9+
import io.netty.buffer.ByteBuf;
10+
import net.minecraft.core.BlockPos;
11+
import net.minecraft.core.Direction;
12+
import net.minecraft.network.codec.StreamCodec;
13+
import net.minecraft.world.phys.BlockHitResult;
14+
import net.minecraft.world.phys.Vec3;
15+
16+
public record ClipPosition(Vec3 position, BlockPos blockPosition, Direction side) {
17+
public static final Codec<ClipPosition> CODEC = RecordCodecBuilder.create(instance -> instance.group(
18+
MCCodecs.VEC3.fieldOf("position").forGetter(ClipPosition::position),
19+
BlockPos.CODEC.fieldOf("block").forGetter(ClipPosition::blockPosition),
20+
Direction.CODEC.fieldOf("side").forGetter(ClipPosition::side)
21+
).apply(instance, ClipPosition::new));
22+
23+
public static final StreamCodec<ByteBuf, ClipPosition> STREAM_CODEC = CompositeStreamCodec.of(
24+
MCStreamCodecs.VEC3, ClipPosition::position,
25+
BlockPos.STREAM_CODEC, ClipPosition::blockPosition,
26+
Direction.STREAM_CODEC, ClipPosition::side,
27+
ClipPosition::new
28+
);
29+
30+
public static final DataType<ClipPosition> DATA_TYPE = DataType.of(CODEC, STREAM_CODEC, ClipPosition.class);
31+
32+
public ClipPosition(BlockHitResult hit) {
33+
this(hit.getLocation(), hit.getBlockPos(), hit.getDirection());
34+
}
35+
36+
public BlockPos getOffsetBlockPosition() {
37+
return blockPosition.relative(side);
38+
}
39+
40+
public Vec3 getBottomCenter() {
41+
return Vec3.atBottomCenterOf(getOffsetBlockPosition());
42+
}
43+
44+
public Vec3 getCenter() {
45+
return Vec3.atCenterOf(getOffsetBlockPosition());
46+
}
47+
}

src/main/java/dev/latvian/mods/klib/math/Cursor.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/dev/latvian/mods/klib/math/Identity.java

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,59 @@
5050
import org.joml.Vector4ic;
5151

5252
public interface Identity {
53-
Vector2fc VEC_2 = new Vector2f();
54-
Vector3fc VEC_3 = new Vector3f();
55-
Vector4fc VEC_4 = new Vector4f();
56-
Matrix2fc MAT_2 = new Matrix2f();
57-
Matrix3fc MAT_3 = new Matrix3f();
58-
Matrix3x2fc MAT_3x2 = new Matrix3x2f();
59-
Matrix4fc MAT_4 = new Matrix4f();
60-
Matrix4x3fc MAT_4x3 = new Matrix4x3f();
61-
Quaternionfc QUATERNION = new Quaternionf();
53+
Vector2f UNSAFE_VEC_2 = new Vector2f();
54+
Vector3f UNSAFE_VEC_3 = new Vector3f();
55+
Vector4f UNSAFE_VEC_4 = new Vector4f();
56+
Matrix2f UNSAFE_MAT_2 = new Matrix2f();
57+
Matrix3f UNSAFE_MAT_3 = new Matrix3f();
58+
Matrix3x2f UNSAFE_MAT_3x2 = new Matrix3x2f();
59+
Matrix4f UNSAFE_MAT_4 = new Matrix4f();
60+
Matrix4x3f UNSAFE_MAT_4x3 = new Matrix4x3f();
61+
Quaternionf UNSAFE_QUATERNION = new Quaternionf();
6262

63-
Vector2dc DVEC_2 = new Vector2d();
64-
Vector3dc DVEC_3 = new Vector3d();
65-
Vector4dc DVEC_4 = new Vector4d();
66-
Matrix2dc DMAT_2 = new Matrix2d();
67-
Matrix3dc DMAT_3 = new Matrix3d();
68-
Matrix3x2dc DMAT_3x2 = new Matrix3x2d();
69-
Matrix4dc DMAT_4 = new Matrix4d();
70-
Matrix4x3dc DMAT_4x3 = new Matrix4x3d();
71-
Quaterniondc DQUATERNION = new Quaterniond();
63+
Vector2fc VEC_2 = UNSAFE_VEC_2;
64+
Vector3fc VEC_3 = UNSAFE_VEC_3;
65+
Vector4fc VEC_4 = UNSAFE_VEC_4;
66+
Matrix2fc MAT_2 = UNSAFE_MAT_2;
67+
Matrix3fc MAT_3 = UNSAFE_MAT_3;
68+
Matrix3x2fc MAT_3x2 = UNSAFE_MAT_3x2;
69+
Matrix4fc MAT_4 = UNSAFE_MAT_4;
70+
Matrix4x3fc MAT_4x3 = UNSAFE_MAT_4x3;
71+
Quaternionfc QUATERNION = UNSAFE_QUATERNION;
7272

73-
Vector2ic IVEC_2 = new Vector2i();
74-
Vector3ic IVEC_3 = new Vector3i();
75-
Vector4ic IVEC_4 = new Vector4i();
73+
Vector2d UNSAFE_DVEC_2 = new Vector2d();
74+
Vector3d UNSAFE_DVEC_3 = new Vector3d();
75+
Vector4d UNSAFE_DVEC_4 = new Vector4d();
76+
Matrix2d UNSAFE_DMAT_2 = new Matrix2d();
77+
Matrix3d UNSAFE_DMAT_3 = new Matrix3d();
78+
Matrix3x2d UNSAFE_DMAT_3x2 = new Matrix3x2d();
79+
Matrix4d UNSAFE_DMAT_4 = new Matrix4d();
80+
Matrix4x3d UNSAFE_DMAT_4x3 = new Matrix4x3d();
81+
Quaterniond UNSAFE_DQUATERNION = new Quaterniond();
7682

77-
Vector2Lc LVEC_2 = new Vector2L();
78-
Vector3Lc LVEC_3 = new Vector3L();
79-
Vector4Lc LVEC_4 = new Vector4L();
83+
Vector2dc DVEC_2 = UNSAFE_DVEC_2;
84+
Vector3dc DVEC_3 = UNSAFE_DVEC_3;
85+
Vector4dc DVEC_4 = UNSAFE_DVEC_4;
86+
Matrix2dc DMAT_2 = UNSAFE_DMAT_2;
87+
Matrix3dc DMAT_3 = UNSAFE_DMAT_3;
88+
Matrix3x2dc DMAT_3x2 = UNSAFE_DMAT_3x2;
89+
Matrix4dc DMAT_4 = UNSAFE_DMAT_4;
90+
Matrix4x3dc DMAT_4x3 = UNSAFE_DMAT_4x3;
91+
Quaterniondc DQUATERNION = UNSAFE_DQUATERNION;
92+
93+
Vector2i UNSAFE_IVEC_2 = new Vector2i();
94+
Vector3i UNSAFE_IVEC_3 = new Vector3i();
95+
Vector4i UNSAFE_IVEC_4 = new Vector4i();
96+
97+
Vector2ic IVEC_2 = UNSAFE_IVEC_2;
98+
Vector3ic IVEC_3 = UNSAFE_IVEC_3;
99+
Vector4ic IVEC_4 = UNSAFE_IVEC_4;
100+
101+
Vector2L UNSAFE_LVEC_2 = new Vector2L();
102+
Vector3L UNSAFE_LVEC_3 = new Vector3L();
103+
Vector4L UNSAFE_LVEC_4 = new Vector4L();
104+
105+
Vector2Lc LVEC_2 = UNSAFE_LVEC_2;
106+
Vector3Lc LVEC_3 = UNSAFE_LVEC_3;
107+
Vector4Lc LVEC_4 = UNSAFE_LVEC_4;
80108
}

src/main/java/dev/latvian/mods/klib/math/WorldMouse.java renamed to src/main/java/dev/latvian/mods/klib/math/ProjectedCoordinates.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,40 @@
77
import net.minecraft.world.phys.HitResult;
88
import net.minecraft.world.phys.Vec3;
99
import org.jetbrains.annotations.Nullable;
10+
import org.joml.Vector2d;
11+
import org.joml.Vector2dc;
12+
import org.joml.Vector2f;
1013
import org.joml.Vector4d;
1114
import org.joml.Vector4f;
1215

13-
public record WorldMouse(
16+
public record ProjectedCoordinates(
1417
Minecraft mc,
1518
Vec3 cameraPos,
1619
float width,
1720
float height,
18-
Vec2d defaultScreenPos
21+
Vector2dc defaultScreenPos
1922
) {
20-
public static WorldMouse of(Minecraft mc, Vec3 cameraPos) {
23+
public static ProjectedCoordinates of(Minecraft mc, Vec3 cameraPos) {
2124
var width = mc.getWindow().getGuiScaledWidth();
2225
var height = mc.getWindow().getGuiScaledHeight();
2326

24-
return new WorldMouse(
27+
return new ProjectedCoordinates(
2528
mc,
2629
cameraPos,
2730
width,
2831
height,
29-
mc.screen == null ? new Vec2d(
32+
mc.screen == null ? new Vector2d(
3033
width * 0.5D,
3134
height * 0.5D
32-
) : new Vec2d(
35+
) : new Vector2d(
3336
mc.mouseHandler.xpos() * width / (double) mc.getWindow().getWidth(),
3437
mc.mouseHandler.ypos() * height / (double) mc.getWindow().getHeight()
3538
)
3639
);
3740
}
3841

3942
@Nullable
40-
public Cursor clip(double maxDistance, ClipContext.Block blockClipContext, ClipContext.Fluid fluidClipContext, @Nullable Vec2d screenPos, @Nullable Entity clipEntity) {
43+
public ClipPosition clip(double maxDistance, ClipContext.Block blockClipContext, ClipContext.Fluid fluidClipContext, @Nullable Vector2dc screenPos, @Nullable Entity clipEntity) {
4144
if (screenPos == null) {
4245
screenPos = defaultScreenPos;
4346
}
@@ -63,16 +66,16 @@ public Cursor clip(double maxDistance, ClipContext.Block blockClipContext, ClipC
6366
return null;
6467
}
6568

66-
return new Cursor(hit);
69+
return new ClipPosition(hit);
6770
}
6871

6972
@Nullable
70-
public Cursor clipOutline() {
73+
public ClipPosition clipOutline() {
7174
return clip(1000D, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, null, null);
7275
}
7376

7477
@Nullable
75-
public Cursor clipCollision() {
78+
public ClipPosition clipCollision() {
7679
return clip(1000D, ClipContext.Block.COLLIDER, ClipContext.Fluid.SOURCE_ONLY, null, null);
7780
}
7881

@@ -86,7 +89,7 @@ public Cursor clipCollision() {
8689
* @return Screen coordinates, or null if outside the screen
8790
*/
8891
@Nullable
89-
public Vec2f screen(double worldX, double worldY, double worldZ, boolean allowOutside) {
92+
public Vector2f screen(double worldX, double worldY, double worldZ, boolean allowOutside) {
9093
double rx = worldX - cameraPos.x;
9194
double ry = worldY - cameraPos.y;
9295
double rz = worldZ - cameraPos.z;
@@ -97,7 +100,7 @@ public Vec2f screen(double worldX, double worldY, double worldZ, boolean allowOu
97100
v.div(v.w);
98101

99102
if (allowOutside || v.z > 0F && v.z < 1F) {
100-
return new Vec2f(
103+
return new Vector2f(
101104
(0.5F + v.x * 0.5F) * width,
102105
(0.5F - v.y * 0.5F) * height
103106
);
@@ -110,34 +113,34 @@ public Vec2f screen(double worldX, double worldY, double worldZ, boolean allowOu
110113
* @param worldX X position
111114
* @param worldY Y position
112115
* @param worldZ Z position
113-
* @see WorldMouse#screen(double, double, double, boolean)
116+
* @see ProjectedCoordinates#screen(double, double, double, boolean)
114117
*/
115118
@Nullable
116-
public Vec2f screen(double worldX, double worldY, double worldZ) {
119+
public Vector2f screen(double worldX, double worldY, double worldZ) {
117120
return screen(worldX, worldY, worldZ, false);
118121
}
119122

120123
/**
121124
* @param worldPos XYZ position
122125
* @param allowOutside Allow outside the screen
123-
* @see WorldMouse#screen(double, double, double, boolean)
126+
* @see ProjectedCoordinates#screen(double, double, double, boolean)
124127
*/
125128
@Nullable
126-
public Vec2f screen(Position worldPos, boolean allowOutside) {
129+
public Vector2f screen(Position worldPos, boolean allowOutside) {
127130
return screen(worldPos.x(), worldPos.y(), worldPos.z(), allowOutside);
128131
}
129132

130133
/**
131134
* @param worldPos XYZ position
132-
* @see WorldMouse#screen(double, double, double, boolean)
135+
* @see ProjectedCoordinates#screen(double, double, double, boolean)
133136
*/
134137
@Nullable
135-
public Vec2f screen(Position worldPos) {
138+
public Vector2f screen(Position worldPos) {
136139
return screen(worldPos.x(), worldPos.y(), worldPos.z(), false);
137140
}
138141

139142
/**
140-
* Convert screen coordinates to world position. Use {@link WorldMouse#clip(double, ClipContext.Block, ClipContext.Fluid, Vec2d, Entity)} if you only care about current mouse position
143+
* Convert screen coordinates to world position. Use {@link ProjectedCoordinates#clip(double, ClipContext.Block, ClipContext.Fluid, Vector2dc, Entity)} if you only care about current mouse position
141144
*
142145
* @param x screen coordinate x-position
143146
* @param y screen coordinate y-position

0 commit comments

Comments
 (0)