Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:21 02 Sep 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Raycaster for PicoVGA

Author Message
gadgetjack
Senior Member

Joined: 15/07/2016
Location: United States
Posts: 170
Posted: 02:54am 27 Jan 2023
Copy link to clipboard 
Print this post

I re-wrote an old raycaster I did for QB64 forum. Runs ok, little slow. Could use some optimizing....
DIM SinTable(1795)
DIM CosTable(1799)
PI = 22 / 7
DIM Mapp(10, 10)
FOR z= 0 TO 1795
SinTable(z) = SIN(z * PI / 900) * 100
CosTable(z) = COS(z * PI / 900) * 100
NEXT z
FOR y = 1 TO 10
FOR x = 1 TO 10
READ Mapp(x, y)
IF Mapp(x, y) = 2 THEN
Mapp(x, y) = 0
px = x: py = y
END IF
NEXT x
NEXT y
px = px * 1000
py = py * 1000
mode 2
mangle = 0
SkyColor = rgb(0,0,255)
FloorColor = rgb(255,0,0)
DO
Raycast
DO
kb$ = INKEY$
loop while kb$=("")
IF kb$ = "w" THEN px = px - SinTable(mangle) * 5: py = py - CosTable(mangle) * 5
IF kb$ = "s" THEN px = px + SinTable(mangle) * 5: py = py + CosTable(mangle) * 5
IF kb$ = "d" THEN mangle = mangle + 25
IF kb$ = "a" THEN mangle = mangle - 25
IF mangle < 0 THEN mangle = mangle + 1800
IF mangle > 1795 THEN mangle = mangle - 1800
LOOP
Sub Raycast
s = -1
FOR z = -160 TO 160
s = s + 1
a = mangle + z
IF a < 0 THEN a = a + 1795
IF a > 1795 THEN a = a - 1795 '
xx = px: yy = py
dd = 0
DO
xx = xx - SinTable(a)
yy = yy - CosTable(a)
m = Mapp(xx / 1000, yy / 1000)
dd = dd + 1
LOOP UNTIL m
ds = 1000 / dd
cr=rnd()*255:cg=rnd()*255:cb=rnd()*255
LINE s, 0,s, 99 - ds,, SkyColor
LINE s, 100 - ds,s, 100 + ds,,rgb(cr,cg,cb)'walls
LINE s, 101 + ds,s, 199,, FloorColor
NEXT z
End Sub
DATA 1,1,1,1,1,1,1,1,1,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,1,0,1,1,1,0,1
DATA 1,1,1,1,0,0,0,1,0,1
DATA 1,0,0,0,0,0,0,1,0,1
DATA 1,0,0,0,0,2,0,0,0,1
DATA 1,1,1,1,0,1,0,0,0,1
DATA 1,0,0,0,0,1,0,1,0,1
DATA 1,0,1,1,1,1,0,0,0,1
DATA 1,1,1,1,1,1,1,1,1,1
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1270
Posted: 09:40am 27 Jan 2023
Copy link to clipboard 
Print this post

  gadgetjack said  I re-wrote an old raycaster I did for QB64 forum. Runs ok, little slow. Could use some optimizing....

Nice Job, your Sub Raycast looks much simpler than mine...
at first view, i found 2 parts to speed things up.
first:
replace
If a < 0 Then a = a + 1795
If a > 1795 Then a = a - 1795

with
a=a+1796*(a < 0)-1795*(a>1795)

this is more performant

scan only every 2nd Ray reduces the nuber of scans from 320 to 160
so in the Sub Raycast

For z = -160 To 160 Step 2
inc s,2

and replace the Line routines with

Box s, 0,2, 99 - ds,, SkyColor
Box s, 100 - ds,2,(101 + ds)- (100 - ds),,RGB(cr,cg,cb)'walls
Box s, 101 + ds,2, 199-(101 + ds),, FloorColor
--
Maybe you use the Frame Buffer to draw the new screen in the Background
Set one Block of Skycolor abd one of Floorcolor befor you start casting
then you get rid of the Floor and Sky Color commands in the Loop  

