From 6033c86247eae6a3368d2b9415d270303a8c2130 Mon Sep 17 00:00:00 2001 From: Holly Date: Wed, 8 Nov 2023 15:45:25 +0000 Subject: [PATCH] start work on AABB --- server/AABB.ts | 53 +++++++++++++++++++++++++++++++++ server/MPClient.ts | 2 +- server/Vec3.ts | 2 +- server/entities/Entity.ts | 7 ++++- server/entities/EntityLiving.ts | 2 +- server/entities/IEntity.ts | 2 +- 6 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 server/AABB.ts diff --git a/server/AABB.ts b/server/AABB.ts new file mode 100644 index 0000000..00cf3b3 --- /dev/null +++ b/server/AABB.ts @@ -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 = new FunkyArray(); + + 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) { + + } +} \ No newline at end of file diff --git a/server/MPClient.ts b/server/MPClient.ts index 4d84371..a8425b8 100644 --- a/server/MPClient.ts +++ b/server/MPClient.ts @@ -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"; diff --git a/server/Vec3.ts b/server/Vec3.ts index c71ed9b..9ccb132 100644 --- a/server/Vec3.ts +++ b/server/Vec3.ts @@ -1,4 +1,4 @@ -export class Vec3 { +export default class Vec3 { public x:number; public y:number; public z:number; diff --git a/server/entities/Entity.ts b/server/entities/Entity.ts index 8e88db9..43dfc65 100644 --- a/server/entities/Entity.ts +++ b/server/entities/Entity.ts @@ -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; @@ -51,6 +54,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; diff --git a/server/entities/EntityLiving.ts b/server/entities/EntityLiving.ts index 47442f6..cdd92e8 100644 --- a/server/entities/EntityLiving.ts +++ b/server/entities/EntityLiving.ts @@ -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"; diff --git a/server/entities/IEntity.ts b/server/entities/IEntity.ts index 810dae8..621bca1 100644 --- a/server/entities/IEntity.ts +++ b/server/entities/IEntity.ts @@ -1,4 +1,4 @@ -import { Vec3 } from "../Vec3" +import Vec3 from "../Vec3" export interface IEntity { entityId:number,