summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/assets/test/intersect3.html99
1 files changed, 72 insertions, 27 deletions
diff --git a/public/assets/test/intersect3.html b/public/assets/test/intersect3.html
index 1e2a1a4..b916f5f 100644
--- a/public/assets/test/intersect3.html
+++ b/public/assets/test/intersect3.html
@@ -67,7 +67,7 @@ Rooms.clipper.update()
Rooms.builder.build()
Rooms.grouper.build()
-var r = 4
+var r = 6
var intersect = new vec2 ()
@@ -115,27 +115,39 @@ function draw () {
drawLine(cursor.x, cursor.y, "#f00")
hud.innerHTML = ""
- var closest_face, closest_intersect, t, min_t = 1
-
+ var closest_intersect, cursor_copy = new Rect(0,0,0,0), t, min_t = 1
+ var cursor_copy = cursor.plus(scene.camera.radius)
+
Walls.list.forEach(function(wall, i){
- if (wall.side & LEFT_RIGHT) {
- wall_vec.x.a = wall.edge
- wall_vec.x.b = wall.vec.a
- wall_vec.y.a = wall.edge
- wall_vec.y.b = wall.vec.b
- }
- else {
+ if (wall.side & LEFT) {
+ wall_vec.x.a = wall.edge
+ wall_vec.x.b = wall.vec.b
+ wall_vec.y.a = wall.edge
+ wall_vec.y.b = wall.vec.a
+ }
+ else if (wall.side & RIGHT) {
+ wall_vec.x.a = wall.edge
+ wall_vec.x.b = wall.vec.a
+ wall_vec.y.a = wall.edge
+ wall_vec.y.b = wall.vec.b
+ }
+ else if (wall.side & FRONT) {
wall_vec.x.a = wall.vec.a
wall_vec.x.b = wall.edge
wall_vec.y.a = wall.vec.b
wall_vec.y.b = wall.edge
}
- drawLine(wall_vec.x, wall_vec.y, "#888", true)
- drawLine(origins.x, origins.y, "rgba(0,0,0,0.1)")
+ else if (wall.side & BACK) {
+ wall_vec.x.a = wall.vec.b
+ wall_vec.x.b = wall.edge
+ wall_vec.y.a = wall.vec.a
+ wall_vec.y.b = wall.edge
+ }
+ drawLine(wall_vec.x, wall_vec.y, "#088")
- t = perp(origins, wall_vec) / ( perp(cursor, wall_vec) || 0.0000001 )
- intersect.a = cursor.x.a + ( cursor.y.a - cursor.x.a ) * t
- intersect.b = cursor.x.b + ( cursor.y.b - cursor.x.b ) * t
+ t = perp(origins, wall_vec) / ( perp(cursor_copy, wall_vec) || 0.0000001 )
+ intersect.a = cursor_copy.x.a + ( cursor_copy.y.a - cursor_copy.x.a ) * t
+ intersect.b = cursor_copy.x.b + ( cursor_copy.y.b - cursor_copy.x.b ) * t
var collinear = is_collinear( intersect, wall_vec )
var long_enough_to_intersect = 0 <= t && t <= 1
@@ -169,22 +181,42 @@ function draw () {
drawPoint(intersect)
if (actually_intersects && t < min_t) {
min_t = t
- hud.innerHTML += t + "<br>"
- var clone = intersect.clone()
- clone.t = t
- closest_intersect = intersecting_face
- closest_intersect = clone
+ closest_intersect = intersect.clone()
+ closest_wall = wall_vec.clone()
}
})
- if (closest_face) {
- var a = angle(cursor)
- closest_intersect.a -= scene.camera.radius * cos(a)
- closest_intersect.b -= scene.camera.radius * sin(a)
+ if (closest_intersect) {
+ var a = angle(closest_wall)
+ var ac = angle(cursor_copy)
+ wall_vec.assign(closest_wall)
+ wall_vec.x.a -= scene.camera.radius * sin(a)
+ wall_vec.x.b += scene.camera.radius * cos(a)
+ wall_vec.y.a -= scene.camera.radius * sin(a)
+ wall_vec.y.b += scene.camera.radius * cos(a)
- ctx.fillStyle = "#00f"
- drawPoint(closest_intersect)
- drawLine(cursor.x, closest_intersect, "#00f")
+ drawLine(wall_vec.x, wall_vec.y, "rgba(0,255,255,1.0)")
+
+ var new_t = perp(origins, wall_vec) / ( perp(cursor_copy, wall_vec) || 0.0000001 )
+ var closest_intersect2 = new vec2()
+ closest_intersect2.a = cursor_copy.x.a + ( cursor_copy.y.a - cursor_copy.x.a ) * new_t
+ closest_intersect2.b = cursor_copy.x.b + ( cursor_copy.y.b - cursor_copy.x.b ) * new_t
+
+ ctx.fillStyle = "#0ff"
+ drawPoint(closest_intersect2)
+ drawLine(cursor.x, closest_intersect2, "#00f")
+
+ var len = (1-min_t) * sqrt(dot(cursor, cursor))
+ if (max(a,ac)-min(a,ac) > PI/2) {
+ a += PI
+ }
+ var end_of_ray = closest_intersect2.clone()
+
+ end_of_ray.a += len * cos(a)
+ end_of_ray.b += len * sin(a)
+
+ drawPoint(end_of_ray)
+ drawLine(closest_intersect2, end_of_ray, "#00f")
}
}
function drawLine (pa, pb, color, headless) {
@@ -204,12 +236,18 @@ function drawPoint (p) {
function angle (va) {
return atan2(va.y.b - va.x.b, va.y.a - va.x.a)
}
+function angle2 (pa, pb) {
+ return atan2(pb.b - pa.b, pb.a - pa.a)
+}
function dot (va, vb) {
return (va.y.a - va.x.a) * (vb.y.a - vb.x.a) + (va.y.b - va.x.b) * (vb.y.b - vb.x.b)
}
function perp (va, vb) {
return (va.y.a - va.x.a) * (vb.y.b - vb.x.b) - (va.y.b - va.x.b) * (vb.y.a - vb.x.a)
}
+function norm(va) {
+ return c
+}
function is_collinear (p, vec) {
var on_x, on_y
var pa = round(p.a), pb = round(p.b)
@@ -230,6 +268,13 @@ function is_collinear (p, vec) {
return !! (on_x && on_y)
}
+cursor.plus = function(n){
+ var a = angle(this)
+ var clone = this.clone()
+ clone.y.a += n*cos(a)
+ clone.y.b += n*sin(a)
+ return clone
+}
function animate(){
requestAnimationFrame(animate)