implement third person camera collision detection
This commit is contained in:
@@ -62,7 +62,7 @@ export default class IngameOverlay extends Gui {
|
|||||||
if (typeId !== 0) {
|
if (typeId !== 0) {
|
||||||
let renderId = "hotbar" + i;
|
let renderId = "hotbar" + i;
|
||||||
let block = Block.getById(typeId);
|
let block = Block.getById(typeId);
|
||||||
this.minecraft.itemRenderer.renderItemInGui(renderId, block, Math.floor(x + i * 20 + 11), Math.floor(y + 11));
|
this.minecraft.itemRenderer.renderItemInGui(renderId, block, Math.floor(x + i * 20 + 11), y + 11);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -366,6 +366,8 @@ export default class BlockRenderer {
|
|||||||
let maxY = 1;
|
let maxY = 1;
|
||||||
let maxZ = 1;
|
let maxZ = 1;
|
||||||
|
|
||||||
|
let offset = (1 / 256);
|
||||||
|
|
||||||
// UV Mapping
|
// UV Mapping
|
||||||
let textureIndex = block.getTextureForFace(EnumBlockFace.NORTH);
|
let textureIndex = block.getTextureForFace(EnumBlockFace.NORTH);
|
||||||
let minU = (textureIndex % 16) / 16.0;
|
let minU = (textureIndex % 16) / 16.0;
|
||||||
@@ -377,6 +379,11 @@ export default class BlockRenderer {
|
|||||||
minV = 1 - minV;
|
minV = 1 - minV;
|
||||||
maxV = 1 - maxV;
|
maxV = 1 - maxV;
|
||||||
|
|
||||||
|
minU += offset;
|
||||||
|
maxU -= offset;
|
||||||
|
minV -= offset;
|
||||||
|
maxV += offset;
|
||||||
|
|
||||||
// Render item
|
// Render item
|
||||||
this.addFace(null, EnumBlockFace.NORTH, false, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV);
|
this.addFace(null, EnumBlockFace.NORTH, false, minX, minY, minZ, maxX, maxY, maxZ, minU, minV, maxU, maxV);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import Block from "../world/block/Block.js";
|
|||||||
import Tessellator from "./Tessellator.js";
|
import Tessellator from "./Tessellator.js";
|
||||||
import ChunkSection from "../world/ChunkSection.js";
|
import ChunkSection from "../world/ChunkSection.js";
|
||||||
import Random from "../../util/Random.js";
|
import Random from "../../util/Random.js";
|
||||||
|
import Vector3 from "../../util/Vector3.js";
|
||||||
|
|
||||||
export default class WorldRenderer {
|
export default class WorldRenderer {
|
||||||
|
|
||||||
@@ -180,46 +181,94 @@ export default class WorldRenderer {
|
|||||||
orientCamera(partialTicks) {
|
orientCamera(partialTicks) {
|
||||||
let player = this.minecraft.player;
|
let player = this.minecraft.player;
|
||||||
|
|
||||||
let rotationX = -MathHelper.toRadians(player.rotationPitch);
|
// Reset rotation stack
|
||||||
let rotationY = -MathHelper.toRadians(player.rotationYaw + 180);
|
let stack = this.camera;
|
||||||
let rotationZ = 0;
|
stack.rotation.order = "ZYX";
|
||||||
|
|
||||||
// Position
|
// Position
|
||||||
let x = player.prevX + (player.x - player.prevX) * partialTicks;
|
let x = player.prevX + (player.x - player.prevX) * partialTicks;
|
||||||
let y = player.prevY + (player.y - player.prevY) * partialTicks;
|
let y = player.prevY + (player.y - player.prevY) * partialTicks + player.getEyeHeight();
|
||||||
let z = player.prevZ + (player.z - player.prevZ) * partialTicks;
|
let z = player.prevZ + (player.z - player.prevZ) * partialTicks;
|
||||||
|
|
||||||
|
// Rotation
|
||||||
|
let yaw = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * partialTicks;
|
||||||
|
let pitch = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * partialTicks;
|
||||||
|
|
||||||
// Add camera offset
|
// Add camera offset
|
||||||
let mode = this.minecraft.settings.thirdPersonView;
|
let mode = this.minecraft.settings.thirdPersonView;
|
||||||
if (mode !== 0) {
|
if (mode !== 0) {
|
||||||
// Flip for front view
|
let distance = WorldRenderer.THIRD_PERSON_DISTANCE;
|
||||||
let frontView = mode === 2;
|
let frontView = mode === 2;
|
||||||
|
|
||||||
|
// Handle front view
|
||||||
if (frontView) {
|
if (frontView) {
|
||||||
rotationY += Math.PI;
|
// Flip to calculate opposite side of the player
|
||||||
rotationX *= -1;
|
pitch += 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shift camera
|
// Calculate vector for back view and front view
|
||||||
let cameraOffsetX = Math.sin(rotationY) * Math.cos(rotationX);
|
let vector = player.getVectorForRotation(pitch, yaw);
|
||||||
let cameraOffsetY = Math.sin(-rotationX);
|
let vectorFlipped = player.getVectorForRotation(pitch + 180, yaw);
|
||||||
let cameraOffsetZ = Math.cos(rotationY) * Math.cos(rotationX);
|
|
||||||
|
|
||||||
x += cameraOffsetX * WorldRenderer.THIRD_PERSON_DISTANCE;
|
let rotationX = vector.x * distance;
|
||||||
y += cameraOffsetY * WorldRenderer.THIRD_PERSON_DISTANCE;
|
let rotationY = vector.y * distance;
|
||||||
z += cameraOffsetZ * WorldRenderer.THIRD_PERSON_DISTANCE;
|
let rotationZ = vector.z * distance;
|
||||||
|
|
||||||
|
// Collision detection
|
||||||
|
for (let i = 0; i < 8; i++) {
|
||||||
|
// Binary counting
|
||||||
|
let bit1 = (i & 1) * 2 - 1;
|
||||||
|
let bit2 = (i >> 1 & 1) * 2 - 1;
|
||||||
|
let bit3 = (i >> 2 & 1) * 2 - 1;
|
||||||
|
|
||||||
|
bit1 *= 0.1;
|
||||||
|
bit2 *= 0.1;
|
||||||
|
bit3 *= 0.1;
|
||||||
|
|
||||||
|
// Calculate ray trace from and to position
|
||||||
|
let from = new Vector3(x + bit1, y + bit2, z + bit3);
|
||||||
|
let to = new Vector3((x - rotationX) + bit1 + bit3, (y - rotationY) + bit2, (z - rotationZ) + bit3);
|
||||||
|
|
||||||
|
// Calculate ray trace
|
||||||
|
let target = this.minecraft.world.rayTraceBlocks(from, to);
|
||||||
|
if (target === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate distance to collision
|
||||||
|
let distanceToCollision = target.vector.distanceTo(new Vector3(x, y, z));
|
||||||
|
if (distanceToCollision < distance) {
|
||||||
|
distance = distanceToCollision;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move camera to third person sphere
|
||||||
|
x += vectorFlipped.x * distance;
|
||||||
|
y += vectorFlipped.y * distance;
|
||||||
|
z += vectorFlipped.z * distance;
|
||||||
|
|
||||||
|
// Handle front view
|
||||||
|
if (frontView) {
|
||||||
|
// Flip back
|
||||||
|
pitch += 180;
|
||||||
|
|
||||||
|
// Flip camera around
|
||||||
|
pitch *= -1;
|
||||||
|
yaw += 180;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update rotation
|
// Update camera rotation
|
||||||
this.camera.rotation.x = rotationX;
|
stack.rotation.x = -MathHelper.toRadians(pitch);
|
||||||
this.camera.rotation.y = rotationY;
|
stack.rotation.y = -MathHelper.toRadians(yaw + 180);
|
||||||
this.camera.rotation.z = rotationZ;
|
stack.rotation.z = 0;
|
||||||
|
|
||||||
// Update camera position
|
// Update camera position
|
||||||
this.camera.position.set(x, y + player.getEyeHeight(), z);
|
stack.position.set(x, y, z);
|
||||||
|
|
||||||
// Apply bobbing animation
|
// Apply bobbing animation
|
||||||
if (mode === 0 && this.minecraft.settings.viewBobbing) {
|
if (mode === 0 && this.minecraft.settings.viewBobbing) {
|
||||||
this.bobbingAnimation(player, this.camera, partialTicks);
|
this.bobbingAnimation(player, stack, partialTicks);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update FOV
|
// Update FOV
|
||||||
|
|||||||
@@ -69,6 +69,9 @@ export default class SoundManager {
|
|||||||
} else if (pool.length > 0) {
|
} else if (pool.length > 0) {
|
||||||
// Play random sound in pool
|
// Play random sound in pool
|
||||||
let sound = pool[Math.floor(Math.random() * pool.length)];
|
let sound = pool[Math.floor(Math.random() * pool.length)];
|
||||||
|
if (typeof volume === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Stop previous sound
|
// Stop previous sound
|
||||||
if (sound.isPlaying) {
|
if (sound.isPlaying) {
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ export default class Vector3 {
|
|||||||
return new Vector3(this.x + x, this.y + y, this.z + z);
|
return new Vector3(this.x + x, this.y + y, this.z + z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
distanceTo(vec) {
|
||||||
|
return Math.sqrt(this.squareDistanceTo(vec));
|
||||||
|
}
|
||||||
|
|
||||||
squareDistanceTo(vec) {
|
squareDistanceTo(vec) {
|
||||||
let d0 = vec.x - this.x;
|
let d0 = vec.x - this.x;
|
||||||
let d1 = vec.y - this.y;
|
let d1 = vec.y - this.y;
|
||||||
|
|||||||
Reference in New Issue
Block a user