VoxelShape .java

net.minecraft.world.phys.shapes.VoxelShape (class): members: singleEncompassing, closestPointTo, getFaceShape, isCubeLike, collideX, toAabbs, forAllBoxes.

net.minecraft.world.phys.shapesclass318 linescommon9.8 KB

API Surface (23)

double min(final Direction.Axis axis)
double max(final Direction.Axis axis)
AABB bounds()
VoxelShape singleEncompassing()
double get(final Direction.Axis axis, final int i)
boolean isEmpty()
VoxelShape move(final Vec3 delta)
VoxelShape move(final Vec3i delta)
VoxelShape move(final double dx, final double dy, final double dz)
VoxelShape optimize()
void forAllEdges(final Shapes.DoubleLineConsumer consumer)
void forAllBoxes(final Shapes.DoubleLineConsumer consumer)
List<AABB> toAabbs()
double min(final Direction.Axis aAxis, final double b, final double c)
double max(final Direction.Axis aAxis, final double b, final double c)
int findIndex(final Direction.Axis axis, final double coord)
Optional<Vec3> closestPointTo(final Vec3 point)
VoxelShape getFaceShape(final Direction direction)
boolean isCubeLike()
double collide(final Direction.Axis axis, final AABB moving, final double distance)
double collideX(final AxisCycle transform, final AABB moving, double distance)
boolean equals(final Object obj)
String toString()

Fields & Constants (2)

shape
faces

String literals (3)

"No bounds for empty shape."
"EMPTY"
"VoxelShape["

Full Source

