pianoman I believe that it is a program that lets you draw 3d things like walls, trees and basically anything else. It calculates where the lines should go to. This is just my guess
function racine(a)
x=1
y=0.5*(1+a)
while math.abs(y-x)>0.001 do
x=y
y=0.5*(x+a/x)
end
return y
end
function renderMap(gc,map,w,h,camera)
for x=0,w,2 do
dirX=math.cos(camera[3])
dirY=math.sin(camera[3])
planeX=math.cos(camera[3]-1.5707963267949)
planeY=math.sin(camera[3]-1.5707963267949)
cameraX=(x*2/w)-1
rayPosX=camera[1]
rayPosY=camera[2]
rayDirX=dirX+planeX*cameraX
rayDirY=dirY+planeY*cameraX
mapX=math.floor(rayPosX)
mapY=math.floor(rayPosY)
deltaDistX=racine(1+(rayDirY*rayDirY)/(rayDirX*rayDirX))
deltaDistY=racine(1+(rayDirX*rayDirX)/(rayDirY*rayDirY))
hit=0
if rayDirX<0 then
stepX=-1
sideDistX=(rayPosX-mapX)*deltaDistX
else
stepX = 1;
sideDistX = (mapX+1-rayPosX)*deltaDistX
end
if rayDirY<0 then
stepY=-1;
sideDistY=(rayPosY-mapY)*deltaDistY
else
stepY=1;
sideDistY=(mapY+1-rayPosY)*deltaDistY
end
while hit==0 do
if sideDistX<sideDistY then
sideDistX=sideDistX+deltaDistX
mapX=mapX+stepX
side=0
else
sideDistY=sideDistY+deltaDistY
mapY=mapY+stepY
side=1
end
if map[mapX][mapY]>0 then
hit=1
end
end
if side==0 then
perpWallDist=math.abs((mapX-rayPosX+(1-stepX)/2)/rayDirX)
else
perpWallDist=math.abs((mapY-rayPosY+(1-stepY)/2)/rayDirY)
end
hauteurLigne=math.abs(math.floor(h/perpWallDist))
drawStart=math.floor(-hauteurLigne/2+h/2)
drawEnd=math.floor(hauteurLigne/2+h/2)
if drawStart<0 then
drawStart=0
end
if drawEnd>=h then
drawEnd=h-1
end
gc:setColorRGB(10,10,10)
if side==1 then
gc:setColorRGB(100,100,100)
end
gc:fillRect(x,drawStart,2,drawEnd-drawStart)
end
end
function on.escapeKey()
menu=1
platform.window:invalidate()
end
on.escapeKey()
function on.enterKey()
menu=nil
fovy=60
camera={10,10,0.05}
w=320 h=240
planeX=0 planeY=1
map={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1},{1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1},{1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1},{1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1},{1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1},{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
platform.window:invalidate()
end
function on.arrowKey(ar)
turn=nil
if ar=="right" then
camera[3]=camera[3]-0.1
end
if ar=="left" then
camera[3]=camera[3]+0.1
end
if ar=="up" then
if map[math.floor(camera[1]+math.cos(camera[3])/3)][math.floor(camera[2]+math.sin(camera[3])/3)]==0 then
camera[1]=camera[1]+math.cos(camera[3])/3
camera[2]=camera[2]+math.sin(camera[3])/3
end
end
if ar=="down" then
if map[math.floor(camera[1]-math.cos(camera[3])/4)][math.floor(camera[2]-math.sin(camera[3])/4)]==0 then
camera[1]=camera[1]-math.cos(camera[3])/4
camera[2]=camera[2]-math.sin(camera[3])/4
end
end
platform.window:invalidate()
end
function on.paint(gc)
if menu then
gc:setColorRGB(0,0,255)
gc:setFont("sansserif","r",30)
gc:drawString("RayCaster Demo",30,0,"top")
gc:setFont("serif","b",10)
gc:drawString("Press Enter to start",100,100,"top")
else
gc:setColorRGB(200,200,200)
gc:fillRect(0,0,platform.window:width(),platform.window:height())
renderMap(gc,map,platform.window:width(),platform.window:height(),camera)
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","r",8)
gc:drawString("Lua RayCaster Demo - Par Loic Pujet",10,200,"top")
end
This is real nice, good job Chockosta!Nope...
What's about a little map, to see, where you are (Shown while pressing special key)I thought this was supposed to be a maze game. I think that would be better without the map personally
So would windows be possible?
Of course, if it is a maze, the map must not be shown. But if it becomes a FPS or a GTA, the map is a good idea.That would be a good idea
This is not my idea, give credits and +1s to p2.Of course, if it is a maze, the map must not be shown. But if it becomes a FPS or a GTA, the map is a good idea.That would be a good idea
would it be possible to add windows into the walls?
Unfortunately, I only can draw plain walls, like Toxic_Kitten said.
You don't actually know if Chockosta used his code or not, so I wouldn't go and accuse him, a PM asking him about this would be more appropriate. However, if this is true, then Chockosta, you should give him credit.Alright.
double cameraX = 2 * x / double(w) - 1; //x-coordinate in camera space
double rayPosX = posX;
double rayPosY = posY;
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
Chokosta's code:cameraX=(x*2/w)-1
rayPosX=camera[1]
rayPosY=camera[2]
rayDirX=dirX+planeX*cameraX
rayDirY=dirY+planeY*cameraX
int mapX = int(rayPosX);
int mapY = int(rayPosY);
double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
Chokosta's code:mapX=math.floor(rayPosX)
mapY=math.floor(rayPosY)
deltaDistX=racine(1+(rayDirY*rayDirY)/(rayDirX*rayDirX))
deltaDistY=racine(1+(rayDirX*rayDirX)/(rayDirY*rayDirY))
if (rayDirX < 0) {
stepX = -1;
sideDistX = (rayPosX - mapX) * deltaDistX;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
}
if (rayDirY < 0) {
stepY = -1;
sideDistY = (rayPosY - mapY) * deltaDistY;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
}
Chokosta's code:if rayDirX<0 then
stepX=-1
sideDistX=(rayPosX-mapX)*deltaDistX
else
stepX = 1;
sideDistX = (mapX+1-rayPosX)*deltaDistX
end
if rayDirY<0 then
stepY=-1;
sideDistY=(rayPosY-mapY)*deltaDistY
else
stepY=1;
sideDistY=(mapY+1-rayPosY)*deltaDistY
end
while (hit == 0) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if (worldMap[mapX][mapY] > 0)
hit = 1;
}
Chokosta's code:while hit==0 do
if sideDistX<sideDistY then
sideDistX=sideDistX+deltaDistX
mapX=mapX+stepX
side=0
else
sideDistY=sideDistY+deltaDistY
mapY=mapY+stepY
side=1
end
if map[mapX][mapY]>0 then
hit=1
end
end
if (side == 0)
perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
else
perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
Chokosta's code:if side==0 then
perpWallDist=math.abs((mapX-rayPosX+(1-stepX)/2)/rayDirX)
else
perpWallDist=math.abs((mapY-rayPosY+(1-stepY)/2)/rayDirY)
end
int lineHeight = abs(int(h / perpWallDist));
int drawStart = -lineHeight / 2 + h / 2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight / 2 + h / 2;
if(drawEnd >= h)
drawEnd = h - 1;
Chokosta's code:hauteurLigne=math.abs(math.floor(h/perpWallDist))
drawStart=math.floor(-hauteurLigne/2+h/2)
drawEnd=math.floor(hauteurLigne/2+h/2)
if drawStart<0 then
drawStart=0
end
if drawEnd>=h then
drawEnd=h-1
end
Hayleia I don't know what Wolfenstein has to do with all this, you clearly did not read either the content in the link nor my last message.Ok, I should not fight but I read your link and it is written "Wolfenstein 3D" so it is you that didn't read your link before speaking. So before saying people are stupid, look at you. As I don't want to start a fight, I will not answer you from now.
Ok, I should not fight but I read your link and it is written "Wolfenstein 3D" so it is you that didn't read your link before speaking. So before saying people are stupid, look at you.The thing he linked to was an raycasting tutorial. The mention of Wolfenstein there was only intended to serve as an example of what raycasting can do - that guy didn't make Wolfenstein or use any of its code.
If you want to keep saying people copy others, go to the ASM forums and ask why they all use bcall(ClrLCDfull)The programmers there aren't trying to claim that they wrote ClrLCDFull. They just call it whenever they have to use it.
...
The programmers there aren't trying to claim that they wrote ClrLCDFull. They just call it whenever they have to use it.
Yes. We probably just stop fighting....
The programmers there aren't trying to claim that they wrote ClrLCDFull. They just call it whenever they have to use it.
But me, I wrote this raycaster. I followed a tutorial, but I wrote the entire thing.
The tutorial only mentioned Wolfenstein a couple of times, but it has nothing to do with the core content. Also, when did I say anyone was stupid? Furthermore, since when has discussing about an issue on the Internet become fighting?Hayleia I don't know what Wolfenstein has to do with all this, you clearly did not read either the content in the link nor my last message.Ok, I should not fight but I read your link and it is written "Wolfenstein 3D" so it is you that didn't read your link before speaking. So before saying people are stupid, look at you. As I don't want to start a fight, I will not answer you from now.
If you want to keep saying people copy others, go to the ASM forums and ask why they all use bcall(ClrLCDfull)
I stand my ground. A big part of the code was copied from the tutorial and you did in no way mention it or the creator of it. There's a difference in reading documents and making up your own mind (i.e. reading many tutorials and then writing your own code from scratch), and copying chunks from another person's code (it doesn't matter if you removed a couple of brackets and changed function names to make it work on the calculator, it's not what's important). He may have meant to have his code copied freely, but it still doesn't change the fact that it is disrespectful to pretend it was all made by you (don't tell me you didn't try to), when it clearly wasn't. It's not a crime; it's just a matter of education, respect and honesty....
The programmers there aren't trying to claim that they wrote ClrLCDFull. They just call it whenever they have to use it.
But me, I wrote this raycaster. I followed a tutorial, but I wrote the entire thing.
Sure. I totally agree. Please read my post ( http://ourl.ca/12034/227840 ) and tell me which tutorial is the original one, then I'll mention it.
And I did not "removed a couple of brackets and changed function names". I used the tutorial author's code to create mine. Isn't it the purpose of a tutorial ?
Also, I'm not a great coder, and I've never done a 3D engine (even if it's only a raycaster) before that. So I have to train, and that's a training. It's absolutely not a finished program, it's a trial of an engine for a possible game.
You are right, I should have given credits to the author. But what you say is a little excessive, don't you think so ?
Oh okay, sorry my bad. I first thought that the fact that the variables dirX, dirY, planeX, planeY, cameraX, rayPosX, rayPosY, rayDirX, rayDirY, sideDistX, sideDistY, perpWallDist, mapX, mapY, hit, stepX, stepY, deltaDistX, deltaDistY, side, drawStart and drawEnd have the exact same name and do exactly the same thing in Vandevenne's was a coincidence.It's a good, easy to understand naming system. Im also using it for the raycaster I'm working on.
For Wolfeinstein, sorry, I'm French so the only word I understood to the tutorial was Wolfeinstein ;DOk, I should not fight but I read your link and it is written "Wolfenstein 3D" so it is you that didn't read your link before speaking. So before saying people are stupid, look at you.The thing he linked to was an raycasting tutorial. The mention of Wolfenstein there was only intended to serve as an example of what raycasting can do - that guy didn't make Wolfenstein or use any of its code.If you want to keep saying people copy others, go to the ASM forums and ask why they all use bcall(ClrLCDfull)The programmers there aren't trying to claim that they wrote ClrLCDFull. They just call it whenever they have to use it.
You could at least have given credit to Lode Vandevenne (http://lodev.org/cgtutor/raycasting.html) before copying the code from his tutorial nearly word by word, or you should've at least changed the variable names to give us the illusion that the "Par Loic Pujet" actually was true.
Sorry for being harsh, but I don't feel much sympathy for people who pretend to have made something themselves, and don't even have the guts to thank or in the slightest way mention the original creator. I'm looking forward to hearing your silly excuses.
I'm looking forward to hearing your silly excuses.will just cause more fights. Please just help stop it instead of increasing the problem.
I ever thought it's not possible to make 3D objects in basic.Its possible but its really hard(I believe)
gc:drawRect(x, y, xwidth, yheight)
gc:drawImage(image.copy(columnImage, xwidth, yheight), x, y)
That would be a lot of TI.images :PNobody has to know if you keep them in an array, hehe
[ i'm not saying anything, okay ? ]:o ... interesting
TI.Image will be faster in the future (very probably) thanks to [ that I really can't say ]
[ / oops what did I just say ]
/me hits adriweb with a large nspire prototype :p
/me hits adriweb with a large nspire prototype :p
/me blocks jimbauwens with 2 released Nspire and 3 prototypes ! :P
pieces={{{0,0,0,0,0,0,0},{0,0,0,1,0,0,0},{0,0,0,0,0,0,0},{0,1,0,2,0,1,0},{0,0,0,0,0,0,0},{0,0,0,1,0,0,0},{0,0,0,0,0,0,0}},{{1,0,1,0,0,0,1},{1,0,1,1,1,0,1},{0,0,0,0,1,0,1},{0,1,0,0,1,0,0},{0,1,0,1,1,0,0},{1,1,0,0,1,1,0},{1,1,0,0,0,0,0}},{{1,1,1,0,1,1,1},{1,0,0,0,0,0,1},{1,0,0,1,0,0,1},{0,0,1,1,1,0,0},{1,0,0,1,0,0,1},{1,0,0,0,0,0,1},{1,1,1,0,1,1,1}},{{1,1,1,0,1,1,1},{0,0,1,0,0,1,0},{0,0,1,0,1,0,0},{0,0,0,0,0,0,0},{0,1,1,1,1,1,0},{0,0,0,0,0,0,1},{1,0,0,0,1,1,1}},{{1,1,1,0,1,1,1},{1,0,0,0,0,0,1},{1,0,1,0,1,0,1},{0,0,0,0,0,0,0},{1,0,1,0,1,0,1},{1,0,0,0,0,0,1},{1,1,1,0,1,1,1}},{{1,1,1,0,0,0,0},{1,0,0,0,0,1,0},{1,0,0,0,1,0,0},{0,0,0,1,0,0,0},{0,0,1,0,0,0,1},{0,1,0,0,0,0,1},{0,0,0,0,1,1,1}}}
function width() return platform.window:width() end
function height() return platform.window:height() end
function refresh() platform.window:invalidate() end
function generateMaze()
local tempMap,mazePieces,currentRow
tempMap={}
tempMap[1]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
tempMap[30]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
mazePieces={}
for i=1,16 do
mazePieces[i]=pieces[math.random(2,6)]
end
mazePieces[math.random(2,16)]=pieces[1]
for i=1,28 do
currentRow={1}
currentRow[30]=1
for j=1,28 do
currentRow[j+1]=mazePieces[math.floor((j-1)/7)+math.floor((i-1)/7)*4+1][(i-1)%7+1][(j-1)%7+1]
end
tempMap[i+1]=currentRow
end
map1=tempMap
end
function sqrt2(a)
x=1
y=0.5*(1+a)
while math.abs(y-x)>0.001 do
x=y
y=0.5*(x+a/x)
end
return y
end
function exp2(a)
return a*a
end
function sign(a)
if a<0 then
return -1
else
return 1
end
end
function render3D(gc,surf,map,cam)
gc:setColorRGB(189,228,255)
gc:fillRect(surf.x,surf.y,surf.w,surf.h/2)
gc:setColorRGB(47,127,0)
gc:fillRect(surf.x,surf.y+surf.h/2,surf.w,surf.h/2)
for i=0,surf.w-1,surf.res do
column=(i*2/surf.w)-1
rayStartX=cam.x
rayStartY=cam.y
rayDirX=math.cos(cam.rot)+math.cos(cam.rot-1.5707963267949)*column
rayDirY=math.sin(cam.rot)+math.sin(cam.rot-1.5707963267949)*column
rayCellX=math.floor(rayStartX)
rayCellY=math.floor(rayStartY)
rayStepX=sqrt2(exp2(rayDirY)/exp2(rayDirX)+1)
rayStepY=sqrt2(exp2(rayDirX)/exp2(rayDirY)+1)
rayLengthX=rayStepX*(rayStartX%1*sign(-rayDirX)+(sign(rayDirX)+1)/2)
rayLengthY=rayStepY*(rayStartY%1*sign(-rayDirY)+(sign(rayDirY)+1)/2)
hit=nil
while not hit do
if rayLengthX<rayLengthY then
rayLengthX=rayLengthX+rayStepX
rayCellX=rayCellX+sign(rayDirX)
wallSide=0
else
rayLengthY=rayLengthY+rayStepY
rayCellY=rayCellY+sign(rayDirY)
wallSide=1
end
if map[rayCellX][rayCellY]~=0 then
hit=true
wallType=map[rayCellX][rayCellY]
end
end
if wallSide==0 then
wallDist=math.abs((rayCellX-rayStartX+(1-sign(rayDirX))/2)/rayDirX)
else
wallDist=math.abs((rayCellY-rayStartY+(1-sign(rayDirY))/2)/rayDirY)
end
columnHeight=math.abs(math.floor(surf.h/wallDist))
columnStart=surf.h/2-columnHeight/2
if columnStart<0 then
columnStart=0
end
if columnHeight+columnStart>=surf.h then
columnHeight=surf.h-columnStart
end
if wallSide==1 then
gc:setColorRGB(100,0,0)
else
gc:setColorRGB(140,0,0)
end
if wallType==2 then
gc:setColorRGB(255,255,255)
end
gc:fillRect(i+surf.x,columnStart+surf.y,surf.res,columnHeight)
end
end
function drawMap(gc,map,cam)
gc:setColorRGB(69,45,0)
gc:setPen("medium","smooth")
gc:drawRect(0,0,66,66)
gc:setColorRGB(222,189,127)
gc:fillRect(0,0,66,66)
startX=1 startY=1
if cam.x>=6 then startX=math.floor(cam.x)-5 end
if cam.y>=6 then startY=math.floor(cam.y)-5 end
if cam.x+5>#map then startX=#map-10 end
if cam.y+5>#map[1] then startY=#map[1]-10 end
for i=startX,startX+10,1 do
for j=startY,startY+10,1 do
if map[i][j]~=0 then
if map[i][j]==1 then
gc:setColorRGB(120,78,0)
elseif map[i][j]==2 then
gc:setColorRGB(255,255,255)
end
gc:fillRect(60-(i*6-startX*6),j*6-startY*6,6,6)
end
end
end
gc:setColorRGB(69,45,0)
gc:fillArc(60-(cam.x*6-startX*6-3),cam.y*6-startY*6-3,5,5,0,360)
end
function on.escapeKey()
menu=1
won=nil
refresh()
end
on.escapeKey()
function on.enterKey()
menu=nil
mapDraw=nil
camera={x=3,y=3,rot=1.05}
screen={x=0,y=0,w=width(),h=height(),res=2}
generateMaze()
refresh()
end
function on.arrowKey(ar)
if not menu then
turn=nil
if ar=="right" then
camera.rot=camera.rot-0.1
end
if ar=="left" then
camera.rot=camera.rot+0.1
end
if ar=="up" then
if map1[math.floor(camera.x+math.cos(camera.rot)/3)][math.floor(camera.y+math.sin(camera.rot)/3)]~=1 then
camera.x=camera.x+math.cos(camera.rot)/3
camera.y=camera.y+math.sin(camera.rot)/3
end
end
if ar=="down" then
if map1[math.floor(camera.x-math.cos(camera.rot)/4)][math.floor(camera.y-math.sin(camera.rot)/4)]~=1 then
camera.x=camera.x-math.cos(camera.rot)/4
camera.y=camera.y-math.sin(camera.rot)/4
end
end
if map1[math.floor(camera.x)][math.floor(camera.y)]==2 then
menu=1
won=1
end
refresh()
end
end
function on.charIn(ch)
if not menu then
if ch=="m" then
mapDraw=not mapDraw
elseif ch=="*" and screen.w<width() then
screen.w=screen.w+width()/height()*5
screen.h=screen.h+5
screen.x=width()/2-screen.w/2
screen.y=height()/2-screen.h/2
elseif ch=="/" and screen.w>100 then
screen.w=screen.w-width()/height()*5
screen.h=screen.h-5
screen.x=width()/2-screen.w/2
screen.y=height()/2-screen.h/2
elseif ch=="h" then screen.res=2
elseif ch=="l" then screen.res=4
end
refresh()
end
end
function on.paint(gc)
if menu then
gc:setColorRGB(0,0,255)
gc:setFont("sansserif","r",30)
gc:drawString("MAZES 3D",75,0,"top")
if won then
gc:drawString("You won !",80,150,"top")
end
gc:setFont("serif","b",10)
gc:drawString("Press Enter to start",100,100,"top")
else
render3D(gc,screen,map1,camera)
if mapDraw then
drawMap(gc,map1,camera)
end
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","r",8)
gc:drawString("Lua Mazes 3D - Par Loic Pujet",10,200,"top")
end
So we need a real TI-Nspire CX emulator...Yep. I had the student software, but my license expired and I didn't feel like buying it. I wonder if it would be easy to add color support in the emu?
So we need a real TI-Nspire CX emulator...Yep. I had the student software, but my license expired and I didn't feel like buying it. I wonder if it would be easy to add color support in the emu?
I thought we had to decrypt the boot2 anyways for the emulator. Or is this a new kind of encryption?
Also, kinda off topic, but if the calc has 16 bit color, that means updating the screen from a screen buffer will take 4x as long.
local timer_stop = timer.stop;
local timer_start = timer.start;
local window = platform.window;
local invalidate = window.invalidate;
local w, h, planeX, planeY, map;
function on.timer()
timer_stop();
invalidate(window);
end
local math_abs = math.abs;
local math_floor = math.floor;
local math_cos, math_sin = math.cos, math.sin;
local function racine(a)
local x=1
local y=0.5*(1+a)
while math_abs(y-x)>0.001 do
x=y
y=0.5*(x+a/x)
end
return y
end
local function renderMap(gc,map,w,h,camera)
local math_cos, math_sin = math_cos, math_sin;
local dirX, dirY, rayPosX, rayPosY, rayDirX, rayDirY, mapX, mapY, deltaDistX,
deltaDisY, hit, stepX, sideDistX, stepY, sideDistY;
for x=0,w,2 do
dirX=math_cos(camera[3])
dirY=math_sin(camera[3])
planeX=math_cos(camera[3]-1.5707963267949)
planeY=math_sin(camera[3]-1.5707963267949)
cameraX=(x*2/w)-1
rayPosX=camera[1]
rayPosY=camera[2]
rayDirX=dirX+planeX*cameraX
rayDirY=dirY+planeY*cameraX
mapX=math_floor(rayPosX)
mapY=math_floor(rayPosY)
deltaDistX=racine(1+(rayDirY*rayDirY)/(rayDirX*rayDirX))
deltaDistY=racine(1+(rayDirX*rayDirX)/(rayDirY*rayDirY))
hit=0
if rayDirX<0 then
stepX=-1
sideDistX=(rayPosX-mapX)*deltaDistX
else
stepX = 1;
sideDistX = (mapX+1-rayPosX)*deltaDistX
end
if rayDirY<0 then
stepY=-1;
sideDistY=(rayPosY-mapY)*deltaDistY
else
stepY=1;
sideDistY=(mapY+1-rayPosY)*deltaDistY
end
while hit==0 do
if sideDistX<sideDistY then
sideDistX=sideDistX+deltaDistX
mapX=mapX+stepX
side=0
else
sideDistY=sideDistY+deltaDistY
mapY=mapY+stepY
side=1
end
if map[mapX][mapY]>0 then
hit=1
end
end
local perpWallDist;
if side==0 then
perpWallDist=math_abs((mapX-rayPosX+(1-stepX)/2)/rayDirX)
else
perpWallDist=math.abs((mapY-rayPosY+(1-stepY)/2)/rayDirY)
end
local hauteurLigne=math.abs(math_floor(h/perpWallDist))
local drawStart=math_floor(-hauteurLigne/2+h/2)
local drawEnd=math_floor(hauteurLigne/2+h/2)
if drawStart<0 then
drawStart=0
end
if drawEnd>=h then
drawEnd=h-1
end
gc:setColorRGB(10,10,10)
if side==1 then
gc:setColorRGB(100,100,100)
end
gc:fillRect(x,drawStart,2,drawEnd-drawStart)
end
end
local menu;
function on.escapeKey()
menu=1
invalidate(window);
end
on.escapeKey()
function on.enterKey()
menu=nil
--fovy=60
camera={10,10,0.05}
w=320 h=240
planeX=0 planeY=1
map={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1},{1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1},{1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1},{1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1},{1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1},{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},{1,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
invalidate(window);
end
function on.arrowKey(ar)
turn=nil
if ar=="right" then
camera[3]=camera[3]-0.1
end
if ar=="left" then
camera[3]=camera[3]+0.1
end
if ar=="up" then
if map[math_floor(camera[1]+math_cos(camera[3])/3)][math_floor(camera[2]+math_sin(camera[3])/3)]==0 then
camera[1]=camera[1]+math_cos(camera[3])/3
camera[2]=camera[2]+math_sin(camera[3])/3
end
end
if ar=="down" then
if map[math_floor(camera[1]-math_cos(camera[3])/4)][math_floor(camera[2]-math_sin(camera[3])/4)]==0 then
camera[1]=camera[1]-math_cos(camera[3])/4
camera[2]=camera[2]-math_sin(camera[3])/4
end
end
invalidate(window);
end
local milli = timer.getMilliSecCounter;
local last = 0;
function on.paint(gc)
if menu then
gc:setColorRGB(0,0,255)
gc:setFont("sansserif","r",30)
gc:drawString("RayCaster Demo",30,0,"top")
gc:setFont("serif","b",10)
gc:drawString("Press Enter to start",100,100,"top")
else
gc:setColorRGB(200,200,200)
gc:fillRect(0,0,platform.window:width(),platform.window:height())
renderMap(gc,map,platform.window:width(),platform.window:height(),camera)
end
gc:setColorRGB(0,0,0)
gc:setFont("sansserif","r",8)
gc:drawString("FPS: " .. tostring(1000/(milli()-last)),10,200,"top")
last = milli();
timer_start(0.01);
end