Skip to content

Commit

Permalink
translation added to Gfx3PhysicsJNM
Browse files Browse the repository at this point in the history
  • Loading branch information
jay19240 committed Dec 19, 2024
1 parent 9502c82 commit e68adb4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 32 deletions.
31 changes: 31 additions & 0 deletions src/lib/core/tree_partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type SplitResult<T> = {
export interface ITreePartitionMethod<T> {
split(objects: Array<T>): SplitResult<T>;
search(node: TreePartitionNode<T>, ...params: any[]): Array<T>;
translate(x: number, y: number, z: number): void;
draw(): void;
}

Expand Down Expand Up @@ -35,6 +36,17 @@ class TreePartition<T> {
this.root.draw();
}

/**
* Translate the position.
*
* @param {number} x - The amount of translation in the x-axis direction.
* @param {number} y - The amount of translation in the y-axis direction.
* @param {number} z - The amount of translation in the z-axis direction.
*/
translate(x: number, y: number, z: number = 0): void {
this.root.translate(x, y, z);
}

/**
* Search and return all objects that intersect with the target.
*
Expand Down Expand Up @@ -113,6 +125,25 @@ class TreePartitionNode<T> {
}
}

/**
* Translate the position.
*
* @param {number} x - The amount of translation in the x-axis direction.
* @param {number} y - The amount of translation in the y-axis direction.
* @param {number} z - The amount of translation in the z-axis direction.
*/
translate(x: number, y: number, z: number = 0): void {
this.method.translate(x, y, z);

if (this.left) {
this.left.translate(x, y, z);
}

if (this.right) {
this.right.translate(x, y, z);
}
}

/**
* Search and return all objects that intersect with the target.
*
Expand Down
13 changes: 13 additions & 0 deletions src/lib/gfx2/gfx2_tree_partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class Gfx2TreePartitionMethod implements ITreePartitionMethod<Gfx2BoundingRect>
ctx.fillRect(this.rect.min[0], this.rect.min[1], size[0], size[1]);
}

/**
* Translate the position.
*
* @param {number} x - The amount of translation in the x-axis direction.
* @param {number} y - The amount of translation in the y-axis direction.
*/
translate(x: number, y: number) {
this.rect.min[0] += x;
this.rect.min[1] += y;
this.rect.max[0] += x;
this.rect.max[1] += y;
}

/**
* Search and return all objects that intersect with the target.
*
Expand Down
16 changes: 16 additions & 0 deletions src/lib/gfx3/gfx3_tree_partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ class Gfx3TreePartitionMethod implements ITreePartitionMethod<Gfx3BoundingBox> {
gfx3DebugRenderer.drawBoundingBox(UT.MAT4_IDENTITY(), this.box.min, this.box.max);
}

/**
* Translate the position.
*
* @param {number} x - The amount of translation in the x-axis direction.
* @param {number} y - The amount of translation in the y-axis direction.
* @param {number} z - The amount of translation in the z-axis direction.
*/
translate(x: number, y: number, z: number) {
this.box.min[0] += x;
this.box.min[1] += y;
this.box.min[2] += z;
this.box.max[0] += x;
this.box.max[1] += y;
this.box.max[2] += z;
}

/**
* Search and return all objects that intersect with the target.
*
Expand Down
82 changes: 50 additions & 32 deletions src/lib/gfx3_physics/gfx3_physics_jnm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gfx3DebugRenderer } from '../gfx3/gfx3_debug_renderer';
import { UT } from '../core/utils';
import { Gfx3BoundingBox } from '../gfx3/gfx3_bounding_box';
import { Gfx3TreePartition } from '../gfx3/gfx3_tree_partition';
import { Gfx3TreePartition, Gfx3TreePartitionMethod } from '../gfx3/gfx3_tree_partition';

class Frag extends Gfx3BoundingBox {
index: number;
Expand Down Expand Up @@ -74,7 +74,7 @@ class Gfx3PhysicsJNM {
* @param {number} bspMaxChildren - The maximum of children per bsp node.
* @param {number} bspMaxDepth - The maximum depth for bsp tree.
*/
async loadFromFile(path: string, bspMaxChildren: number = 20, bspMaxDepth: number = 10, transform?: mat4): Promise<void> {
async loadFromFile(path: string, bspMaxChildren: number = 20, bspMaxDepth: number = 10): Promise<void> {
const response = await fetch(path);
const json = await response.json();

Expand All @@ -85,24 +85,12 @@ class Gfx3PhysicsJNM {
this.boundingBox = new Gfx3BoundingBox(json['Min'], json['Max']);
this.btree = new Gfx3TreePartition(bspMaxChildren, bspMaxDepth, this.boundingBox);

if (transform) {
this.boundingBox = this.boundingBox.transform(transform);
}

this.frags = [];
for (let i = 0; i < json['NumFrags']; i++) {
const obj = json['Frags'][i];

let v1 = obj[0];
let v2 = obj[1];
let v3 = obj[2];

if (transform) {
v1 = UT.MAT4_MULTIPLY_BY_VEC4(transform, v1);
v2 = UT.MAT4_MULTIPLY_BY_VEC4(transform, v2);
v3 = UT.MAT4_MULTIPLY_BY_VEC4(transform, v3);
}

const v1 = obj[0];
const v2 = obj[1];
const v3 = obj[2];
const frag = new Frag(i, v1, v2, v3);
this.btree.addChild(frag);
this.frags.push(frag);
Expand All @@ -122,7 +110,7 @@ class Gfx3PhysicsJNM {
* @param {number} bspMaxChildren - The maximum of children per bsp node.
* @param {number} bspMaxDepth - The maximum depth for bsp tree.
*/
async loadFromBinaryFile(path: string, bspMaxChildren: number = 20, bspMaxDepth: number = 10, transform?: mat4): Promise<void> {
async loadFromBinaryFile(path: string, bspMaxChildren: number = 20, bspMaxDepth: number = 10): Promise<void> {
const response = await fetch(path);
const buffer = await response.arrayBuffer();
const data = new Float32Array(buffer);
Expand All @@ -143,22 +131,11 @@ class Gfx3PhysicsJNM {
this.boundingBox = new Gfx3BoundingBox([minX, minY, minZ], [maxX, maxY, maxZ]);
this.btree = new Gfx3TreePartition(bspMaxChildren, bspMaxDepth, this.boundingBox);

if (transform) {
this.boundingBox = this.boundingBox.transform(transform);
}

this.frags = [];
for (let i = 0; i < numFrags; i++) {
let v1: vec3 = [data[offset + (i * 9) + 0], data[offset + (i * 9) + 1], data[offset + (i * 9) + 2]];
let v2: vec3 = [data[offset + (i * 9) + 3], data[offset + (i * 9) + 4], data[offset + (i * 9) + 5]];
let v3: vec3 = [data[offset + (i * 9) + 6], data[offset + (i * 9) + 7], data[offset + (i * 9) + 8]];

if (transform) {
v1 = UT.MAT4_MULTIPLY_BY_VEC4(transform, [v1[0], v1[1], v1[2], 1]) as vec3;
v2 = UT.MAT4_MULTIPLY_BY_VEC4(transform, [v2[0], v2[1], v2[2], 1]) as vec3;
v3 = UT.MAT4_MULTIPLY_BY_VEC4(transform, [v3[0], v3[1], v3[2], 1]) as vec3;
}

const v1: vec3 = [data[offset + (i * 9) + 0], data[offset + (i * 9) + 1], data[offset + (i * 9) + 2]];
const v2: vec3 = [data[offset + (i * 9) + 3], data[offset + (i * 9) + 4], data[offset + (i * 9) + 5]];
const v3: vec3 = [data[offset + (i * 9) + 6], data[offset + (i * 9) + 7], data[offset + (i * 9) + 8]];
const frag = new Frag(i, v1, v2, v3);
this.btree.addChild(frag);
this.frags.push(frag);
Expand Down Expand Up @@ -203,6 +180,47 @@ class Gfx3PhysicsJNM {
}
}

/**
* Translate the position.
*
* @param {number} x - The amount of translation in the x-axis direction.
* @param {number} y - The amount of translation in the y-axis direction.
* @param {number} z - The amount of translation in the z-axis direction.
*/
translate(x: number, y: number, z: number): void {
this.boundingBox.min[0] += x;
this.boundingBox.min[1] += y;
this.boundingBox.min[2] += z;

this.boundingBox.max[0] += x;
this.boundingBox.max[1] += y;
this.boundingBox.max[2] += z;

this.btree.translate(x, y, z);

for (const frag of this.frags) {
frag.v1[0] += x;
frag.v1[1] += y;
frag.v1[2] += z;

frag.v2[0] += x;
frag.v2[1] += y;
frag.v2[2] += z;

frag.v3[0] += x;
frag.v3[1] += y;
frag.v3[2] += z;

frag.min[0] += x;
frag.min[1] += y;
frag.min[2] += z;

frag.max[0] += x;
frag.max[1] += y;
frag.max[2] += z;
}
}

/**
* Returns a new move with smooth sliding along wall and floor for the given box.
* Infos are composed to a move vector, a wall collide flag and floor collide flag.
Expand Down

0 comments on commit e68adb4

Please sign in to comment.