VoxelShape.java318 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.phys.shapes;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.math.DoubleMath;
5
import it.unimi.dsi.fastutil.doubles.DoubleList;
6
import java.util.List;
7
import java.util.Objects;
8
import java.util.Optional;
9
import net.minecraft.core.AxisCycle;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Direction;
12
import net.minecraft.core.Vec3i;
13
import net.minecraft.util.Mth;
14
import net.minecraft.util.Util;
15
import net.minecraft.world.phys.AABB;
16
import net.minecraft.world.phys.BlockHitResult;
17
import net.minecraft.world.phys.Vec3;
18
import org.apache.commons.lang3.mutable.MutableObject;
19
import org.jspecify.annotations.Nullable;
20
21
public abstract class VoxelShape {
22
	protected final DiscreteVoxelShape shape;
23
	private @Nullable VoxelShape @Nullable [] faces;
24
25
	protected VoxelShape(final DiscreteVoxelShape shape) {
26
		this.shape = shape;
27
	}
28
29
	public double min(final Direction.Axis axis) {
30
		int i = this.shape.firstFull(axis);
31
		return i >= this.shape.getSize(axis) ? Double.POSITIVE_INFINITY : this.get(axis, i);
32
	}
33
34
	public double max(final Direction.Axis axis) {
35
		int i = this.shape.lastFull(axis);
36
		return i <= 0 ? Double.NEGATIVE_INFINITY : this.get(axis, i);
37
	}
38
39
	public AABB bounds() {
40
		if (this.isEmpty()) {
41
			throw (UnsupportedOperationException)Util.pauseInIde(new UnsupportedOperationException("No bounds for empty shape."));
42
		} else {
43
			return new AABB(
44
				this.min(Direction.Axis.X),
45
				this.min(Direction.Axis.Y),
46
				this.min(Direction.Axis.Z),
47
				this.max(Direction.Axis.X),
48
				this.max(Direction.Axis.Y),
49
				this.max(Direction.Axis.Z)
50
			);
51
		}
52
	}
53
54
	public VoxelShape singleEncompassing() {
55
		return this.isEmpty()
56
			? Shapes.empty()
57
			: Shapes.box(
58
				this.min(Direction.Axis.X),
59
				this.min(Direction.Axis.Y),
60
				this.min(Direction.Axis.Z),
61
				this.max(Direction.Axis.X),
62
				this.max(Direction.Axis.Y),
63
				this.max(Direction.Axis.Z)
64
			);
65
	}
66
67
	protected double get(final Direction.Axis axis, final int i) {
68
		return this.getCoords(axis).getDouble(i);
69
	}
70
71
	public abstract DoubleList getCoords(final Direction.Axis axis);
72
73
	public boolean isEmpty() {
74
		return this.shape.isEmpty();
75
	}
76
77
	public VoxelShape move(final Vec3 delta) {
78
		return this.move(delta.x, delta.y, delta.z);
79
	}
80
81
	public VoxelShape move(final Vec3i delta) {
82
		return this.move(delta.getX(), delta.getY(), delta.getZ());
83
	}
84
85
	public VoxelShape move(final double dx, final double dy, final double dz) {
86
		return this.isEmpty()
87
			? Shapes.empty()
88
			: new ArrayVoxelShape(
89
				this.shape,
90
				new OffsetDoubleList(this.getCoords(Direction.Axis.X), dx),
91
				new OffsetDoubleList(this.getCoords(Direction.Axis.Y), dy),
92
				new OffsetDoubleList(this.getCoords(Direction.Axis.Z), dz)
93
			);
94
	}
95
96
	public VoxelShape optimize() {
97
		VoxelShape[] result = new VoxelShape[]{Shapes.empty()};
98
		this.forAllBoxes((x1, y1, z1, x2, y2, z2) -> result[0] = Shapes.joinUnoptimized(result[0], Shapes.box(x1, y1, z1, x2, y2, z2), BooleanOp.OR));
99
		return result[0];
100
	}
101
102
	public void forAllEdges(final Shapes.DoubleLineConsumer consumer) {
103
		this.shape
104
			.forAllEdges(
105
				(xi1, yi1, zi1, xi2, yi2, zi2) -> consumer.consume(
106
					this.get(Direction.Axis.X, xi1),
107
					this.get(Direction.Axis.Y, yi1),
108
					this.get(Direction.Axis.Z, zi1),
109
					this.get(Direction.Axis.X, xi2),
110
					this.get(Direction.Axis.Y, yi2),
111
					this.get(Direction.Axis.Z, zi2)
112
				),
113
				true
114
			);
115
	}
116
117
	public void forAllBoxes(final Shapes.DoubleLineConsumer consumer) {
118
		DoubleList xCoords = this.getCoords(Direction.Axis.X);
119
		DoubleList yCoords = this.getCoords(Direction.Axis.Y);
120
		DoubleList zCoords = this.getCoords(Direction.Axis.Z);
121
		this.shape
122
			.forAllBoxes(
123
				(xi1, yi1, zi1, xi2, yi2, zi2) -> consumer.consume(
124
					xCoords.getDouble(xi1), yCoords.getDouble(yi1), zCoords.getDouble(zi1), xCoords.getDouble(xi2), yCoords.getDouble(yi2), zCoords.getDouble(zi2)
125
				),
126
				true
127
			);
128
	}
129
130
	public List<AABB> toAabbs() {
131
		List<AABB> list = Lists.newArrayList();
132
		this.forAllBoxes((x1, y1, z1, x2, y2, z2) -> list.add(new AABB(x1, y1, z1, x2, y2, z2)));
133
		return list;
134
	}
135
136
	public double min(final Direction.Axis aAxis, final double b, final double c) {
137
		Direction.Axis bAxis = AxisCycle.FORWARD.cycle(aAxis);
138
		Direction.Axis cAxis = AxisCycle.BACKWARD.cycle(aAxis);
139
		int bi = this.findIndex(bAxis, b);
140
		int ci = this.findIndex(cAxis, c);
141
		int i = this.shape.firstFull(aAxis, bi, ci);
142
		return i >= this.shape.getSize(aAxis) ? Double.POSITIVE_INFINITY : this.get(aAxis, i);
143
	}
144
145
	public double max(final Direction.Axis aAxis, final double b, final double c) {
146
		Direction.Axis bAxis = AxisCycle.FORWARD.cycle(aAxis);
147
		Direction.Axis cAxis = AxisCycle.BACKWARD.cycle(aAxis);
148
		int bi = this.findIndex(bAxis, b);
149
		int ci = this.findIndex(cAxis, c);
150
		int i = this.shape.lastFull(aAxis, bi, ci);
151
		return i <= 0 ? Double.NEGATIVE_INFINITY : this.get(aAxis, i);
152
	}
153
154
	protected int findIndex(final Direction.Axis axis, final double coord) {
155
		return Mth.binarySearch(0, this.shape.getSize(axis) + 1, index -> coord < this.get(axis, index)) - 1;
156
	}
157
158
	public @Nullable BlockHitResult clip(final Vec3 from, final Vec3 to, final BlockPos pos) {
159
		if (this.isEmpty()) {
160
			return null;
161
		}
162
163
		Vec3 diff = to.subtract(from);
164
		if (diff.lengthSqr() < 1.0E-7) {
165
			return null;
166
		}
167
168
		Vec3 testPoint = from.add(diff.scale(0.001));
169
		return this.shape
170
				.isFullWide(
171
					this.findIndex(Direction.Axis.X, testPoint.x - pos.getX()),
172
					this.findIndex(Direction.Axis.Y, testPoint.y - pos.getY()),
173
					this.findIndex(Direction.Axis.Z, testPoint.z - pos.getZ())
174
				)
175
			? new BlockHitResult(testPoint, Direction.getApproximateNearest(diff.x, diff.y, diff.z).getOpposite(), pos, true)
176
			: AABB.clip(this.toAabbs(), from, to, pos);
177
	}
178
179
	public Optional<Vec3> closestPointTo(final Vec3 point) {
180
		if (this.isEmpty()) {
181
			return Optional.empty();
182
		}
183
184
		MutableObject<Vec3> closest = new MutableObject<>();
185
		this.forAllBoxes((x1, y1, z1, x2, y2, z2) -> {
186
			double x = Mth.clamp(point.x(), x1, x2);
187
			double y = Mth.clamp(point.y(), y1, y2);
188
			double z = Mth.clamp(point.z(), z1, z2);
189
			Vec3 currentClosest = closest.get();
190
			if (currentClosest == null || point.distanceToSqr(x, y, z) < point.distanceToSqr(currentClosest)) {
191
				closest.setValue(new Vec3(x, y, z));
192
			}
193
		});
194
		return Optional.of(Objects.requireNonNull(closest.get()));
195
	}
196
197
	public VoxelShape getFaceShape(final Direction direction) {
198
		if (!this.isEmpty() && this != Shapes.block()) {
199
			if (this.faces != null) {
200
				VoxelShape face = this.faces[direction.ordinal()];
201
				if (face != null) {
202
					return face;
203
				}
204
			} else {
205
				this.faces = new VoxelShape[6];
206
			}
207
208
			VoxelShape face = this.calculateFace(direction);
209
			this.faces[direction.ordinal()] = face;
210
			return face;
211
		} else {
212
			return this;
213
		}
214
	}
215
216
	private VoxelShape calculateFace(final Direction direction) {
217
		Direction.Axis axis = direction.getAxis();
218
		if (this.isCubeLikeAlong(axis)) {
219
			return this;
220
		} else {
221
			Direction.AxisDirection sign = direction.getAxisDirection();
222
			int index = this.findIndex(axis, sign == Direction.AxisDirection.POSITIVE ? 0.9999999 : 1.0E-7);
223
			SliceShape slice = new SliceShape(this, axis, index);
224
			if (slice.isEmpty()) {
225
				return Shapes.empty();
226
			} else {
227
				return slice.isCubeLike() ? Shapes.block() : slice;
228
			}
229
		}
230
	}
231
232
	protected boolean isCubeLike() {
233
		for (Direction.Axis axis : Direction.Axis.VALUES) {
234
			if (!this.isCubeLikeAlong(axis)) {
235
				return false;
236
			}
237
		}
238
239
		return true;
240
	}
241
242
	private boolean isCubeLikeAlong(final Direction.Axis axis) {
243
		DoubleList coords = this.getCoords(axis);
244
		return coords.size() == 2 && DoubleMath.fuzzyEquals(coords.getDouble(0), 0.0, 1.0E-7) && DoubleMath.fuzzyEquals(coords.getDouble(1), 1.0, 1.0E-7);
245
	}
246
247
	public double collide(final Direction.Axis axis, final AABB moving, final double distance) {
248
		return this.collideX(AxisCycle.between(axis, Direction.Axis.X), moving, distance);
249
	}
250
251
	protected double collideX(final AxisCycle transform, final AABB moving, double distance) {
252
		if (this.isEmpty()) {
253
			return distance;
254
		}
255
256
		if (Math.abs(distance) < 1.0E-7) {
257
			return 0.0;
258
		}
259
260
		AxisCycle inverse = transform.inverse();
261
		Direction.Axis aAxis = inverse.cycle(Direction.Axis.X);
262
		Direction.Axis bAxis = inverse.cycle(Direction.Axis.Y);
263
		Direction.Axis cAxis = inverse.cycle(Direction.Axis.Z);
264
		double maxA = moving.max(aAxis);
265
		double minA = moving.min(aAxis);
266
		int aMin = this.findIndex(aAxis, minA + 1.0E-7);
267
		int aMax = this.findIndex(aAxis, maxA - 1.0E-7);
268
		int bMin = Math.max(0, this.findIndex(bAxis, moving.min(bAxis) + 1.0E-7));
269
		int bMax = Math.min(this.shape.getSize(bAxis), this.findIndex(bAxis, moving.max(bAxis) - 1.0E-7) + 1);
270
		int cMin = Math.max(0, this.findIndex(cAxis, moving.min(cAxis) + 1.0E-7));
271
		int cMax = Math.min(this.shape.getSize(cAxis), this.findIndex(cAxis, moving.max(cAxis) - 1.0E-7) + 1);
272
		int aSize = this.shape.getSize(aAxis);
273
		if (distance > 0.0) {
274
			for (int a = aMax + 1; a < aSize; a++) {
275
				for (int b = bMin; b < bMax; b++) {
276
					for (int c = cMin; c < cMax; c++) {
277
						if (this.shape.isFullWide(inverse, a, b, c)) {
278
							double newDistance = this.get(aAxis, a) - maxA;
279
							if (newDistance >= -1.0E-7) {
280
								distance = Math.min(distance, newDistance);
281
							}
282
283
							return distance;
284
						}
285
					}
286
				}
287
			}
288
		} else if (distance < 0.0) {
289
			for (int a = aMin - 1; a >= 0; a--) {
290
				for (int b = bMin; b < bMax; b++) {
291
					for (int c = cMin; c < cMax; c++) {
292
						if (this.shape.isFullWide(inverse, a, b, c)) {
293
							double newDistance = this.get(aAxis, a + 1) - minA;
294
							if (newDistance <= 1.0E-7) {
295
								distance = Math.max(distance, newDistance);
296
							}
297
298
							return distance;
299
						}
300
					}
301
				}
302
			}
303
		}
304
305
		return distance;
306
	}
307
308
	@Override
309
	public boolean equals(final Object obj) {
310
		return super.equals(obj);
311
	}
312
313
	@Override
314
	public String toString() {
315
		return this.isEmpty() ? "EMPTY" : "VoxelShape[" + this.bounds() + "]";
316
	}
317
}
318

Decompiled Minecraft 26.3 source of VoxelShape (net/minecraft/world/phys/shapes/VoxelShape.java) with Mojang official mappings, stored verbatim on the site.