'JIAN2587 RAYCASTING ALGORITHMS 'Story behind raycasting (please jump to the end) 'First, you have to initialize a few variables. X,Y of the player, steps covered by one key 'press, heading of the player, and most important, the 2D map. 'Why I dim extra -31 to +32? That's the 64 rays I am gonna send out '0.Dim SinuseTable(-31 TO 360 + 32), CosineTable(-31 TO 360 + 32) '1.First, read the map into a 2 dimension array. '2.Caculate the Sinuse/Cosine table to speed up calculations. '''*****Stride - Steps moved per movement*****''' CONST BlockWidth = 900 CONST Phi = 100 '/*Calculates Cosine/Sinuse table*/' Factor = (ATN(1) * 8) / 360 FOR D = 0 TO 359 Angle = CSNG(D) * Factor SinuseTable(D) = SIN(Angle) * .1 '.1 is the size of every grid X CosineTable(D) = COS(Angle) * .1 '.1 is the size of every grid Y NEXT '/*Calculates steps covered(bit hard 2 explain, go down for exact explanation)*/' FOR A = -31 TO -1 SinuseTable(A) = SinuseTable(A + 360) CosineTable(A) = CosineTable(A + 360) NEXT FOR A = 360 TO 360 + 32 SinuseTable(A) = SinuseTable(A - 360) CosineTable(A) = CosineTable(A - 360) NEXT '3.Update it 'Left Heading = (Heading + Turn) MOD 360 'Right Heading = (Heading + (360 - Turn)) MOD 360 'Forward NewPositionX = CurrentPositionX - (SinuseTable(Heading) * Stride) NewPositionY = CurrentPositionY - (CosineTable(Heading) * Stride) '/*Checks if it's walkthroughable*/' 'Backward NewPositionX = CurrentPositionX + (SinuseTable(Heading) * Stride) NewPositionY = CurrentPositionY + (CosineTable(Heading) * Stride) '/*Checks if it's walkthroughable*/' '4.For every movement and after computations, update the graphic '**PCOPY Background1, WorkPage: Swaps Background1 with 2 'For each viewing field vertical ray from left to right you are viewing... X1 = 0 FOR A = Heading + 32 TO Heading - 31 STEP -1 StepX = SinuseTable(A): StepY = CosineTable(A) XX = CurrentPositionX: YY = CurrentPositionY L = 0 DO 'OK, this loop test if there's any wall to draw... XX = XX - StepX: YY = YY - StepY L = L + 1 K = Map's XX and YY LOOP UNTIL K 'Yeah, found one, draw it DD = BlockWidth \ L 'BlockWidth=900|This is the wall's width H = DD + DD 'Height of your EYE from the ground DT = Phi - DD 'A higher value would see the ceiling 'Lower would see the floor :) LINE (X1, DT)-STEP(4, H), K ,BF X1 = X1 + 5 'Why 5? See below for further explanation NEXT 'Okay, See that FOR A = Heading + 32 TO Heading - 31 STEP -1 ? 'That's about 64 loops, so each wall block will be cut into 64 pieces and drew separately 'Hey, that's blocky(Coz' it speeds up everythin') So, since we are using 320X200 so tha' 'X1 = X1 + 5 makes us draw each pieces with width of 5 and 64 * 5 = 320 horizontal 'phew!