Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ record UploadingContraptionChunk(int[] data) {
final UploadingContraptionChunk chunk = chunks.computeIfAbsent(sectionPos.asLong(), longPos -> new UploadingContraptionChunk(new int[LevelChunkSection.SECTION_SIZE]));

final VoxelNeighborhoodState state = VoxelNeighborhoodState.CORNER;
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(blockState);
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(blockState, blockPos.set(x, y, z));

final int index = (x & 15) + ((z & 15) << 4) + ((y & 15) << 8);

Expand Down Expand Up @@ -346,7 +346,7 @@ public void handleChunkSectionAddition(final LevelChunkSection section, final in
for (int by = 0; by < 16; by++) {
final BlockPos globalPos = new BlockPos(bx, by, bz).offset(sectionPos.minBlockX(), sectionPos.minBlockY(), sectionPos.minBlockZ());
final VoxelNeighborhoodState state = VoxelNeighborhoodState.getState(this.accelerator, globalPos, chunk);
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(this.accelerator.getBlockState(globalPos));
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(this.accelerator.getBlockState(globalPos), globalPos);

final int index = bx + (bz << 4) + (by << 8);

Expand Down Expand Up @@ -392,15 +392,15 @@ public void handleBlockChange(final SectionPos sectionPos, final LevelChunkSecti
for (final Direction dir : Direction.values()) {
final BlockPos pos = globalBlockPos.relative(dir);
final VoxelNeighborhoodState state = VoxelNeighborhoodState.getState(this.accelerator, pos, null);
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(this.level.getBlockState(pos));
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(this.level.getBlockState(pos), pos);

final int colliderValue = colliderData == null ? 0 : colliderData.handle() + 1;
Rapier3D.changeBlock(this.scene.handle(), pos.getX(), pos.getY(), pos.getZ(), packBlockState(state, colliderValue));
}

// do it for the block without offset
final VoxelNeighborhoodState state = VoxelNeighborhoodState.getState(this.accelerator, globalBlockPos, null);
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(newState);
final RapierVoxelColliderData colliderData = this.colliderBakery.getPhysicsDataForBlock(newState, globalBlockPos);

final int colliderValue = colliderData == null ? 0 : colliderData.handle() + 1;
Rapier3D.changeBlock(this.scene.handle(), x, y, z, packBlockState(state, colliderValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.ryanhcode.sable.physics.impl.rapier.collider;

import com.mojang.datafixers.util.Pair;
import dev.ryanhcode.sable.api.block.BlockSubLevelCollisionShape;
import dev.ryanhcode.sable.api.block.BlockWithSubLevelCollisionCallback;
import dev.ryanhcode.sable.api.physics.callback.BlockSubLevelCollisionCallback;
Expand Down Expand Up @@ -28,7 +29,7 @@
*/
public class RapierVoxelColliderBakery {
private final @NotNull PhysicsColliderBlockGetter level;
private final Function<BlockState, RapierVoxelColliderData> blockPhysicsDataBuilder = Util.memoize(this::buildPhysicsDataForBlock);
private final Function<Pair<BlockState, BlockPos>, RapierVoxelColliderData> blockPhysicsDataBuilder = Util.memoize(this::buildPhysicsDataForBlock);

/**
* Creates a new level collider for the given level
Expand All @@ -49,10 +50,12 @@ public RapierVoxelColliderBakery(@NotNull final BlockGetter blockGetter) {
/**
* Builds a box or compound collision shape
*
* @param childState the state to build the shape for
* @param childBlock the position and state to build the shape for
* @return the physics data ID for the block at the given position, or null for empty
*/
private @NotNull RapierVoxelColliderData buildPhysicsDataForBlock(final BlockState childState) {
private @NotNull RapierVoxelColliderData buildPhysicsDataForBlock(final Pair<BlockState, BlockPos> childBlock) {
final BlockState childState = childBlock.getFirst();
final BlockPos childPos = childBlock.getSecond();
final boolean liquid = VoxelNeighborhoodState.isLiquid(childState);

final double friction = PhysicsBlockPropertyHelper.getFriction(childState);
Expand All @@ -72,7 +75,7 @@ public RapierVoxelColliderBakery(@NotNull final BlockGetter blockGetter) {
if (childState.getBlock() instanceof final BlockSubLevelCollisionShape extension) {
shape = extension.getSubLevelCollisionShape(this.level, childState);
} else {
shape = childState.getCollisionShape(this.level, BlockPos.ZERO, SableCollisionContext.get());
shape = childState.getCollisionShape(this.level, childPos, SableCollisionContext.get());
}
this.level.setup(Blocks.AIR.defaultBlockState());

Expand All @@ -98,8 +101,8 @@ public RapierVoxelColliderBakery(@NotNull final BlockGetter blockGetter) {
* @param state the state to build the shape for
* @return the physics data ID for the block at the given position, or null for empty
*/
public @Nullable RapierVoxelColliderData getPhysicsDataForBlock(final BlockState state) {
final RapierVoxelColliderData data = this.blockPhysicsDataBuilder.apply(Objects.requireNonNull(state, "state"));
public @Nullable RapierVoxelColliderData getPhysicsDataForBlock(final BlockState state, final BlockPos pos) {
final RapierVoxelColliderData data = this.blockPhysicsDataBuilder.apply(new Pair<BlockState, BlockPos>(Objects.requireNonNull(state,"state"), Objects.requireNonNull(pos, "pos")));
return data == RapierVoxelColliderData.EMPTY ? null : data;
}
}