aboutsummaryrefslogtreecommitdiff
path: root/src/collisionCaster.ts
blob: e37ecbd7df3056ae2ff3b251e458f7c634618838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
    */
  }
}