diff options
author | davidpkj <davidpenkow1@gmail.com> | 2022-12-13 15:03:29 +0100 |
---|---|---|
committer | davidpkj <davidpenkow1@gmail.com> | 2022-12-13 15:03:29 +0100 |
commit | fb5add1b17c40919a913d280758966b758601641 (patch) | |
tree | dc5fd8410ec8a8eb0d9a0fd5b0c1af74a33cc82d /src | |
parent | 13fc62a67d8d1183e4d01dd57ee4ac42466e9ef0 (diff) |
Add: physics
Diffstat (limited to 'src')
-rw-r--r-- | src/collisionCaster.ts | 39 |
1 files changed, 39 insertions, 0 deletions
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<THREE.Object3D>) => { + 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<THREE.Object3D>; + + 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); + */ + } +} |