From fb5add1b17c40919a913d280758966b758601641 Mon Sep 17 00:00:00 2001 From: davidpkj Date: Tue, 13 Dec 2022 15:03:29 +0100 Subject: Add: physics --- src/collisionCaster.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/collisionCaster.ts diff --git a/src/collisionCaster.ts b/src/collisionCaster.ts new file mode 100644 index 0000000..e37ecbd --- /dev/null +++ b/src/collisionCaster.ts @@ -0,0 +1,39 @@ +import * as THREE from "three"; + +const calculateDistanceFromRay = (ray: THREE.Raycaster, clips: Array) => { + const intersected = ray.intersectObjects(clips, true); + + if (intersected.length > 0) return parseFloat(intersected[0].distance.toPrecision(3)); + + return 0; +} + +export class CollisionCaster { + private _position: THREE.Vector3; + private _length: number; + private _clips: Array; + + up: Number; + down: Number; + /* + front: Number; + behind: Number; + right: Number; + left: Number; + */ + + constructor(position, length, clips) { + this._position = position; + this._length = length; + this._clips = clips; + + this.up = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, 1, 0), 0, this._length), this._clips); + this.down = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, -1, 0), 0, this._length), this._clips); + /* + this.front = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, -1, 0), 0, this._length), clips); + this.behind = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, -1, 0), 0, this._length), clips); + this.right = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, -1, 0), 0, this._length), clips); + this.left = calculateDistanceFromRay(new THREE.Raycaster(this._position, new THREE.Vector3(0, -1, 0), 0, this._length), clips); + */ + } +} -- cgit v1.2.3