Author Topic: [Lua] RayCaster  (Read 43950 times)

0 Members and 1 Guest are viewing this topic.

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
[Lua] RayCaster
« on: July 11, 2011, 12:00:35 pm »
Hello guys !

With the OS 3, we had no mean to have 3D games on our calcs. This was a shame, because it's really funny when people are amazed by on-calc 3D stuff. (Actually, there is already Make3D by Levak)
But now, the first raycaster engine in Lua is released !
It's only a demo, you can move in a small 3D world in greyscale (I'll add colors).
Since Lua is interpreted, it's not really fast. So it's not very good-looking, because I cast one ray for two-pixels-wide columns...
Unfortunately, textures are impossible (it would become too much slow)

I plan to do a 3D game with this little engine... But nDoom will stay really really better than it.
(I attach the released demo and two screenies. Enjoy !)

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: [Lua] RayCaster
« Reply #1 on: July 11, 2011, 12:02:25 pm »
Wow! Looks great!
What exactly is a raycaster? :P

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: [Lua] RayCaster
« Reply #2 on: July 11, 2011, 12:08:51 pm »
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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] RayCaster
« Reply #3 on: July 11, 2011, 12:11:31 pm »
Yep.

A raycaster is a tiny 3D engine which allows only perpendicular walls.

Offline p2

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 849
  • Rating: +51/-11
  • I'm back :)
    • View Profile
Re: [Lua] RayCaster
« Reply #4 on: July 11, 2011, 12:11:59 pm »
WOW!!!  :o  :o
*insert supercool signature*

Ashbad

  • Guest
Re: [Lua] RayCaster
« Reply #5 on: July 11, 2011, 12:16:44 pm »
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

Well, no actually :) a ray caster is basically projecting a 3D version of a 2D map based on your position, where some data will be walls and render as so, and everything else will be empty.  However, you can add non-raycasted objects in there as well like what Doom does using other techniques.

On a side note, nice job :)

Offline p2

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 849
  • Rating: +51/-11
  • I'm back :)
    • View Profile
Re: [Lua] RayCaster
« Reply #6 on: July 11, 2011, 12:18:51 pm »
Why don't you make a game of it?
So that you'll have to walk around in this labyrinth and that you'll have to do things. (fight against monsters, Find things, ...)
*insert supercool signature*

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] RayCaster
« Reply #7 on: July 11, 2011, 12:20:52 pm »
I plan to do that.
But I need some time :) I'll post here when I will have something decent.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [Lua] RayCaster
« Reply #8 on: July 11, 2011, 12:42:39 pm »
That looks very nice. I can't see any real game being made with this because it's very slow, afterall it's Lua, here's an animated screenshot I made for you:



Nevertheless, it looks good

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: [Lua] RayCaster
« Reply #9 on: July 11, 2011, 12:44:51 pm »
Oh, I get it.
Is there any chance you can upload the code for this?

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] RayCaster
« Reply #10 on: July 11, 2011, 12:49:01 pm »
Sure... But it's not very good-looking :
Code: [Select]
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

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: [Lua] RayCaster
« Reply #11 on: July 11, 2011, 12:58:00 pm »
That's very cool.  Great job, Chockosta.  It's a very nice attempt at pushing Lua's limits.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: [Lua] RayCaster
« Reply #12 on: July 11, 2011, 12:58:40 pm »
whats the framerate right now?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Chockosta

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 447
  • Rating: +169/-6
    • View Profile
Re: [Lua] RayCaster
« Reply #13 on: July 11, 2011, 01:01:28 pm »
Well, the framerate is key input framerate. I don't know exacty it. between 2 and 3 fps on my calc

EDIT : or maybe more, I really don't know :)
« Last Edit: July 11, 2011, 01:03:42 pm by Chockosta »

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: [Lua] RayCaster
« Reply #14 on: July 11, 2011, 01:20:05 pm »
It shouldn't be too hard to calculate the framerate if you have a timer you can use. Also, on a calc, the framerate does not need to be very high, as the LCD has a very slow response time, so a high framerate would look like a blur.
« Last Edit: July 11, 2011, 01:23:49 pm by t0xic_kitt3n »