gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Looking for explanations of Raycasting.

#18753 - zazery - Sat Apr 03, 2004 10:22 pm

I've searched the forums, the site and other websites. I have read the theories on: http://www.permadi.com/tutorial/raycast/raycbb.html
However I can't seem to grasp the actual code needed to create one of these. I've got the code from the file section on this website (raycaster 0.7) and find it abit more confusing that I want it to be.

I understand tiles, sprites, game logic and various other things to do with 2D games. However I can't grasp raycastinig. I looked at the build engine but obviously that is large complex and confusing. I am looking for some websites with concepts so I can understand what is happening and also very basic code to run either on the PC in C or on the GBA in C.

I did look at Raycaster 0.7 in the files section as I said however I just want some little code that will teach me how to create the basic raycaster. Something like maybe 4 "blocks" with walls surrounding:

1111
1001
1010
1111

Any help would be appreciated.

#18804 - niltsair - Mon Apr 05, 2004 7:52 pm

I programmed one using this exact tutorial.

It's quite sufficient to build yourself a basic raycast engine.

You should create the basic routine in a Windows program :
1- Create base rendering loop, unoptimzed.
2- Once everything work(albeight slowly) Optimize it (use a trigo table insted of functions, etc...)
3- Replace all of your float/double by fixed mathematic.
4- Port your code to Gba.

For the Gba, use mode 5 with the Bg rotated 90degree and scaled 2x in height : (http://www.gbadev.org/files/newmode.txt) This will give you 16bits mode, with backbuffer.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#18816 - zazery - Tue Apr 06, 2004 1:10 am

I will try using Mode 5 and that tutorial as my basis along with the example on the GBA. However I don't understand one thing. When you are tracing each ray, does that account for one pixel on the screen? I'm not entirely sure how and what I should be drawing to the screen. I've had lots of experience with modes 0-3, 4 not so much and 5 not at all. Putting pixels in their proper place is not a problem, I just don't know how to determine what to put where.

I'm under the impression that each ray is a line and you draw all the lines. However I'm thinking that it should be casted pixel by pixel, am I right?

Thanks

#18818 - tepples - Tue Apr 06, 2004 1:49 am

In a Wolfenstein style raycasting engine, each ray corresponds to a column of pixels on the screen. A mode 3 or mode 5 double-buffered full screen has 120 such columns.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#18829 - niltsair - Tue Apr 06, 2004 4:21 pm

Like Tepples explained, you cast 120 'rays' untill you hit a 'wall'. Each ray is a column, and the height of the column depends on the distance it took to hit the wall. You basicly treat it like a 2D map, from overhead then deduce the height from the distance.

In my previous post, I said you scale in height, my bad, it's in width.

The advantage of using the mode 5 rotated/scaled is that :
1-You get 1/2 rays less to calculate (120 columns instead of 240).
2-You can write directly 2 consecutives pixels when drawing you column. (since BG is rotated 2 adjacents pixels are not on the same line, they're on the same column.)
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#18838 - zazery - Tue Apr 06, 2004 11:27 pm

Alright now it's starting to make more sense. Thanks to both niltsair and tepples for clearing up some questions I had.

I'll be working on this and another small game over the long weekend so I'll let you guys know how I make out and post any questions I have.

Thanks again, wish me luck.

#18846 - niltsair - Wed Apr 07, 2004 1:23 am

Good luck :)
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#19537 - corranga - Wed Apr 21, 2004 5:42 pm

niltsair wrote:
I programmed one using this exact tutorial.

You should create the basic routine in a Windows program :
1- Create base rendering loop, unoptimzed.
2- Once everything work(albeight slowly) Optimize it (use a trigo table insted of functions, etc...)
3- Replace all of your float/double by fixed mathematic.
4- Port your code to Gba.

For the Gba, use mode 5 with the Bg rotated 90degree and scaled 2x in height : (http://www.gbadev.org/files/newmode.txt) This will give you 16bits mode, with backbuffer.



There is a LOVELY little API (well, really a sort of wrapper clas for DirectDraw I suppose) that I used when working on my 3D stuff. It is called TinyPTC and I would recommend it tgo anyone wanting to try anything for GBA. It allows you to set up a window with only 1 or 2 lines of code, and drawing to a buffer is done by inserting colour values into an array - just like the GBA! but with the additional use of all things windows programming allows (so you have floating point numbers, and a console window for output if yuo make a console application)

I actually used exactly the same rendering code with this as I do on the GBA (with a slight change to the popular RGB16 macro)

Get it from: http://www.gaffer.org/

Chris
_________________
If virtual reality is ever on a par with reality, I want to be Bomberman! :D

#19556 - zazery - Wed Apr 21, 2004 11:36 pm

It sounds interesting. I'll check it out later tonight. I haven't had time to start on the code. I read through some documents and stuff but I just need to apply it now. I'm currently concentraiting on microchip programming until that project is done, which should be by May 1st. After that I will attempt this project again. Thanks for the comments and help.