forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
September 08, 2010, 09:18:32 AM
.
News: 06/10/2009 - New Features For Forum.
Friendly URLs, twitter for profile, and video site URLs will auto-display the video.
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read August 27, 2008, 03:52:09 PM #0
osgeld

The worlds crappiest ray-caster

ray casting test/demo

theory found here
script mostly ripped from here

changes from the brain damage version (besides changing functions tween the 2 systems)

  • most everything removed out of table structures, tables are slow, luaplayer is slow, this was really slow
  • removed redundant math (do we really need to calc "math.pi /180" 7 times a loop?)
  • removed multiple colors
  • replaced floor and sky rendering with a static image
  • changed rendering and ray casting properties

all of the above was done in order to get a decent frame rate, without those changes it ran full screen @ 333mhz at about 1 frame per 1-5 seconds

now @ 333mhz, its still slow, and still bogs down, but its frame rate is almost respectable (maybe 8 fps?)

why?

dunno, i got this stuck in my head over the weekend, today i happened to be up at 6 am nursing a sick cat, which means i was stuck in the spare room with my old compaq and nothing to do most of the morning

all in all it was a amusing learning experience

script:
Code:
-------------------------
-------------------------
-- crappy raycast test --
-- for luaplayer 0.20  --
-- 2008 CFDT / OSGELD  --
-------------------------
-------------------------

-- basic setup
screen_width   = 480
screen_height  = 272
feild_of_view  = 060 -- degrees
sliver_width   = 010 -- thickness of wall sliver in pixels
wall_zoom      = 200 -- how much to zoom the walls
ray_resolution = 0.05

slivers_per_screen = math.ceil(screen_width / sliver_width)
ang_per_sliver = feild_of_view / slivers_per_screen
pibt = (math.pi / 180)
fovbt = (feild_of_view / 2)

-- color data
color = Color.new(200, 200, 200)
bg    = Image.load("bg.JPG")

-- map data
map_width  = 11
map_height = 11
map_start_point_x = 2.5
map_start_point_y = 2.5
map_start_angle   = 90
map =
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

}

-- player data
player_speed  = 0.1
player_turn   = 5
player_x = map_start_point_x
player_y = map_start_point_y
player_ang = map_start_angle


function get_map(x, y)
x = math.floor(x)
y = math.floor(y)

if (x < 1 or y < 1) or (x > map_width or y > map_height) then
return 1
end

return map[y][x]
end

function cast_ray(x, y, ang)
local distance = 0
local dx = math.cos(ang * pibt) * ray_resolution
local dy = math.sin(ang * pibt) * ray_resolution

repeat
x = x + dx
y = y + dy
distance = distance + ray_resolution
col = get_map(x, y)
if (col ~= 0) then
break
end
until (false)

return distance, col
end


while true do
local ox = player_x
local oy = player_y
local pad = Controls.read()
screen:clear()
screen:blit(0,0,bg)
-- controls
if pad:up() then
player_x = player_x + math.cos(player_ang * pibt) * player_speed
player_y = player_y + math.sin(player_ang * pibt) * player_speed

elseif pad:down() then
player_x = player_x - math.cos(player_ang * pibt) * player_speed
player_y = player_y - math.sin(player_ang * pibt) * player_speed
end

if pad:right() then
player_ang = (player_ang + player_turn) % 360

elseif pad:left() then
player_ang = (player_ang + 360 - player_turn) % 360
end
--
-- check bounds
if (get_map(player_x, player_y) ~= 0) then
player_x = ox
player_y = oy
end
--
-- cast rays
local sx = 0
local ang = player_ang - fovbt
local ray_len = 0
local col = 0
local height = 0
local start_wall = 0

-- start from the left and cast a ray per sliver
for s = 1, slivers_per_screen do
-- cast the ray
ray_len, col = cast_ray(player_x, player_y, ang)
ray_len = ray_len *  math.cos((player_ang - ang) * pibt)

-- calulate wall height
height = math.floor(wall_zoom / ray_len)
start_wall = math.floor(136 - height / 2)

-- clip the wall to the screen
if (start_wall < 0) then
height = height + start_wall
start_wall = 0
end

if (start_wall + height > screen_height) then
height = screen_height - start_wall
end

