Raycasting help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nulldorf
Prole
Posts: 3
Joined: Tue Feb 24, 2009 5:59 pm

Raycasting help

Post by nulldorf »

Hey guys! First post and glad to be here.

Love2D is awesome, but I have some questions. I've written a simple raycasting engine in Love, and now I'm working on adding textured walls. In order to texture the walls I have to compare the image for the wall and the pixels in the wall itself, figure out what goes where, and draw the appropriate color pixel to the screen. This means I'm using love.graphics.point(x,Y) about a bazillion times per loop, which is hella slow!

I've had a few ideas about how to speed this up, but don't know how to implement them:
One would be to create an array of [screenSizeX][screenSizeY] and assign the color values to each data point as I go through the calculations, then draw everything at once...but still means using point() the same number of times, and might not be any faster.

Another idea was similar to the above. Is it possible to create an image from raw data rather than loading it from a file and display it that way?

Or perhaps tap into the routines that Love itself uses to draw images?

I'm really psyched up about this project, but I'm stumbling pretty hard. If these ideas would work, or there is a much faster alternative i'm not thinking about please let me know!

As things progress I'll post screenshots etc and eventually the engine itself.

Thanks!
User avatar
Star Crunch
Prole
Posts: 33
Joined: Sun Feb 15, 2009 12:13 am
Location: Monterrey, MX
Contact:

Re: Raycasting help

Post by Star Crunch »

I'm not sure if this will be a big win, as the process itself sounds rather expensive, but in any tight loop in Lua the obvious improvement is, before the code in question, to do:

Code: Select all

local point = love.graphics.point
Assuming you haven't already done this, of course. In the love.graphics.point form this is doing three table hashes ("love" on the environment, "graphics" on the result, "point" on the second result) per iteration, whereas the above directly indexes a local.

You can get away without doing this all the time, but when it's "a bazillion times per loop"... :P
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Raycasting help

Post by bartbes »

Addition to the above, Lua also accesses a local WAY faster than a global, this is because of how it's implemented in C. I don't know how much you'll notice of that when just storing references, but anyway, I just had to say that :P
nulldorf
Prole
Posts: 3
Joined: Tue Feb 24, 2009 5:59 pm

Re: Raycasting help

Post by nulldorf »

Thanks for the tip. I wasn't doing that before. I changed the code and got a 50% fps increase when flush with a wall (so that the rendering algorithm is literally drawing every pixel on the screen) unfortunately 50% was a bump from 4 to 6. It's a start though.

I haven't worked with c since college, and then very little, but is it possible/feasible to write the drawing routines in c and rebuild everything myself? I'm not really sure how to do that :( but I guess that might be the way to go.

Anyway great advice and let me know if you have any more!
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Raycasting help

Post by osgeld »

i dunno if its any help, but heres the one i was fooling around with

its basically the one from brain damage lua (rip) with some texture mapping (badly) shoehorned in

it scales Y perfectly, X is a totally different matter and i haven't (recently tried) to resolve it , but its hella fast
Attachments
raycaster.love
(2.76 KiB) Downloaded 434 times
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Raycasting help

Post by osuf oboys »

nulldorf wrote:Thanks for the tip. I wasn't doing that before. I changed the code and got a 50% fps increase when flush with a wall (so that the rendering algorithm is literally drawing every pixel on the screen) unfortunately 50% was a bump from 4 to 6. It's a start though.

I haven't worked with c since college, and then very little, but is it possible/feasible to write the drawing routines in c and rebuild everything myself? I'm not really sure how to do that :( but I guess that might be the way to go.

Anyway great advice and let me know if you have any more!
This might be somewhat easier with next LÖVE version where you can prepare the image over a few frames and draw it efficiently, although with a limited number of updates per second.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
nulldorf
Prole
Posts: 3
Joined: Tue Feb 24, 2009 5:59 pm

Re: Raycasting help

Post by nulldorf »

Thanks for the advice! The raycasting sample posted above proved just how much of a begginer I am at Love. That code is quite elegent compared to mine...and a lot shorter. But progress is being made!

Rather than drawing every pixel, it is much easier to use love.graphics.draws to take a slice of a texture and then scale it. There are some problems I haven't worked out, but I got things kind of working, and with 50+ FPS. Sweet.

I'm working on this engine for the cockpit compo over at tigsource.com so I'll post screens in the next few days as I keep working.

nulldorf out.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Raycasting help

Post by osgeld »

good luck, and i cant wait to see what you come up with
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests