aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidpkj <davidpenkow1@gmail.com>2022-12-13 15:03:29 +0100
committerdavidpkj <davidpenkow1@gmail.com>2022-12-13 15:03:29 +0100
commitfb5add1b17c40919a913d280758966b758601641 (patch)
treedc5fd8410ec8a8eb0d9a0fd5b0c1af74a33cc82d
parent13fc62a67d8d1183e4d01dd57ee4ac42466e9ef0 (diff)
Add: physics
-rw-r--r--src/collisionCaster.ts39
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);
+ */
+ }
+}