Ray Casting Tutorial

 
Misc Post by Matt 2 years ago. Read 6325 times

Hi,

 

This is the first of hopefully many little tutorials I'll be writing. These tutorials are not only for developers and aspiring game creators, but also those of you who are a little curious about the inner workings of your favourite games.


This tutorial is about an old school method of 3D rendering called ray casting. Games which most notably use ray casting are Doom and Castle Wolfenstein. An example of what this tutorial will show you how to do can be found here.

 

I quickly want to mention that a far more in-depth analysis of ray casting can be found here: http://www.permadi.com/tutorial/raycast/. This is was I used to learn so it's definitely a good place to expand on what I am going to try and teach here. So, on with the tutorial!

 

Classic ray casting: Doom


The theory behind ray casting is simple. Take a 2D grid, add a point (your player) and trace a designated amount of lines from that point measuring the length of each line until it hits a wall. When you have an array of line lengths, you can convert this into vertical slices on the screen. The more slices you have, the more accurate the 3D render.

 

This has probably already got you very confused, so let's break it down into digestible chunks!

 

  1. Draw a grid of squares, I chose 64x64 pixels per square. In the grid define each square as being a wall or not.

  2. Draw a point. The point can go anywhere on your grid, except in a wall square. The point also needs to have a direction it is facing (0-360 degrees)

  3. Trace 80 lines from -30 degrees of the players direction to +30 degrees. For each line record the distance until the line hits a wall square.

  4. Take those distances and draw a vertical line, 4 pixels wide, on your screen for each.

 

AS2 source

 

It's all well and good breaking these steps down, but obviously it's more difficult to actually put into practice! You can download the source of the example file here. This should shed some light on the ins and outs!

 

If you have any questions which aren't answered in the source files, comment below and I'll be happy to answer any queries!

 
Sign up to Rusty Arcade Today!
 

Comments

 
Tired of filling all this in? Sign up for a full Rusty Arcade account.
Or please log in if you already have an account

Won't be public

#
LenaSloan31 says:
I took my first <a href="http://goodfinance-blog.com/topics/business-loans">business loans</a> when I was very young and it supported me a lot. However, I need the secured loan once again.
Posted 1 week ago | report
 
#
forum posting services says:
We always provide the best forum posting service. We mastered the most workable submission plans. With us your internet site will be more workable!
Posted 5 months ago | report
 
#
essay writing service says:
You need to use all power of mind. Nonetheless, that is more reasonable to find essay writing service.
Posted 5 months ago | report
 
#
college papers online says:
That doesn't mean that you plagiarize when you buy essay paper! It just means that you want to get a success!
Posted 5 months ago | report
 
#
free ringtones says:
Oh, I have watched such really good release on some buy ringtones or just cellphone ringtones proposing company. This was a great performance, thanks a lot.
Posted 5 months ago | report
 
 
 

Author

 
#

Matt

111 exp
772 RA Tickets  76 Achievement
 

Games Mentioned

 

Ray Casting Tutorial Example

Play Plays
3896
Score
3.04
 
 
 
 
 

More Recent Blog Posts

 
Read

Bullet Bill 1 & 2 Passwords

2 months ago by cicla
Bullet Bill 1 and Bullet Bill 2 level and character Passwords
(1 comments)
Read more...
Read

Easier Achievements so far

5 months ago by cicla
A detailed list with the easier achievements so far in rustyarcade
(7 comments)
Read more...
Read

London Riots : The Game Guide

5 months ago by Matt
Can't beat the police? Check out this London Riots : The Game guide
(1 comments)
Read more...
Read

Neopets Hasee Bounce

8 months ago by Matt
There are so many games to play online and one of the cuter ones is Neopets Hasee Bounce
(2 comments)
Read more...
Read

Mecha Martyr 2 Cheat and guide

1 year ago by cicla
Mecha Martyr 2 Cheat and full guide walk trough
(17 comments)
Read more...