Results 1 to 3 of 3

Thread: 2D raycasting demo

  1. #1
    Member
    Join Date
    Mar 2007
    Location
    Finland
    Age
    50
    Posts
    76
    Rep Power
    26

    Cool 2D raycasting demo

    ' ============================================
    ' 2D Field-of-View (FOV) — 90-Ray Casting
    ' BazzBasic version: https://ekbass.github.io/BazzBasic/
    ' Reads walls from map array, SCREENLOCK for
    ' flicker-free rendering
    ' ============================================
    
    
    LET NUM_RAYS# = 90
    LET STEP_SIZE# = 0.2
    LET MAX_STEPS# = 40
    LET PI2# = 6.28318530
    
    
    ' Map definition
    DIM m$
    GOSUB [sub:readMap]
    
    
    LET MAP_H# = 21
    LET MAP_W# = 31
    LET px$ = 3
    LET py$ = 3
    
    
    DIM vis$
    CLS
    
    
    ' === Main Loop ===
    LET running$ = TRUE
    WHILE running$
    
    
        ' 1) Reset visibility
        FOR vy$ = 0 TO MAP_H# - 1
            FOR vx$ = 0 TO MAP_W# - 1
                vis$(vy$, vx$) = 0
            NEXT
        NEXT
        vis$(py$, px$) = 1
    
    
        ' 2) Cast rays — read walls from array, no screen needed
        GOSUB [sub:CastAllRays]
    
    
        ' 3) Draw everything in one go
        SCREENLOCK ON
        GOSUB [sub:DrawFOV]
    
    
        ' 4) Player
        COLOR 10, 0
        LOCATE py$ + 1, px$ + 1
        PRINT "@";
    
    
        ' 5) Status bar
        COLOR 8, 0
        LOCATE MAP_H# + 2, 1
        PRINT "Arrows=Move  ESC=Quit  Pos:"; px$; ","; py$; "   ";
        SCREENLOCK OFF
    
    
        ' 6) Wait for a keypress
        LET key$ = 0
        WHILE key$ = 0
            key$ = INKEY
            SLEEP 16
        WEND
    
    
        ' 7) Movement + collision
        LET nx$ = px$
        LET ny$ = py$
        IF key$ = KEY_UP# THEN ny$ = py$ - 1
        IF key$ = KEY_DOWN# THEN ny$ = py$ + 1
        IF key$ = KEY_LEFT# THEN nx$ = px$ - 1
        IF key$ = KEY_RIGHT# THEN nx$ = px$ + 1
        IF key$ = KEY_ESC# THEN running$ = FALSE
    
    
        IF MID(m$(ny$), nx$ + 1, 1) = "." THEN
            px$ = nx$
            py$ = ny$
        ENDIF
    
    
    WEND
    
    
    COLOR 7, 0
    CLS
    END
    
    
    ' -----------------------------------------------
    ' Cast rays — check walls from map array directly
    ' -----------------------------------------------
    [sub:CastAllRays]
        FOR ray$ = 0 TO NUM_RAYS# - 1
            LET angle$ = (ray$ / NUM_RAYS#) * PI2#
            LET dx$ = COS(angle$) * STEP_SIZE#
            LET dy$ = SIN(angle$) * STEP_SIZE#
            LET rx$ = px$ + 0.5
            LET ry$ = py$ + 0.5
            LET hit$ = 0
    
    
            FOR s$ = 1 TO MAX_STEPS#
                IF hit$ = 0 THEN
                    rx$ = rx$ + dx$
                    ry$ = ry$ + dy$
                    LET cx$ = INT(rx$)
                    LET cy$ = INT(ry$)
    
    
                    IF cx$ >= 0 AND cx$ < MAP_W# AND cy$ >= 0 AND cy$ < MAP_H# THEN
                        vis$(cy$, cx$) = 1
                        IF MID(m$(cy$), cx$ + 1, 1) = "#" THEN hit$ = 1
                    ELSE
                        hit$ = 1
                    ENDIF
                ENDIF
            NEXT
        NEXT
    RETURN
    
    
    ' -----------------------------------------------
    ' Draw map: visible=lit, not visible=black
    ' -----------------------------------------------
    [sub:DrawFOV]
        FOR dy$ = 0 TO MAP_H# - 1
            FOR dx$ = 0 TO MAP_W# - 1
                LOCATE dy$ + 1, dx$ + 1
                LET ch$ = MID(m$(dy$), dx$ + 1, 1)
                IF vis$(dy$, dx$) = 1 THEN
                    IF ch$ = "#" THEN
                        COLOR 7, 0
                    ELSE
                        COLOR 14, 0
                    ENDIF
                ELSE
                    COLOR 0, 0
                ENDIF
                PRINT ch$;
            NEXT
        NEXT
    RETURN
    
    
    ' -----------------------------------------------
    ' Read map into array
    ' -----------------------------------------------
    [sub:readMap]
    	m$(0) 	= "###############################"
    	m$(1) 	= "#...........#..#........#.....#"
    	m$(2) 	= "####..#..####..#######..#..#..#"
    	m$(3) 	= "#.....#........#...........#..#"
    	m$(4) 	= "#######..#..#..#..####..#######"
    	m$(5) 	= "#........#..#.....#..#........#"
    	m$(6) 	= "#######..#..####..#..####..####"
    	m$(7) 	= "#.....#..#..#.....#.....#.....#"
    	m$(8) 	= "####..##########..#..#######..#"
    	m$(9) 	= "#...........#.....#..#..#..#..#"
    	m$(10)	= "####..####..#..####..#..#..#..#"
    	m$(11)	= "#.....#.....#..............#..#"
    	m$(12) 	= "#..#######..#..#..##########..#"
    	m$(13) 	= "#.....#.....#..#........#..#..#"
    	m$(14) 	= "#######..##########..####..#..#"
    	m$(15) 	= "#...........#.....#..#..#.....#"
    	m$(16) 	= "#..##########..#######..####..#"
    	m$(17) 	= "#..............#..............#"
    	m$(18) 	= "##########..#..####..####..#..#"
    	m$(19) 	= "#...........#...........#..#..#"
    	m$(20) 	= "###############################"
    RETURN
    
    Last edited by E.K.Virtanen; 06-02-2026 at 20:01.
    Author of https://ekbass.github.io/BazzBasic/ and avid bass player

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    58
    Posts
    8,890
    Rep Power
    10
    I will try when back home.

    BazzBasic is becoming my preferred study because I've a crazy idea to move to .Net thinBasic and I was looking for an inspiration.
    And you arrived with your great project!
    Maybe its is a sign of the digital universe
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Member
    Join Date
    Mar 2007
    Location
    Finland
    Age
    50
    Posts
    76
    Rep Power
    26
    I am indeed impressed how fast NET10 actually is.

    But on the other hand, I have a really high-end desktop at home so I'll know how smooth this really runs when I get to try it out on my work laptop next week.


    It's already a bit old and slow.
    Author of https://ekbass.github.io/BazzBasic/ and avid bass player

Similar Threads

  1. Stick Demo
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 0
    Last Post: 14-05-2013, 13:30
  2. Sdl Demo
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 6
    Last Post: 13-05-2013, 16:54
  3. Fern Demo
    By peter in forum TBGL module by Petr Schreiber
    Replies: 4
    Last Post: 08-10-2012, 17:41
  4. Art Thankies Demo 1.1a
    By Lionheart008 in forum TBGL Scripts and Projects
    Replies: 7
    Last Post: 23-04-2009, 18:54
  5. Demo
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 10
    Last Post: 12-03-2009, 15:39

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •