I need help with a raycaster very simple issue

Started by LemonWizard, September 23, 2013, 09:38:33 PM

Previous topic - Next topic

LemonWizard

Hey guys. I'm working on this port of something someone made. I can post the original code.
For some reason this isn't working at all... I don't know what I'm doing wrong but I don't understand the math they used to start with. Here's the code.
It only sort of works. I can't actually turn around or walk in 3D etc. I'll post the original code to though it's in another language.

Here's in Playbasic (what i've managed to port and get working)

PlayBASIC Code: [Select]
; PROJECT : Raycastthing
; AUTHOR : Programming_PC
; CREATED : 9/23/2013
; EDITED : 9/23/2013
; ---------------------------------------------------------------------
openscreen 256, 192, 32, 1

CLS
//VISIBLE 1,0,0,0,0,1
//PNLTYPE "OFF"
//CLEAR
O#=4095/4096
DIM MAP(16,16)
DIM TEX(8,8)
CAMX# = 1.5
CAMY# = 14.5
CAMZ# = 0.5
CAMA# = neg(PI#/3)
CAMB# = 0
//Q=Quality. Lower is slower.
Q=3

TOUCHING=0
JUMPING=0

GOSUB LOADMAP
GOSUB LOADTEX

NEXTCOUNT=MAINCNTL+60
//FPS=0

PAGE=1
MAINLOOP:
//GPAGE 0,!PAGE,PAGE
//PAGE=!PAGE
GOSUB MOVE

BoxC 0,0,255,95-CAMB#, 1, rgb(50, 50,255)
BoxC 0,96-CAMB#,255,191, 1, RGB(100, 100, 100)
GOSUB DRAWWALLS
//FPS=FPS+1
//IF MAINCNTL
//NEXTCOUNT=MAINCNTL+60
//LOCATE 0,0
//PRINT FPS;" FPS"
//FPS=0
NOFPS:


Dx=Dx+1
Dy=Dy+1

sync
cls rgb(0,0,0)
GOTO MAINLOOP

MOVE:
X=mousex()
Y=mousey()
B=TCHST
IF not leftmousebutton() then GOTO NOTOUCH
CAMA#=CAMA#-(X-TOUCHX)*0.05
CAMB#=CAMB#+(Y-TOUCHY)
IF ABS(CAMB#)>60 THEN CAMB#=60*SGN(CAMB#)
NOTOUCH:
TOUCHING=B
TOUCHX=X
TOUCHY=Y

DX=COS(CAMA#)*0.2
DY=SIN(CAMA#)*0.2
X=CAMX#
Y=CAMY#
//B=BUTTON(0)
IF JUMPING=0 then GOTO NOTJUMPING
CAMZ#=CAMZ+VELZ
VELZ=VELZ-0.06
IF CAMZ#<0.5 THEN CAMZ#=0.5:JUMPING=0
GOTO JUMPEND
NOTJUMPING:
WALKING=WALKING+1
if not spacekey() then walking=0
CAMZ#=0.5+ABS(SIN(WALKING))*0.15
WALKING=1
JUMPING=1
CAMZ#=0.5
VELZ=0.2
JUMPEND:

IF upkey() THEN X=X+DX and Y=Y+DY
IF downkey() THEN X=X-DX and Y=Y-DY
IF leftkey() THEN X=X-DY and Y=Y+DX
IF rightkey() THEN X=X+DY and Y=Y-DX

//IF MAP(X,Y) THEN RETURN
CAMX#=X
CAMY#=Y
RETURN

DRAWWALLS:
ANGSTEP = PI#*Q/3/256
CURRANG = PI#/6
FOR I=0 TO 255 STEP Q
C=COS(CAMA#+CURRANG)
S=SIN(CAMA#+CURRANG)
GOSUB RAYCAST
DX=COLLX-CAMX#
DY=COLLY-CAMY#
DIST=SQRT(DX*DX+DY*DY)
IF not DIST THEN DIST=1/4096
H=24/DIST/COS(CURRANG)
T=(T+SLICE)*8
J=I+Q-1
P=96-CAMB#+H*8*CAMZ#-H

IF T<0 then T=0
if T>8 then T=8

colcol=TEX(T, 7)
if colcol=1 then ink rgb(255, 255, 255)
if colcol=2 then ink rgb(255, 0, 0)

Box I,P,J,P+H, 1

colcol=TEX(T, 6)
if colcol=1 then ink rgb(255, 255, 255)
if colcol=2 then ink rgb(255, 0, 0)

Box I,P-H,J,P,1
P=P-H*2

colcol=TEX(T, 5)
if colcol=1 then ink rgb(255, 255, 255)
if colcol=2 then ink rgb(255, 0, 0)
Box I,P,J,P+H,1

colcol=TEX(T, 4)
if colcol=1 then ink rgb(255, 255, 255)
if colcol=2 then ink rgb(255, 0, 0)
Box I,P-H,J,P,1




P=P-H*2

colcol=TEX(T, 3)
if colcol=1 then ink rgb(255, 255, 255)
if colcol=2 then ink rgb(255, 0, 0)
Login required to view complete source code



Here's in the original language (MORE LIKE BASIC)


ACLS
VISIBLE 1,0,0,0,0,1
PNLTYPE "OFF"
CLEAR
O=4095/4096
DIM MAP(16,16)
DIM TEX(8,8)
CAMX = 1.5
CAMY = 14.5
CAMZ = 0.5
CAMA = -PI()/3
CAMB = 0
'Q=Quality. Lower is slower.
Q=4

TOUCHING=0
JUMPING=0

GOSUB @LOADMAP
GOSUB @LOADTEX

NEXTCOUNT=MAINCNTL+60
FPS=0

PAGE=1
@MAINLOOP
GPAGE 0,!PAGE,PAGE
PAGE=!PAGE
GOSUB @MOVE
GFILL 0,0,255,95-CAMB,6
GFILL 0,96-CAMB,255,191,1
GOSUB @DRAWWALLS
FPS=FPS+1
IF MAINCNTL<NEXTCOUNT GOTO @NOFPS
NEXTCOUNT=MAINCNTL+60
LOCATE 0,0
PRINT FPS;" FPS"
FPS=0
@NOFPS
GOTO @MAINLOOP

@MOVE
X=TCHX
Y=TCHY
B=TCHST
IF !(TOUCHING AND B) GOTO @NOTOUCH
CAMA=CAMA-(X-TOUCHX)*.01
CAMB=CAMB+(Y-TOUCHY)
IF ABS(CAMB)>60 THEN CAMB=60*SGN(CAMB)
@NOTOUCH
TOUCHING=B
TOUCHX=X
TOUCHY=Y

DX=COS(CAMA)*.2
DY=SIN(CAMA)*.2
X=CAMX
Y=CAMY
B=BUTTON(0)
IF !JUMPING GOTO @NOTJUMPING
CAMZ=CAMZ+VELZ
VELZ=VELZ-.06
IF CAMZ<.5 THEN CAMZ=.5:JUMPING=0
GOTO @JUMPEND
@NOTJUMPING
WALKING=WALKING+0.9
IF !(B AND 15) THEN WALKING=0
CAMZ=0.5+ABS(SIN(WALKING))*.15
IF B AND 256 THEN WALKING=0:JUMPING=1:CAMZ=.5:VELZ=.2
@JUMPEND

IF B AND 1 THEN X=X+DX:Y=Y+DY
IF B AND 2 THEN X=X-DX:Y=Y-DY
IF B AND 4 THEN X=X-DY:Y=Y+DX
IF B AND 8 THEN X=X+DY:Y=Y-DX

IF MAP(X,Y) THEN RETURN
CAMX=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+O*(S<0)
COLLX=X-(Y-COLLY)/DY*DX
SLICE=ABS(COLLX%1-O*(S<0))
RETURN
@CASTXCOLL
COLLX=X
COLLY=Y
T=MAP(X,Y)-1
SLICE=ABS(COLLY%1-O*(C>0))
RETURN

@LOADMAP
RESTORE @MAPDATA
FOR J=15 TO 0 STEP -1
FOR I=0 TO 15
READ MAP(I,J)
NEXT
NEXT
RETURN

@MAPDATA
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DATA 1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1
DATA 1,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1
DATA 1,0,0,1,0,0,0,0,0,1,0,0,0,1,0,1
DATA 1,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1
DATA 1,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1
DATA 1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1
DATA 1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1
DATA 1,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1
DATA 1,0,1,0,0,0,1,1,0,0,0,0,0,1,0,1
DATA 1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1
DATA 1,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1
DATA 1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

@LOADTEX
RESTORE @TEXDATA
FOR T=0 TO 0
FOR J=0 TO 7
FOR I=0 TO 7
READ TEX(T*8+I,J)
NEXT
NEXT
NEXT
RETURN

@TEXDATA
DATA 18,18,18,30,18,18,18,30
DATA 18,18,18,30,18,18,18,30
DATA 30,30,30,30,30,30,30,30
DATA 18,30,18,18,18,30,18,18
DATA 18,30,18,18,18,30,18,18
DATA 30,30,30,30,30,30,30,30
DATA 18,18,18,30,18,18,18,30
DATA 18,18,18,30,18,18,18,30





In the second code listed here the TCHX and TCHY are system variables where the touch screen was last touched or is being touched.

kevin


it seems to be full of mismatched data types..

ie.

PlayBASIC Code: [Select]
    C=COS(CAMA#+CURRANG)
S=SIN(CAMA#+CURRANG)



  C & S are integers, not floats...


PlayBASIC Code: [Select]
   H=24/DIST/COS(CURRANG)




  H is integer.. The expression will resolve   24/Dist (integer division), which is then divided by float (cos(CURRANG) 


  Just make all the tracing variables floats at least.