BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / util / ray.js

ray.js

ray.js 3.5 KiB Download Raw
1class Ray
2{
3 static Cast (scene, origin, direction, maxLength = -1)
4 {
5 direction = direction.normalize();
6 const hits = []; // {hitObj, hitPoint}
7 for (var i = 0; i < scene.activeObjects.length; i++)
8 {
9 let castAngle = direction.toAngle();
10
11 if (scene.activeObjects[i] != this && scene.activeObjects[i].collider.enabled)
12 {
13 var hw = scene.activeObjects[i].transform.size.x / 2;
14 var hh = scene.activeObjects[i].transform.size.y / 2;
15
16 const points =
17 [
18 new vector(scene.activeObjects[i].transform.position.x - hw, scene.activeObjects[i].transform.position.y - hh),
19 new vector(scene.activeObjects[i].transform.position.x + hw, scene.activeObjects[i].transform.position.y - hh),
20 new vector(scene.activeObjects[i].transform.position.x - hw, scene.activeObjects[i].transform.position.y + hh),
21 new vector(scene.activeObjects[i].transform.position.x + hw, scene.activeObjects[i].transform.position.y + hh)
22 ];
23
24 points.sort((p1, p2) => p1.subtract(origin).length - p2.subtract(origin).length).pop();
25
26 for (let p = 0; p < points.length; p++)
27 {
28 const point = points[p];
29 const deltaDistance = point.subtract(origin).length;
30
31 if (deltaDistance > maxLength && maxLength > 0)
32 {
33 continue;
34 }
35
36 const stretched = direction.stretch(Math.min(deltaDistance, maxLength > 0 ? maxLength : deltaDistance));
37
38 const calcPoint = origin.add(stretched);
39
40 if (scene.activeObjects[i].collider.isInBounds(calcPoint))
41 {
42 //determine in between which vector angles ray is
43 var angles = points.map(p => Ray.mapPointToPointAndAngle(p, origin));
44
45 pointLoop:
46 for (let pa = 0; pa < angles.length; pa++)
47 {
48 for (let pb = 0; pb < angles.length; pb++)
49 {
50 if (angles[pa].angle - angles[pb].angle > 270 || angles[pb].angle - angles[pa].angle > 270)
51 {
52
53 castAngle = (castAngle + 180) % 360;
54 angles.forEach(pIn => pIn.angle = (pIn.angle + 180) % 360);
55 break pointLoop;
56 }
57 }
58 }
59
60 angles = angles.sort((a, b) => b.angle - a.angle);
61
62 if (angles[1].length > angles[0].length && angles[1].length > angles[2].length)
63 {
64 //only one face can be hit at this time, which is between angles[0] and angles[2]
65 angles.removeAt(1); //remove the center, connecting to the un-hittable face
66 }
67 else
68 {
69 //one of two faces can be hit at this time, either angles[0]-angles[1] or angles[1]-angles[2]
70 (castAngle > angles[1].angle) ? angles.pop() : angles.shift(); //figure out on which side we are, and remove the furthest
71 }
72
73 //calc % of angle in relation to the angles it is between
74 const smallAngle = Math.min(angles[1].angle, angles[0].angle);
75 const percent = (castAngle - smallAngle) / (Math.max(angles[1].angle, angles[0].angle) - smallAngle);
76
77 const hitPoint = angles[1].point.lerpTo(angles[0].point, percent);
78 hits.push({
79 hitObj: scene.activeObjects[i],
80 hitPoint: hitPoint,
81 length: hitPoint.subtract(origin).length
82 });
83 break;
84 }
85 }
86 }
87 }
88
89 if (hits.length === 0)
90 { return undefined; }
91
92 let shortest = hits[0];
93
94 for (let i = 1; i < hits.length; i++)
95 {
96 if (hits[i].length < shortest.length)
97 {
98 shortest = hits[i];
99 }
100 }
101
102 return shortest;
103 }
104
105 static mapPointToPointAndAngle (point, origin)
106 {
107 const delta = point.subtract(origin);
108
109 return {
110 point: point,
111 angle: delta.toAngle(),
112 length: delta.length
113 };
114 }
115}