Commit ad4b312b authored by Hugo Hörnquist's avatar Hugo Hörnquist

I know trigonometry.

parent a0524a09
......@@ -39,7 +39,7 @@
(define texture-map (make-hash-table))
(define ray-count 2)
(define ray-count 64)
(define rays (make-atomic-box '()))
......@@ -145,6 +145,36 @@
(define distance-from-y-wall x-from-edge)
(define (move-horizontally-from-middle x y a callback)
(let* ((dx (* 1.01 (sgn (cos a))
(mod (* x -1 (sgn (cos a)))
1)))
(pdy (* dx (tan a))))
(if (= (truncate y) (truncate (+ y pdy)))
(callback (+ x dx)
(+ y pdy))
(let ((dy (* 1.01 (sgn (sin a))
(mod (* y -1 (sgn (sin a)))
1))))
(callback (+ x (* dy (cot a)))
(+ y dy))))))
(define (move-vertically-from-middle x y a callback)
(let* ((dy (* 1.01 (sgn (sin a))
(mod (* y -1 (sgn (sin a)))
1)))
(pdx (* dy (cot a))))
(if (= (truncate x) (truncate (+ x pdx)))
(callback (+ x pdx)
(+ y dy))
(let ((dx (* 1.01 (sgn (cos a))
(mod (* x -1 (sgn (cos a)))
1))))
(callback (+ x (* dx (tan a)))
(+ y dy))))))
;; (loop (+ x (* 0.01 (cos a)))
;; (+ y (* 0.01 (sin a))))
......@@ -180,11 +210,17 @@
;; ================= follow ray ===============================
;; (hit-x?)
#;
[(distance-from-x-wall y a)
(lambda (d) (let ((dyw (distance-from-y-wall x a)))
(and (< dyw 0.01)
(< dyw d))))
=> (lambda (d)
(make-ray #:a a #:type 'wall
#:v (v3 (- x px) (- y py))
#:hitf (v3 x y))
#;
(let ((possible-dy (* (sgn (cos a))
(tan a))))
(if (> 1 (abs (+ (* d (sgn (cos a)))
......@@ -199,12 +235,13 @@
(+ y dy))))))]
;; hit wall from top or bottom (hit-y?)
#;
[(distance-from-y-wall x a)
(lambda (d) (let ((dxw (distance-from-x-wall y a)))
(and (< dxw 0.01)
(< dxw d))))
=> (lambda (d)
(make-ray #:a a #:type 'wall
#:v (v3 (- x px) (- y py))
#:hitf (v3 x y))
......@@ -261,42 +298,17 @@
;; ===== Middle of square =====
;; moving horizontally
[(> (abs (cos a))
(abs (sin a)))
(let* ((dx (* 1.01 (sgn (cos a))
(mod (* x -1 (sgn (cos a)))
1)))
(pdy (* dx (tan a))))
(if (= (truncate y) (truncate (+ y pdy)))
(loop (+ x dx)
(+ y pdy))
(let ((dy (* 1.01 (sgn (sin a))
(mod (* y -1 (sgn (sin a)))
1))))
(loop (+ x (* dy (cot a)))
(+ y dy)))))]
;; moving vertically
[else
(let* ((dy (* 1.01 (sgn (sin a))
(mod (* y -1 (sgn (sin a)))
1)))
(pdx (* dy (cot a))))
(if (= (truncate x) (truncate (+ x pdx)))
(loop (+ x pdx)
(+ y dy))
(let ((dx (* 1.01 (sgn (cos a))
(mod (* x -1 (sgn (cos a)))
1))))
(loop (+ x (* dx (tan a)))
(+ y dy))
)
[else (let ((dx-ampl (mod (* -1 (sgn (cos a)) (decimals x)) 1))
(dy-ampl (mod (* -1 (sgn (sin a)) (decimals y)) 1)))
(let* ((dx (* 1.01 dx-ampl (sgn (cos a))))
(dy (* dx (tan a))))
(if (<= (abs dy) dy-ampl)
(loop (+ x dx) (+ y dy))
(let* ((dy (* 1.01 dy-ampl (sgn (sin a))))
(dx (* dy (cot a))))
(loop (+ x dx) (+ y dy))))))])))
))])))
(iota ray-count (- (a player) (/ (fov player) 2))
(/ (fov player) ray-count)))))
)
......@@ -423,7 +435,7 @@
(parameterize ((color (make-color 0 0 0 #xFF)))
;; FPS counter
(render-text (format #f "FPS: ~,2f" (/ 1000 (fps-counter))) #:line 0)
(render-text (format #f "FPS: ~,2f" (/ 1000 (1+ (fps-counter)))) #:line 0)
(render-text
(format #f "x = ~,4f y = ~,4f a = ~,4f"
......
(load "main.scm")
;; (define x 2.1574)
;; (define y 2.0389)
;; (define fov (/ tau 8))
#|
dx
+---/
dy | /
| /
|/
== Om världen var enkel ==
dx = cos a
dy = sin a
|#
(define (decimals x)
(- x (truncate x)))
(define (cot a)
(/ (tan a)))
(define sgn signum)
(define (find-edge x y a)
(let ((dx-ampl (mod (* -1 (sgn (cos a)) (decimals x)) 1))
(dy-ampl (mod (* -1 (sgn (sin a)) (decimals y)) 1)))
(let* ((dx (* dx-ampl (sgn (cos a))))
(dy (* dx (tan a))))
(if (<= (abs dy) dy-ampl)
(format
#t "Try 1: a = ~,2f (~,1f, ~,1f) => (~,1f, ~,1f), + Δ(~,1f, ~,1f)~%"
(/ a tau) x y (+ x dx) (+ y dy) dx dy)
(let* ((dy (* dy-ampl (sgn (sin a))))
(dx (* dy (cot a))))
(format
#t
"Try 2: a = ~,2f (~,1f, ~,1f) => (~,1f, ~,1f), + Δ(~,1f, ~,1f)~%"
(/ a tau) x y (+ x dx) (+ y dy) dx dy))))))
(for-each
(lambda (x y)
(let ((ray-count 8))
(for-each
(lambda (theta)
(let ((a (* tau theta)))
(find-edge x y a)
#;
((if (> (abs (cos a))
(abs (sin a)))
move-horizontally-from-middle
move-vertically-from-middle)
x y a (lambda (nx ny)
(format #t "~a s = (~,2f, ~,2f), e = (~,2f, ~,2f), a = τ ~a~%"
(* theta 8) x y nx ny theta)))))
(iota ray-count 0 1/8)))
(newline))
'(1.1 1.1 1.9 1.9)
'(1.1 1.9 1.1 1.9))
#;
(move-vertically-from-middle 1.1 1.1 (* tau 3/8)
(lambda (x y) (format #t "(~,2f, ~,2f)~%"
x y)))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment