start work on AABB

This commit is contained in:
Holly Stubbs 2023-11-08 15:45:25 +00:00
parent 875318db04
commit 6033c86247
6 changed files with 63 additions and 5 deletions

53
server/AABB.ts Normal file
View file

@ -0,0 +1,53 @@
import { FunkyArray } from "../funkyArray";
import Vec3 from "./Vec3";
// Based on this MDN article:
// https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection
export default class AABB {
private static readonly aabbPool:FunkyArray<string, AABB> = new FunkyArray<string, AABB>();
public readonly aabbPoolString:string;
public min:Vec3;
public max:Vec3;
public constructor(minXOrMin:Vec3 | number, minYOrMax:Vec3 | number, minZ?:number, maxX?:number, maxY?:number, maxZ?:number) {
if (minXOrMin instanceof Vec3 && minYOrMax instanceof Vec3) {
this.min = minXOrMin;
this.max = minYOrMax;
} else if (typeof(minXOrMin) === "number" && typeof(minYOrMax) === "number" && typeof(minZ) === "number" && typeof(maxX) === "number" && typeof(maxY) === "number" && typeof(maxZ) === "number") {
this.min = new Vec3(minXOrMin, minYOrMax, minZ);
this.max = new Vec3(maxX, maxY, maxZ);
} else {
throw new Error("Invalid input parameters: AABB must be supplied with either two Vec3 with the min and max bounds or the raw bounds.");
}
this.aabbPoolString = AABB.createAABBPoolString(this.min.x, this.min.y, this.min.z, this.max.x, this.max.y, this.max.z);
if (!AABB.aabbPool.has(this.aabbPoolString)) {
AABB.aabbPool.set(this.aabbPoolString, this);
}
console.log(this);
}
public static createAABBPoolString(minX:number, minY:number, minZ:number, maxX:number, maxY:number, maxZ:number) {
return `m${minX}c${minY}a${minZ}a${maxX}b${maxY}b${maxZ}`;
}
public static getAABB(minX:number, minY:number, minZ:number, maxX:number, maxY:number, maxZ:number) {
const aabbPoolString = this.createAABBPoolString(minX, minY, minZ, maxX, maxY, maxZ);
if (!AABB.aabbPool.has(aabbPoolString)) {
return AABB.aabbPool.get(aabbPoolString);
}
return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
}
intersects(a:AABB, b:AABB) {
return a.min.x <= b.max.x && a.max.x >= b.min.x && a.min.y <= b.max.y && a.max.y >= b.min.y && a.min.z <= b.max.z && a.max.z >= b.min.z;
}
intersectionAmount(a:AABB, b:AABB) {
}
}

View file

@ -12,7 +12,7 @@ import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
import { PacketPlayerDigging } from "./packets/PlayerDigging";
import { Player } from "./entities/Player";
import { Socket } from "net";
import { Vec3 } from "./Vec3";
import Vec3 from "./Vec3";
import { PacketRespawn } from "./packets/Respawn";
import { PacketSpawnPosition } from "./packets/SpawnPosition";
import { PacketPlayerBlockPlacement } from "./packets/PlayerBlockPlacement";

View file

@ -1,4 +1,4 @@
export class Vec3 {
export default class Vec3 {
public x:number;
public y:number;
public z:number;

View file

@ -1,7 +1,8 @@
import { Chunk } from "../Chunk";
import { MetadataEntry, MetadataWriter } from "../MetadataWriter";
import { Rotation } from "../Rotation";
import { Vec3 } from "../Vec3";
import { Vec2 } from "../Vec2";
import Vec3 from "../Vec3";
import { World } from "../World";
import { MetadataFieldType } from "../enums/MetadataFieldType";
import { PacketEntityLook } from "../packets/EntityLook";
@ -17,6 +18,8 @@ export class Entity implements IEntity {
public entityId:number;
public entitySize:Vec2;
public world:World;
public position:Vec3;
@ -52,6 +55,8 @@ export class Entity implements IEntity {
public constructor(world:World) {
this.entityId = Entity.nextEntityId++;
this.entitySize = new Vec2(0.6, 1.8);
this.fire = this.fallDistance = 0;
this.onGround = false;

View file

@ -1,5 +1,5 @@
import { Rotation } from "../Rotation";
import { Vec3 } from "../Vec3";
import Vec3 from "../Vec3";
import { World } from "../World";
import { Block } from "../blocks/Block";
import { EntityStatus } from "../enums/EntityStatus";

View file

@ -1,4 +1,4 @@
import { Vec3 } from "../Vec3"
import Vec3 from "../Vec3"
export interface IEntity {
entityId:number,