take a look at my try (maybe we both can improve)
Raycast / Pico
Edited 2023-01-27 20:22 by Martin H.
'no comment
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1270
Posted: 01:34pm 27 Jan 2023
Copy link to clipboard 
Print this post

so here is my suggestion for optimization.
first set
OPTION CPUSPEED  378000




FRAMEBUFFER create
FRAMEBUFFER write f
Dim SinTable(1795)
Dim CosTable(1795)
'PI = 22 / 7
Dim Mapp(10, 10)
For z= 0 To 1795
 SinTable(z) = Sin(z * Pi / 900) * 100
 CosTable(z) = Cos(z * Pi / 900) * 100
Next z
For y = 1 To 10
 For x = 1 To 10
   Read Mapp(x, y)
   If Mapp(x, y) = 2 Then
     Mapp(x, y) = 0
     px = x: py = y
   EndIf
 Next x
Next y
px = px * 1000
py = py * 1000
MODE 2
mangle = 0
SkyColor = RGB(0,0,255)
FloorColor = RGB(255,0,0)
Do
 Box 0,0,320,100,,SkyColor,SkyColor
 Box 0,100,320,100,,FloorColor,FloorColor
 Raycast
 Box 0,200,320,40,,0,0
 FRAMEBUFFER copy f,n
 Do
   kb$ = Inkey$
 Loop While kb$=("")
 Select Case kb$
   Case = "w"
     px = px - SinTable(mangle) * 5: py = py - CosTable(mangle) * 5
   Case "s"
     px = px + SinTable(mangle) * 5: py = py + CosTable(mangle) * 5
   Case "d"
     mangle = mangle + 25
   Case "a"
     mangle = mangle - 25
  End Select
   mangle=mangle+1800*(mangle < 0)-1800*(mangle > 1795)
Loop
Sub Raycast
 s = -2
 For z = -160 To 160 Step 2
   Inc s,2
   a = mangle + z
   a=a+1796*(a < 0)-1795*(a>1795)
   xx = px: yy = py
   dd = 0
   Do
     Inc  xx,-SinTable(a):Inc  yy,-CosTable(a)
     m = Mapp(xx / 1000, yy / 1000)
     Inc dd
   Loop Until m
   ds = 1000 / dd
   cr=Rnd()*255:cg=Rnd()*255:cb=Rnd()*255
   Box s, 100 - ds,2,(101 + ds)- (100 - ds),,RGB(cr,cg,cb),RGB(cr,cg,cb)'walls
 Next z
End Sub
Data 1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,0,1,1,1,0,1
Data 1,1,1,1,0,0,0,1,0,1
Data 1,0,0,0,0,0,0,1,0,1
Data 1,0,0,0,0,2,0,0,0,1
Data 1,1,1,1,0,1,0,0,0,1
Data 1,0,0,0,0,1,0,1,0,1
Data 1,0,1,1,1,1,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1                                                        


with tracing you go a different way than I had in my Program.
You look after each step whether the cell is set. That works, but if you're unlucky you'll query the same cell 1000 times.
Cheers
Mart!n
Edited 2023-01-27 23:50 by Martin H.
'no comment
 
gadgetjack
Senior Member

Joined: 15/07/2016
Location: United States
Posts: 170
Posted: 04:39pm 27 Jan 2023
Copy link to clipboard 
Print this post

Martin , thanks for you input. I knew that smarter people than me would get this running better. I tip my hat to you sir.
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1270
Posted: 04:55pm 27 Jan 2023
Copy link to clipboard 
Print this post

I had understood your sentence
  gadgetjack said   Could use some optimizing....

as an invitation to take a look at it and see where you can optimize it
and because I just had the time, I tried out what I could improve.
That doesn't mean that I feel smarter than you. The community lives from the exchange of experiences, so don't take my posts as criticism.
Cheers
Mart!n
'no comment
 
gadgetjack
Senior Member

Joined: 15/07/2016
Location: United States
Posts: 170
Posted: 03:33pm 28 Jan 2023
Copy link to clipboard 
Print this post

Martin , I thank you for your input. Sometimes we just have to look at it from another angle , and folks here are great at that. One of the reasons I hang around on this site.
Jack
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025