This commit is contained in:
2026-10-02 11:07:09 -03:00
parent d5015edfa1
commit 9ca717d2d0
18 changed files with 449 additions and 38 deletions
+59 -7
View File
@@ -1,4 +1,4 @@
import { PLAYER_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_EYE_HEIGHT, TICK_DELTA } from "$/common/constants.ts";
import { back_off_from_edge, box_is_free } from "$/common/physics.ts";
import type { Client } from "../client.ts";
import { InputManager } from "../input_manager.ts";
@@ -55,6 +55,10 @@ const SWIM_DOWN = 0.04 / TICK_DELTA;
// be climbed onto
const WATER_HOP_HEIGHT = 0.6;
const WATER_HOP = 0.36 / TICK_DELTA;
// swimming, sprinting with the eyes under water: no gravity and less drag sideways, so it goes 4 blocks a second
const SWIM_DRAG = 0.9;
// and it turns up or down toward where it's looking, this much of the way each tick, like minecraft's Player.travel
const SWIM_STEER = 0.06;
// on land, the entity's own
const LAND_GRAVITY = -32;
const LAND_DRAG = 0.98;
@@ -115,11 +119,21 @@ export class LocalPlayer extends Player {
tick() {
this.#in_water = this.in_water;
this.gravity = this.#in_water ? WATER_GRAVITY : LAND_GRAVITY;
const was_swimming = this.swimming;
const input = this.client.screen ? NO_INPUT : this.#read_input();
this.#apply_input(input);
// after sprinting is, so letting go stands it up the same tick
this.#update_swimming();
// out of the water but still in the short box, under something low, it falls like on land
this.gravity = !this.#in_water ? LAND_GRAVITY : this.swimming ? 0 : WATER_GRAVITY;
this.drag = this.#in_water ? WATER_DRAG : LAND_DRAG;
this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input());
if (this.swimming && this.#in_water) this.#steer(input);
this.#prev_eye_height = this.eye_height;
const eye_height = this.sneaking ? SNEAK_EYE_HEIGHT : PLAYER_EYE_HEIGHT;
const eye_height = this.swimming
? PLAYER_SWIM_EYE_HEIGHT
: this.sneaking
? SNEAK_EYE_HEIGHT
: PLAYER_EYE_HEIGHT;
this.eye_height += (eye_height - this.eye_height) * EYE_EASE;
this.#prev_fov_scale = this.#fov_scale;
this.#fov_scale += ((this.sprinting ? SPRINT_FOV : 1) - this.#fov_scale) * FOV_EASE;
@@ -143,7 +157,43 @@ export class LocalPlayer extends Player {
if (this.#in_water) this.#hop_out_of_water(from_y);
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0;
this.#send_position(this.on_ground && !was_on_ground);
this.update_swim_animation();
this.#send_position((this.on_ground && !was_on_ground) || this.swimming !== was_swimming);
}
// like voxelibre: sprinting with the eyes under water starts it, and it keeps going while it sprints in water, along
// the surface too. it only stands back up where there's room, until then it stays in the short box
#update_swimming() {
if (!this.swimming) {
if (this.sprinting && this.eyes_in_water) this.set_swimming(true);
return;
}
if (this.sprinting && this.#in_water) return;
const standing = {
x: this.x,
y: this.y,
z: this.z,
width: this.width,
height: PLAYER_HEIGHT,
vx: 0,
vy: 0,
vz: 0,
};
if (box_is_free(standing, 0, 0, 0, (x, y, z) => this.level.has_collision(x, y, z))) this.set_swimming(false);
}
// turns up or down toward where it's looking. not up once its head is out, so it swims along the surface instead
// of leaping out of the water, unless it's holding jump
#steer(input: MoveInput) {
const look_y = Math.sin(this.pitch);
const head_in_water = this.level.is_liquid(
Math.floor(this.x),
Math.floor(this.y + this.height),
Math.floor(this.z),
);
if (look_y <= 0 || input.jump || head_in_water) {
this.vy += (look_y / TICK_DELTA - this.vy) * SWIM_STEER;
}
}
// turns with the mouse every frame, not every tick, like minecraft's MouseHandler.turnPlayer
@@ -156,7 +206,7 @@ export class LocalPlayer extends Player {
}
override horizontal_friction() {
if (this.#in_water) return WATER_DRAG;
if (this.#in_water) return this.swimming ? SWIM_DRAG : WATER_DRAG;
return this.on_ground ? GROUND_FRICTION : AIR_FRICTION;
}
@@ -241,7 +291,8 @@ export class LocalPlayer extends Player {
const into_wall = sin * this.colliding_x + cos * this.colliding_z;
if (!can_sprint || into_wall > SPRINT_WALL_ANGLE) {
this.sprinting = false;
} else if (input.sprint) {
} else if (input.sprint && (!this.#in_water || this.eyes_in_water)) {
// in water only with the eyes under, to swim, not to sprint along the bottom
this.sprinting = true;
}
}
@@ -266,6 +317,7 @@ export class LocalPlayer extends Player {
yaw: this.yaw,
pitch: this.pitch,
teleport: this.teleport_id,
...(this.swimming && { swimming: true }),
});
}
}
+45 -1
View File
@@ -1,10 +1,17 @@
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_SWIM_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
import { ItemStack } from "$/common/inventory.ts";
import type { ClientLevel } from "../level/client_level.ts";
import type { Texture } from "../renderer/mod.ts";
import { decode_skin } from "../skins.ts";
import { Entity } from "./entity.ts";
// how much of the way it tips over or stands up each tick, so it takes 10 ticks
const SWIM_EASE = 0.1;
// how much of the way the body turns toward which way it's going each tick
const SWIM_PITCH_EASE = 0.3;
// blocks a tick. slower than this the body lies the way they look instead, it's how they moved that's too jittery
const SWIM_MOVING = 0.05;
export abstract class Player extends Entity {
name: string;
// like minecraft's players: slabs yes, full blocks need a jump
@@ -18,6 +25,14 @@ export abstract class Player extends Entity {
slim = false;
// what they wear that shows, items with a worn model, see the server's player_worn
worn: ItemStack[] = [];
// lying flat in the short box, see set_swimming
swimming = false;
// how far over they've tipped, 0 standing to 1 lying flat, see update_swim_animation
swim_amount = 0;
prev_swim_amount = 0;
// which way the body lies while swimming, radians up from flat: the way they're going, or looking when still
swim_pitch = 0;
prev_swim_pitch = 0;
#skin_version = 0;
constructor(level: ClientLevel, id: string, name: string) {
@@ -25,6 +40,35 @@ export abstract class Player extends Entity {
this.name = name;
}
// swimming lies them flat in the short box, and not swimming stands them back up. the local player checks there's
// room first, remote players' clients already did
set_swimming(swimming: boolean) {
this.swimming = swimming;
this.height = swimming ? PLAYER_SWIM_HEIGHT : PLAYER_HEIGHT;
}
// after moving each tick, for tipping over and standing up smoothly, local and remote players alike. the pitch comes
// from how they moved, so remote players get it without anything more from the server
update_swim_animation() {
this.prev_swim_amount = this.swim_amount;
this.prev_swim_pitch = this.swim_pitch;
const amount = this.swimming ? 1 : 0;
this.swim_amount += Math.max(-SWIM_EASE, Math.min(SWIM_EASE, amount - this.swim_amount));
const [dx, dy, dz] = [this.x - this.prev_x, this.y - this.prev_y, this.z - this.prev_z];
const moving = Math.atan2(dy, Math.hypot(dx, dz));
const weight = Math.min(Math.hypot(dx, dy, dz) / SWIM_MOVING, 1);
const pitch = this.pitch + (moving - this.pitch) * weight;
this.swim_pitch += (pitch - this.swim_pitch) * SWIM_PITCH_EASE;
}
// a frame partial_tick of the way to the next tick
render_swim(partial_tick: number) {
return {
amount: this.prev_swim_amount + (this.swim_amount - this.prev_swim_amount) * partial_tick,
pitch: this.prev_swim_pitch + (this.swim_pitch - this.prev_swim_pitch) * partial_tick,
};
}
// a base64 png from the server, or undefined for the default. the old skin stays until the new one is decoded
async set_skin(skin: string | undefined, slim = false) {
const version = ++this.#skin_version;
+2
View File
@@ -29,6 +29,7 @@ export class RemotePlayer extends Player {
this.set_skin(info.skin, info.slim);
}
this.worn = (info.worn ?? []).map(ItemStack.from_data);
this.set_swimming(info.swimming === true);
}
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number) {
@@ -53,5 +54,6 @@ export class RemotePlayer extends Player {
this.y += (this.target_y - this.y) * t;
this.z += (this.target_z - this.z) * t;
this.update_walk_animation();
this.update_swim_animation();
}
}
+1
View File
@@ -159,6 +159,7 @@ function client_context(listing: ModListing): ClientContext {
const { x, y, z } = need_client().player;
return { x, y, z };
},
is_swimming: () => need_client().player.swimming,
},
world: {
get_block(x, y, z) {
+1
View File
@@ -90,6 +90,7 @@ export class ClientPacketListener {
const player = level.entities.get(message.id);
if (player instanceof RemotePlayer) {
player.lerp_to(message.x, message.y, message.z, message.yaw, message.pitch);
player.set_swimming(message.swimming === true);
}
break;
}
+59 -7
View File
@@ -1,9 +1,9 @@
// minecraft's player model and how it's posed each frame, for drawing players and what they wear. no drawing here, so
// tests can use it
import { PLAYER_HEIGHT } from "$/common/constants.ts";
import { PLAYER_HEIGHT, PLAYER_SWIM_HEIGHT } from "$/common/constants.ts";
import { PLAYER_PARTS, type WornProperties } from "$/common/everything_registry.ts";
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
import { apply, IDENTITY, type Matrix, rotation_around, type Vec3 } from "./model_math.ts";
import { apply, IDENTITY, type Matrix, multiply, rotation_around, type Vec3 } from "./model_math.ts";
export type PartName = typeof PLAYER_PARTS[number];
@@ -117,19 +117,38 @@ export interface PoseInput {
pitch: number;
walk: { position: number; speed: number };
slim: boolean;
// how far over they've tipped and which way the body lies, see Player.update_swim_animation. none is standing
swim?: { amount: number; pitch: number };
// ticks, for the arms drifting while they swim in place
time?: number;
}
// where a player is drawn this frame: each part's matrix, which turns its points (in skin pixels, around the model's
// feet, like a bone's) the way the part is turned, and where the model is
// feet, like a bone's) the way the part is turned, then the root's, which turns the whole model (lying flat to swim),
// and where the model is
export interface PlayerPose {
x: number;
y: number;
z: number;
yaw: number;
slim: boolean;
root: Matrix;
parts: Record<PartName, Matrix>;
}
// the model lies flat around the middle of its length, moved down to the middle of the swimming box
const SWIM_PIVOT: Vec3 = [0, 16, 0];
const SWIM_DROP = PLAYER_SWIM_HEIGHT / 2 / PIXEL - SWIM_PIVOT[1];
// the crawl: arms go around this much per walk step (once a second swimming at full speed), legs flutter this fast and this far
const STROKE_RATE = 0.3;
const FLUTTER_RATE = 1.2;
const FLUTTER = 0.3;
// arms out a little so they go around beside the head, not through it
const STROKE_SPREAD = 0.15;
// swimming in place, the arms and legs drift slowly, about every 2 seconds
const DRIFT_RATE = 0.15;
const DRIFT = 0.25;
export function pose_at(input: PoseInput): PlayerPose {
// limbs swing opposite to each other, like minecraft's HumanoidModel.setupAnim
const swing = Math.cos(input.walk.position * 0.6662) * input.walk.speed;
@@ -141,18 +160,51 @@ export function pose_at(input: PoseInput): PlayerPose {
right_leg: swing * 1.4,
left_leg: -swing * 1.4,
};
const spread: Record<PartName, number> = { head: 0, body: 0, right_arm: 0, left_arm: 0, right_leg: 0, left_leg: 0 };
let root = IDENTITY;
const swim = input.swim?.amount ?? 0;
if (swim > 0) {
// forward onto its front, then further for going down or less for going up
const tilt = swim * (Math.PI / 2 - input.swim!.pitch);
root = multiply([1, 0, 0, 0, 0, 1, 0, swim * SWIM_DROP, 0, 0, 1, 0], rotation_around(SWIM_PIVOT, -tilt, 0, 0));
// the head turns back up as much, so it faces where they look
angles.head = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, input.pitch + tilt));
const speed = input.walk.speed;
const drift = Math.sin((input.time ?? 0) * DRIFT_RATE) * DRIFT;
// arms go around half a turn apart, forward over the head and back under, like walking's -swing
const stroke = -input.walk.position * STROKE_RATE;
const arm = (offset: number) => drift * (1 - speed) + wrap(stroke + offset - drift) * speed;
const flutter = Math.cos(input.walk.position * FLUTTER_RATE) * FLUTTER * speed + drift * (1 - speed);
const swimming: Partial<Record<PartName, number>> = {
right_arm: arm(0),
left_arm: arm(Math.PI),
right_leg: flutter,
left_leg: -flutter,
};
for (const [name, angle] of Object.entries(swimming) as [PartName, number][]) {
angles[name] += (angle - angles[name]) * swim;
}
spread.right_arm = STROKE_SPREAD * swim;
spread.left_arm = -STROKE_SPREAD * swim;
}
const parts = {} as Record<PartName, Matrix>;
for (const part of input.slim ? SLIM_PARTS : PARTS) {
parts[part.name] = rotation_around(part.pivot, angles[part.name], 0, 0);
parts[part.name] = rotation_around(part.pivot, angles[part.name], 0, spread[part.name]);
}
return { x: input.x, y: input.y, z: input.z, yaw: input.yaw, slim: input.slim, parts };
return { x: input.x, y: input.y, z: input.z, yaw: input.yaw, slim: input.slim, root, parts };
}
// from the model's skin pixels to the world: scaled to the player's height, then turned to their yaw
// an angle the same way around, between -pi and pi
function wrap(angle: number): number {
return angle - 2 * Math.PI * Math.round(angle / (2 * Math.PI));
}
// from the model's skin pixels to the world: turned by the root, scaled to the player's height, then turned to their
// yaw
export function pose_to_world(pose: PlayerPose): (x: number, y: number, z: number) => Vec3 {
const [sin, cos] = [Math.sin(pose.yaw), Math.cos(pose.yaw)];
return (px, py, pz) => {
const [mx, my, mz] = [px * PIXEL, py * PIXEL, pz * PIXEL];
const [mx, my, mz] = apply(pose.root, px, py, pz).map((n) => n * PIXEL);
return [pose.x + mx * cos + mz * sin, pose.y + my, pose.z - mx * sin + mz * cos];
};
}
+3 -1
View File
@@ -65,7 +65,9 @@ const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: num
export function player_pose(player: Player, partial_tick: number): PlayerPose {
const { x, y, z } = player.render_position(partial_tick);
const { yaw, pitch, slim } = player;
return pose_at({ x, y, z, yaw, pitch, slim, walk: player.walk_animation(partial_tick) });
const walk = player.walk_animation(partial_tick);
const swim = player.render_swim(partial_tick);
return pose_at({ x, y, z, yaw, pitch, slim, walk, swim, time: player.level.time + partial_tick });
}
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture. tint