-- draw the wall
screen:fillRect(sx, start_wall, sliver_width, height, color)
-- increase the sliver pos
sx = sx + sliver_width
-- increase the sliver angle
ang = ang + ang_per_sliver
end
screen.flip()
screen.waitVblankStart()
end

you will need an image named bg.JPG, mine is just a 480x272 image, top half dark blue, bottom half dark grey

EDIT!

i made an optimization which nearly doubled the rendering speed, which about 1/3 of that improvement was used to increase rendering quality (sharp corners vs blobby ones) the above code represents the current version 
« Last Edit: August 28, 2008, 09:18:10 PM by osgeld »
Offline  
Read August 28, 2008, 12:45:45 AM #1
PL3X

Re: The worlds crappiest ray-caster

If you fire several rays through the same pixel (say 16) and get the average colour returned from that intersection you can remove most of the aliasing.
Offline  
Read August 28, 2008, 12:38:36 PM #2
Buddy4point0

Re: The worlds crappiest ray-caster

Change
Code:
screen.waitVblankStart(1)
To just
Code:
screen.waitVblankStart()
Using LUAPlayer Windows I got about 105 fps when it was the way you have it now, and 130 after the change.
Could result in maybe a 3 fps change or so on the PSP.


veiw my coding projects here
Offline  
Read August 28, 2008, 06:08:45 PM #3
osgeld

Re: The worlds crappiest ray-caster

i did do that before the optimization, and it did not make a darn bit of difference

omitting the vblank makes a huge difference, but the screen ghost's like crazy

of course that optimization was dealing with math functions and now it could actually make (like you said) about 3 fps difference, since the psp is not doing redundant math per cycle

its still not fast enough to do anti-aliasing, nor texture maps, nor decent quality

maybe if i used the gu ... but if im going tru that much trouble why the hell would i raycast. id use polys hehe

this isnt anything but a learning experiance...


anywho i changed the script again
« Last Edit: August 28, 2008, 09:17:49 PM by osgeld »
Offline  
Read August 31, 2008, 11:47:26 PM #4
Tdude_gamer

Re: The worlds crappiest ray-caster

hey, i really suck at 3d so i probably wouldn't know, but i think you did a good job.... nice, will help me learn a bit more about 3d in lua. =)


if you want help on a project, i would be happy to help
Offline  
Read August 31, 2008, 11:58:17 PM #5
PL3X

Re: The worlds crappiest ray-caster

This is a ray caster, the theory is applicable to any API, if you want to learn about the GU library perhaps looking at the documentation would be a better option.
Offline  
Read September 01, 2008, 12:00:37 AM #6
Tdude_gamer

Re: The worlds crappiest ray-caster

yeah... ill look at everything. if there's one thing that i learned from these forums, it's that you are always right. lol. all part of the learning experience for me. will definately check documentations on theory and stuffs out...


if you want help on a project, i would be happy to help
Offline  
Read September 01, 2008, 11:30:30 AM #7
osgeld

Re: The worlds crappiest ray-caster

theres actually not all that much 3d about it

its using distances and "line of sight" to render 2d rectangles in a 3d perspective
Offline  
Read September 01, 2008, 02:49:41 PM #8
Tdude_gamer

Re: The worlds crappiest ray-caster

yeah, i know, but hey, that's how 3d is made right, functions that render 2d graphics to generate a 3d image  Smile it's like 3d made simple... or brocken down i should say. Smile i think... Confused


if you want help on a project, i would be happy to help
Offline  
Read September 01, 2008, 05:45:47 PM #9
osgeld

Re: The worlds crappiest ray-caster

read the first link in the first post ///

but yea its very basic old school way of presenting a 3d image
Offline  
Read September 01, 2008, 05:59:05 PM #10
PL3X

Re: The worlds crappiest ray-caster

There's dozens of ways of creating 3d scenes, ray caster, ray tracer, photon mapping, photon tracer, directx, openGL or various engines that all calculate their scenes with different algorithms like perspective projection.

A simple way to think of 3D programming is a huge number of calculations to produce a 2D image, after all, your screen doesn't have depth.

Most 3D engines work like this, all they do is apply a whole lot of calculations (that is a generalisation) to find the colour of a single pixel, then save it to memory and later print it.
Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re