Share Your Code

Texturemapped Raycasting Engine

Posted by Jesder, Posted on March 10, 2012

As a follow up to my original code share of the Corona port I did of my 2D raycasting engine, texture mapped walls have been added.

This was achieved using sprite sheets. The downside of this is the frame rate takes quite a hit compared with the flat filled walls shown in the original sample. I know I can optimise how I use sprites in this demo, but I still think more needs to be done to get the frame rate up - that may mean using a different approach than sprites.

There is a mode button at the top left, which allows you to switch between textured and flat filled walls. The boxes on the left control your camera direction, and the two on the right control backwards/forwards movement.

Rather than relist all the code from before, I will just link it from my site. Most of the code remains untouched, outside of moving files into sub folders, and the changes to the render system. There is also a bug fix included in the raycasting engine to fix the texture offset returned for each wall slice to draw.

You can download the full source from here

Questions and comments always welcomed :)


Replies

Sunny
User offline. Last seen 2 days 10 hours ago. Offline
Joined: 20 Oct 2009

impressive work !
thanks for sharing !!

horacebury
User is online Online
Joined: 17 Aug 2010

This is really incredible work. I think you are crossing the line which we all hoped could be crossed but couldn't see how to.

I assume the frame rate would be back up there if 2.5d image manipulation were to be worked on by Ansca? Though the 3D code would be significantly different....

Danny
User offline. Last seen 32 weeks 19 hours ago. Offline
Staff
Joined: 17 Aug 2011

Amazing work, very impressive.

Have you tried this on the latest daily build? The new automatic culling feature of offscreen objects may help tremendously with the framerate

Jesder
User offline. Last seen 2 years 4 weeks ago. Offline
Joined: 10 Dec 2011

Thanks for the positive comments guys :) The feedback i've been given on my previous demos/samples has actually kept me going to go 'one better' and try to improve them, or come out with something even better.

I have just updated the raycaster to v1.2, which has improved usage of sprites, giving the demo a frame rate boost. I also found 1 extra scan line was being calced every update, so that too offered something of a frame rate boost (although minimal).

You can get the updated version from the same link.

@Danny: I did see the new improvements released and spent some time in the blog reading up on them. It was funny timing actually since I had just been playing with sprite sheets, and now they are deprecated with something better. Although I was happy to see I can adapt easily.

I can't grab new build though since I'm not a subscriber. I'm holding off on that for now as I would like to see solid offline documentation available (I do most of my Corona coding without net access, so need offline docs to assist).

canupa.com
User offline. Last seen 15 weeks 20 hours ago. Offline
Joined: 20 Jun 2011

>> I'm not a subscriber

Ansca Team! Give that man a free subscription for being an awesomely nerdy contributor!

doubleslashdesign
User offline. Last seen 8 hours 26 min ago. Offline
Joined: 27 Nov 2010

Very sweet GREAT Job!!!

GuGuGames
User offline. Last seen 43 weeks 8 hours ago. Offline
Joined: 10 Jul 2010

Good Job!
thanks for sharing !

Marco

nicholasclayg
User offline. Last seen 1 week 3 days ago. Offline
Joined: 16 May 2011

HOLY CRAP.

That means I like it. I feel like I am playing Wolfenstein 3d haha. It's amazing this can happen in Corona. Crazy Crazy Crazy!

Way cool. I don't understand the logic looking at it (I looked at the code and my brain went nuclear shutdown) but I can kind of see how it's done....barely.

You sir are highly skilled.

-ng

canupa.com
User offline. Last seen 15 weeks 20 hours ago. Offline
Joined: 20 Jun 2011

I just tested this on the iPad3.
It runs at an acceptable speed - I can absolutely imagine a 90s-style dungeon crawler using this ;)

skolesnyk
User offline. Last seen 2 years 20 weeks ago. Offline
Joined: 26 Jul 2011

Did you test it using latest daily build?

Satheesh
User offline. Last seen 1 day 18 hours ago. Offline
Joined: 25 May 2011

