Release

3D Raytracing Demo

8

was8bit 2020-01-05 06:55

3D code written by Timo

Converted into NX by was8bit

powered by PXL code written by nathanielbabiak


Roger Davis 2020-01-05 09:00

Very well done was8bit! Regardless of the 'flicker', you've ported this over very well! I can see what you meant though on the other post (GAMELEGEND's post) , about it having fairly limited gaming potential! Still, . . A great example of 'pseudo' 3D! : )


G-9 2020-01-05 09:11

WoW !


was8bit 2020-01-05 14:05

Thanky.... and this techically would be real 3D from a math point of view... anything better would require different technology that required 3d goggles...

also the flickering could be minimized but would require rewriting the math... as raytracing math still eludes me I'm not the one who could do it, and it would never be as smooth as it is in original lowres...

And adding things like enemies would slow the frame rate even further...


Roger Davis 2020-01-05 21:02

I understand now - about it being technically speaking 3D from a math perspective. Thanks for explaining that was8bit! : )


was8bit 2020-01-06 05:17

I have seen the virtual reality goggles that have little computer screens inside, they are wicked cool.... also wicked expensive ;'


Roger Davis 2020-01-06 06:59

Was8bit, Too rich for my blood also! Well out of the budget range!! : )


was8bit 2020-01-06 10:19

Me 2


Jonnywhateva 2020-01-06 23:35

This is fantastic! I am definitely going to look through this code to find out how it ticks!


was8bit 2020-01-06 23:44

:)


GAMELEGEND 2020-03-24 07:09

Holy crap dont do what i did and play this game in the dark even with fully reduced white point and night shift and true tone my eyes hurt


was8bit 2020-03-24 07:19

U can edit the colors ;)


nathanielbabiak 2020-04-12 03:53

Wow! I'm not sure how I missed this - this is great! (You may have noticed I've been away for a while.) Multi-pixel graphics in Pxl Library are really slow. I went through the code and even changed two lines, but it didn't help. For speed, you can change both:
(1) CALL PXL_RECT_FILL(0,0,180,126,0 ) to FILL PXL_ADDR, PXL_SIZE
(2) CALL PXL_LINE(X,64-LH/2,X,64+LH/2,1) to CALL PXL_VERT(X,64-LH/2,LH,1)
Another thing to try would require rewriting the engine a bit. Instead of solid white walls, what if only the top edge and bottom edge were drawn? This would replace item (2) above with two separate lines:
(2a) CALL PXL_SET(X,64-LH/2,1)
(2b) CALL PXL_SET(X,64+LH/2,1)
This will eliminate the flicker! But, it looks like the rendering engine is a bit wonky, and took advantage of the solid white walls to hide some weirdness. If you make this change you'll see what I mean.


was8bit 2020-04-12 05:02 (Edited)

I actuall merely combined code from two sources..

REM MAZE 3D
REM BY TIMO KLOSS
REM RAYCASTING DEMO

REM PXL CODE WRITTEN BY NATHANIELBABIAK ;)


And I honestly haven't looked into either codes too deeply...

If you can improve on the merged code you are welcome to post it, just leave Timo's name in for the 3D part (which was written originally for original lowres)...

It would be nice to see a cleaner version :)


was8bit 2020-04-12 05:06

I did tweak Timo's original code, would you like to see the original version?


was8bit 2020-04-12 05:08

REM MAZE 3D PREVIEW 3
REM BY TIMO KLOSS
REM RAYCASTING DEMO


DIM MAP(19,19)
FOR Y=0 TO 19
FOR X=0 TO 19
READ MAP(X,Y)
NEXT X
NEXT Y

DIM DARK(15)
FOR I=0 TO 15
READ DARK(I)
COLOR I
BAR 0,I*4 TO 3,I*4+3
COLOR DARK(I)
BAR 4,I*4 TO 7,I*4+3
NEXT I

GAMEPAD 1

MOVSPEED=0.25
ROTSPEED=PI*0.05
PADDING=0.3

POSX=10
POSY=11
DIRX=0
DIRY=-1
CPLX=0.66
CPLY=0

DO
IF UP(0) THEN
MS=MOVSPEED
GOSUB PLMOVE
END IF
IF DOWN(0) THEN
MS=-MOVSPEED
GOSUB PLMOVE
END IF
IF LEFT(0) THEN
RS=-ROTSPEED
GOSUB PLROTATE
END IF
IF RIGHT(0) THEN
RS=ROTSPEED
GOSUB PLROTATE
END IF

COLOR 11
BAR 0,0 TO 63,31
COLOR 6
BAR 0,32 TO 63,63

FOR X=0 TO 63

CAMX=2*X/64-1
RAYPX=POSX
RAYPY=POSY
RAYDX=DIRX+CPLX*CAMX
RAYDY=DIRY+CPLY*CAMX

MAPX=INT(RAYPX)
MAPY=INT(RAYPY)

IF RAYDX<>0 THEN
DDISX=SQR(1+(RAYDY*RAYDY)/(RAYDX*RAYDX))
ELSE
DDISX=1000
END IF
IF RAYDY<>0 THEN
DDISY=SQR(1+(RAYDX*RAYDX)/(RAYDY*RAYDY))
ELSE
DDISY=1000
END IF

