QB CULT MAGAZINE
Issue #4 - June 2000

RAYCASTING IN QBASIC

Writer: William Moores

Hello! This is my first ever tutorial and lets hope it's a good one! Wanna make a game like DOOM? Well, with this tutorial, I will explain the basics of RAYCASTING, which will, hopefully, lead on to greater things... The concept of raycasting is actually very simple, imagine you had a laser pointer, which had a little screen on the back saying the colour and the distance of the thing you where pointing at. Well, raycasting is just that! (I worked all this out on my paper round! ^_^) Anyway, in QB, this is much easier than it sounds! In this tutorial, I will explain how to make a Doom style demo. Firstly, for our doom demo we would need a map. Like this:

DIM map%(1 TO 20, 1 TO 20)

As you can see, this would make a little level that was 20 * 20 blocks big. Each array element is a wall in our demo (eg. map%(3, 4) = 10, this would make a wall at 3, 4 be the colour ten). "This is just 2D rubbish!" I hear you say? Well, yes and no. In the demo it doesn't have any true 3D calculations, but it looks 3D. Anyway, before we start our 3D demo, let me show you a program that demonstrates a 3D veiw, which isn't actually in 3D, but it does help explain raycasting... The demo is a top veiw of our map, and it has a little cone that you can rotate and move. Well, in our 3D demo, this is the 'camera', the 2D one shows the camera and its RAYS coming out of it.

The 2D demo is <2DRAY.BAS>

Before we go any further, I'll explian the RAY bit. In a raycaster, the 'camera' emits rays, and when the ray hits a wall in our map, the 'camera' returns:

And the ray stops going.

Another way to explain this. Imagine you had a radar, the radar emits radio waves, the radar detects the radio waves that have bounced of the wall that they have hit, somehow(I don't know how) the radar calculates how far away the solid is that the radio waves have bounced of.

When you have understood all that, lets make our 3d(ish) camera! Remember that just now we made a map (DIM map%(1 to 20, 1 to 20)). Well, the camera is in the 2D demo as well, but the program doesn't draw in 3D (style).

<3DRAY.BAS>

All our 3D demo does is this: Instead of drawing the ray, it draws a lines along the screen corresponding to the ray it's fired (if you looked at the 2D demo, it sorta 'scans' along with lots of rays). The colour of the line is of what it's hit. To make it look 3D, we take away the distants of what it's hit from the vertical length of the line. This gives a very good effect. The only problem with this method is that you get a 'fish-eye' effect on your view. If you want you could work out a rule that gets rid of it.

I admit that the demos are slow, but you can easily modify them to suit you needs. If you don't quite understand this tutorial, study the examples hard. (If you want that is)

Now with your new knowledge of RAYCASTING, you could do some really cool stuff! I hope you enjoy your new skill and improve your knowledge of it, further than mine. Think about it, you could add lights! All you would have to do is the opposite of a camera! Instead of reading the colour, you could write the colour!