| 1 | class Ray |
| 2 | { |
| 3 | static Cast(scene, origin, direction, maxLength = -1) |
| 4 | { |
| 5 | let hits = [] // {hitObj, hitPoint} |
| 6 | for(var i = 0; i < scene.activeObjects.length; i++) |
| 7 | { |
| 8 | if(scene.activeObjects[i] != this && scene.activeObjects[i].collider.enabled) |
| 9 | { |
| 10 | var hw = scene.activeObjects[i].transform.size.x / 2; |
| 11 | var hh = scene.activeObjects[i].transform.size.y / 2; |
| 12 | |
| 13 | let points = |
| 14 | [ |
| 15 | new vector(scene.activeObjects[i].transform.position.x - hw, scene.activeObjects[i].transform.position.y - hh), |
| 16 | new vector(scene.activeObjects[i].transform.position.x + hw, scene.activeObjects[i].transform.position.y - hh), |
| 17 | new vector(scene.activeObjects[i].transform.position.x - hw, scene.activeObjects[i].transform.position.y + hh), |
| 18 | new vector(scene.activeObjects[i].transform.position.x + hw, scene.activeObjects[i].transform.position.y + hh) |
| 19 | ] |
| 20 | |
| 21 | for(let p = 0; p < points.length; p++) |
| 22 | { |
| 23 | let point = points[p]; |
| 24 | let deltaDistance = point.subtract(origin).length; |
| 25 | direction = direction.normalize(); |
| 26 | |
| 27 | let stretched = direction.stretch(Math.min(deltaDistance, maxLength > 0? maxLength : deltaDistance)) |
| 28 | |
| 29 | let calcPoint = origin.add(stretched); |
| 30 | |
| 31 | if(scene.activeObjects[i].collider.isInBounds(calcPoint)) |
| 32 | { |
| 33 | hits.push({hitObj: scene.activeObjects[i], hitPoint: calcPoint}) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return hits; |
| 40 | } |
| 41 | } |