BlueGit

timfa frameworkv3

fix rays

timfa Tim · pushed by timfa

Authored 2022-10-18 · 4 years ago

Hash
420b76408e73c15a9fc306e1c156ae87ecaccdb8
Parent
2bbed98f
2 files changed +36 −10
framework/util/ray.js +32 −9
@@ -35,29 +35,52 @@ class Ray
35 35 {
36 36 //determine in between which vector angles ray is
37 37 //todo: toAngle offset if required
38 var angles = points.map(p => Ray.mapPointToPointAndAngle(p, origin)).sort();
39 castAngle < angles[1].angle ? angles.pop() : angles.shift();
40
41 //calc % of angle in relation to the angles it is between
42 let percent = castAngle / (angles[1].angle - angles[0].angle);
38 var angles = points.map(p => Ray.mapPointToPointAndAngle(p, origin))
39 angles = angles.sort((a,b) => b.angle - a.angle);
40
41 if(angles[1].length > angles[0].length && angles[1].length > angles[2].length)
42 {
43 //only one face can be hit at this time, which is between angles[0] and angles[2]
44 angles.removeAt(1); //remove the center, connecting to the un-hittable face
45 }
46 else
47 {
48 //one of two faces can be hit at this time, either angles[0]-angles[1] or angles[1]-angles[2]
49 (castAngle > angles[1].angle) ? angles.pop() : angles.shift(); //figure out on which side we are, and remove the furthest
50 }
43 51
44 hits.push({hitObj: scene.activeObjects[i], hitPoint: angles[0].point.lerpTo(angles[1].point, percent)})
52 //calc % of angle in relation to the angles it is between
53 let smallAngle = Math.min(angles[1].angle, angles[0].angle);
54 let percent = (castAngle - smallAngle) / (Math.max(angles[1].angle, angles[0].angle) - smallAngle);
45 55
56 hits.push({hitObj: scene.activeObjects[i], hitPoint: angles[1].point.lerpTo(angles[0].point, percent)})
46 57 break;
47 58 }
48 59 }
49 60 }
50 61 }
51 62
52 //todo: find shortest distance
53 return hits;
63 let shortest = hits[0];
64
65 for(let i = 0; i < hits.length; i++)
66 {
67 if(hits[i].length < shortest.length)
68 {
69 shortest = hits[i];
70 }
71 }
72
73 return shortest;
54 74 }
55 75
56 76 static mapPointToPointAndAngle(point, origin)
57 77 {
78 let delta = point.subtract(origin);
79
58 80 return {
59 81 point: point,
60 angle: point.subtract(origin).toAngle()
82 angle: delta.toAngle(),
83 length: delta.length
61 84 };
62 85 }
63 86 }
@@ -35,29 +35,52 @@ class Ray
35 {
35 {
36 //determine in between which vector angles ray is
36 //determine in between which vector angles ray is
37 //todo: toAngle offset if required
37 //todo: toAngle offset if required
38 var angles = points.map(p => Ray.mapPointToPointAndAngle(p, origin)).sort();
38 var angles = points.map(p => Ray.mapPointToPointAndAngle(p, origin))
39 castAngle < angles[1].angle ? angles.pop() : angles.shift();
39 angles = angles.sort((a,b) => b.angle - a.angle);
40
40
41 //calc % of angle in relation to the angles it is between
41 if(angles[1].length > angles[0].length && angles[1].length > angles[2].length)
42 let percent = castAngle / (angles[1].angle - angles[0].angle);
42 {
43 //only one face can be hit at this time, which is between angles[0] and angles[2]
44 angles.removeAt(1); //remove the center, connecting to the un-hittable face
45 }
46 else
47 {
48 //one of two faces can be hit at this time, either angles[0]-angles[1] or angles[1]-angles[2]
49 (castAngle > angles[1].angle) ? angles.pop() : angles.shift(); //figure out on which side we are, and remove the furthest
50 }
43
51
44 hits.push({hitObj: scene.activeObjects[i], hitPoint: angles[0].point.lerpTo(angles[1].point, percent)})
52 //calc % of angle in relation to the angles it is between
53 let smallAngle = Math.min(angles[1].angle, angles[0].angle);
54 let percent = (castAngle - smallAngle) / (Math.max(angles[1].angle, angles[0].angle) - smallAngle);
45
55
56 hits.push({hitObj: scene.activeObjects[i], hitPoint: angles[1].point.lerpTo(angles[0].point, percent)})
46 break;
57 break;
47 }
58 }
48 }
59 }
49 }
60 }
50 }
61 }
51
62
52 //todo: find shortest distance
63 let shortest = hits[0];
53 return hits;
64
65 for(let i = 0; i < hits.length; i++)
66 {
67 if(hits[i].length < shortest.length)
68 {
69 shortest = hits[i];
70 }
71 }
72
73 return shortest;
54 }
74 }
55
75
56 static mapPointToPointAndAngle(point, origin)
76 static mapPointToPointAndAngle(point, origin)
57 {
77 {
78 let delta = point.subtract(origin);
79
58 return {
80 return {
59 point: point,
81 point: point,
60 angle: point.subtract(origin).toAngle()
82 angle: delta.toAngle(),
83 length: delta.length
61 };
84 };
62 }
85 }
63}
86}
framework/util/vector.js +4 −1
@@ -23,7 +23,10 @@ class vector
23 23
24 24 toAngle()
25 25 {
26 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
26 let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
27 if(angle < 0)
28 angle += 360;
29 return angle;
27 30 }
28 31
29 32
@@ -23,7 +23,10 @@ class vector
23
23
24 toAngle()
24 toAngle()
25 {
25 {
26 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
26 let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
27 if(angle < 0)
28 angle += 360;
29 return angle;
27 }
30 }
28
31
29
32