IF RAYDX<0 THEN
STEPX=-1
SDISX=(RAYPX-MAPX)*DDISX
ELSE
STEPX=1
SDISX=(MAPX+1-RAYPX)*DDISX
END IF

IF RAYDY<0 THEN
STEPY=-1
SDISY=(RAYPY-MAPY)*DDISY
ELSE
STEPY=1
SDISY=(MAPY+1-RAYPY)*DDISY
END IF

REPEAT
IF SDISX<SDISY THEN
SDISX=SDISX+DDISX
MAPX=MAPX+STEPX
SIDE=0
ELSE
SDISY=SDISY+DDISY
MAPY=MAPY+STEPY
SIDE=1
END IF
UNTIL MAP(MAPX,MAPY)>0

WDIS=1
IF SIDE=0 THEN
IF RAYDX<>0 THEN WDIS=ABS((MAPX-RAYPX+(1-STEPX)/2)/RAYDX)
ELSE
IF RAYDY<>0 THEN WDIS=ABS((MAPY-RAYPY+(1-STEPY)/2)/RAYDY)
END IF
IF WDIS<0.1 THEN WDIS=0.1

LH=ABS(64/WDIS)
WCOL=MAP(MAPX,MAPY)
IF SIDE=1 THEN WCOL=DARK(WCOL)
COLOR WCOL
LINE X,32-LH/2 TO X,32+LH/2
COLOR DARK(WCOL)
IF X MOD 2=0 THEN
PLOT X,32+LH/8
ELSE
PLOT X,32-LH/16
END IF
PLOT X,32-LH/2
PLOT X,32+LH/2

NEXT X

WAIT 0.04
LOOP

PLMOVE:
MOVDX=DIRX*MS
MOVDY=DIRY*MS
NPOSX=POSX+MOVDX
NPOSY=POSY+MOVDY
PADX=0
PADY=0
IF MOVDX<0 THEN PADX=-PADDING
IF MOVDX>0 THEN PADX=PADDING
IF MOVDY<0 THEN PADY=-PADDING
IF MOVDY>0 THEN PADY=PADDING
PAD2=PADDING+0.01
IF MAP(INT(NPOSX+PADX),INT(NPOSY+PADY))>0 THEN
IF MOVDX<0 THEN
NPOSX2=INT(POSX)+PAD2
ELSE
NPOSX2=INT(POSX+1)-PAD2
END IF
IF MOVDY<0 THEN
NPOSY2=INT(POSY)+PAD2
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0,0,0,6,0,0,0,0,9
DATA 9,0,9,9,9,9,9,9,9,0
DATA 9,9,9,9,9,9,9,9,9,9
DATA 2,0,0,0,0,9,0,0,0,0
DATA 9,0,0,0,0,0,0,0,0,1
DATA 2,0,0,4,0,9,0,9,9,9
DATA 9,9,9,0,4,0,0,0,0,1
DATA 2,0,0,0,0,9,0,9,0,0
DATA 0,0,0,0,4,0,0,12,0,1
DATA 2,0,0,0,0,9,0,0,0,0
DATA 0,0,0,0,4,0,0,0,0,1
DATA 2,2,2,2,2,4,4,4,4,4
DATA 4,4,4,4,4,1,1,1,1,1

REM DARK COLORS
DATA 0,2,3,0,5,0,5,6,7,10,0,12,13,0,15,5


nathanielbabiak 2020-04-12 19:14

Thanks!


was8bit 2020-04-13 04:05

:) hope you can make a better rendition :)


nathanielbabiak 2020-04-13 11:49

Thanks was8bit! :-) Try this one. The issue was that the graphics were not clipping outside the character window. I used the final version of pxl library too - it's a bit cleaner. Oh, and I added a feature - the pause button toggles the wall rendering for additional speed. I'd really like to make the A/B buttons strafe left/right, but that will have to wait until after work today.

3D Demo.nx | Open in app
2020-04-13 11:49

G-9 2020-04-13 12:22

Good !


was8bit 2020-04-13 14:52

Thats super good... you need to post that when you are ready :)


nathanielbabiak 2020-04-13 16:49

Thanks! Before I post it, I'm going to make a few more tweaks...

(1) Strafing left/right with A/B

(2) From what I can tell from Timo's code, each square/panel was a different color. And the floor and ceiling had different colors too. Obviously, in monochrome none of that's possible. What I can do is draw a vertical line between each square/panel if the projected ray is near a square/panel edge. With long hallways, this is merely a graphics update, but at doorways it'll really help show the door outline.

(3) Optimize for flicker. Rewrite some repeated calculations so they only happen once. Minimize the number of pixels turned on each update (this would effect the wall graphics). Clear the screen one column at a time during the update (rather than clear the whole screen before the update).

If the optimizations work, there's actually a surprise I can code into the pause button - no more monochrome!


GAMELEGEND 2020-04-13 17:25

Is there a end to the maze


was8bit 2020-04-14 00:18

Cool :)


nathanielbabiak 2020-04-14 01:29

There's no end to the maze, not yet at least ;-)


nathanielbabiak 2020-04-26 16:15 (Edited)

@was8bit - you should check out my latest upload - I'm very proud of it! I actually found some resources online that explained Timo's code pretty well. I rewrote the whole thing from scratch too. https://lowresnx.inutilis.com/topic.php?id=1161


Log in to reply.