WOW! That's all I can say!

The only problem is understanding the logic behind!

Seriously, Hats off!

RandomGuy73UK
User offline. Last seen 5 weeks 1 day ago. Offline
Joined: 7 Jun 2011

Hello Jesder,

What are the licensing terms for your ray-cast work?

I think some people are studying this and will happily make use/re-use commercially if allowed.

Thanks in advance for your answer and keep the good vibe going.

Giuseppe

Jesder
User offline. Last seen 2 years 4 weeks ago. Offline
Joined: 10 Dec 2011

@Satheesh: Here is a link to the same documentation I learnt from almost 15 years ago: http://www.permadi.com/tutorial/raycast/

The theory behind raycasting is actually quite simple when you get down in to it - especially when it's only dealing with 2 dimensions. Check out the link if you are interested!

@Giuseppe: Contact me at andrew.burch at newhighscore.net. If you are interested in using it for a project, I'd love to hear more!

Thanks everyone for the positive feedback - glad to see people have taken a strong interest in it :)

Satheesh
User offline. Last seen 1 day 18 hours ago. Offline
Joined: 25 May 2011

@Jesder Thanks!
Will do some reading.

RandomGuy73UK
User offline. Last seen 5 weeks 1 day ago. Offline
Joined: 7 Jun 2011

@Jesder

Thanks a lot for your link at the tutorial, that's really what I was looking for!

I'm just planning to play a bit with ray-casting and make a small, fun game with 2D Corona by combining 3-4 environments to get a maze.

I understand I need to plan a bigger matrix and sketch a map, we will see what happens with performance.

Thanks again and keep going! Giuseppe

canupa.com
User offline. Last seen 15 weeks 20 hours ago. Offline
Joined: 20 Jun 2011

@Jesder awesome link! I can see clearly now the rain has gone :D

RandomGuy73UK
User offline. Last seen 5 weeks 1 day ago. Offline
Joined: 7 Jun 2011

Ok, started putting my head into this great work from Jesder and the tutorial he put the link to.

The first remark I can do is that Jesder's solution it is very textMemUsage consuming at the moment.

In fact, by putting the well known Ansca memory checker at the end of his main.lua:

1
2
3
4
5
6
7
8
9
10
11
local function garbagePrinting()
        collectgarbage("collect")
    local memUsage_str = string.format( "memUsage = %.3f KB", collectgarbage( "count" ) )
    print( memUsage_str )
    local texMemUsage_str = system.getInfo( "textureMemoryUsed" )
    texMemUsage_str = texMemUsage_str/1000
    texMemUsage_str = string.format( "texMemUsage = %.3f MB", texMemUsage_str )
    print( texMemUsage_str )
end
 
Runtime:addEventListener( "enterFrame", garbagePrinting )

and launching the app in the simulator, I can read:

memUsage = 719.371 KB
texMemUsage = 67.584 MB

The latest value is clearly too much for older mobile devices equipped with 128 MB Ram. I'm going to have a try with my 256 MB iPod Touch 4g as soon as possible. The latest CORONA SDK package only deploys to iOS 4.3 and following, so we are at the lower limit just now.

Considering that I want to populate this environment in order to make a game out of it, it appears that the critical value to look at is that texMemUsage value.

Well, going to have my tries and then reporting here. THanks again to Jesder.

Any people working on that?

stephenTU
User offline. Last seen 1 year 8 weeks ago. Offline
Joined: 4 Sep 2011

Thanks for sharing. I hope to create something as fantastic as this. Thanks.

PValentini
User offline. Last seen 10 weeks 6 hours ago. Offline
Joined: 21 May 2010

in a ipod touch 4 work so SLOW that I can say that don't work...
it is working on android or others devices?

krishnarajsalim.
User offline. Last seen 11 weeks 2 days ago. Offline
Joined: 23 Mar 2012

Really awesome work Jesder.
Thanks for sharing.