Questions about Ray Casting

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Questions about Ray Casting

Post by DementedPurple » Wed Apr 26, 2017 1:08 pm

I want to make a demake of Wolfenstien 3d on the NES using CHR RAM. But I don't quite understand how the Ray Casting system works. How does it know what pattern of pixels to make? You probably have no idea what I'm talking about. What I'm trying to say is when you are looking at a cube from an angle, you have to have their be a slant in the edges to make it look 3-dimensional. How does it know what pattern to make? It doesn't have a physical camera, so I'm confused. I've never done anything related to rendering 3D.

tepples
Posts: 21755
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Questions about Ray Casting

Post by tepples » Wed Apr 26, 2017 1:40 pm

I strongly urge you to make 3D on a PC before you attempt to make it on the NES.

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: Questions about Ray Casting

Post by DementedPurple » Wed Apr 26, 2017 2:44 pm

Okay, could you give an example of how I would do it on a PC?

User avatar
tokumaru
Posts: 11469
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Questions about Ray Casting

Post by tokumaru » Wed Apr 26, 2017 2:48 pm

I can give you a quick rundown on how raycasting works, but implementing it on the NES (at playable speeds) will require a lot of cleverness and optimization of the basic technique. How good are you with trigonometry? Raycasting basically involves a lot of triangles whose sides and hypotenuses you have to find.

User avatar
dougeff
Posts: 2617
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Questions about Ray Casting

Post by dougeff » Wed Apr 26, 2017 3:07 pm

Article on setting goals...

https://www.psychologytoday.com/blog/co ... al-setting
Goals should not be so be so difficult that the [person] will fail to take them seriously or will experience failure and frustration.
People are happiest when they set small, achievable (yet challenging) goals.
nesdoug.com -- blog/tutorial on programming for the NES

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: Questions about Ray Casting

Post by DementedPurple » Wed Apr 26, 2017 3:10 pm

tokumaru wrote:I can give you a quick rundown on how raycasting works, but implementing it on the NES (at playable speeds) will require a lot of cleverness and optimization of the basic technique. How good are you with trigonometry? Raycasting basically involves a lot of triangles whose sides and hypotenuses you have to find.
I don't know anything about Trigonometry, I'm only in middle school. I'll try learning it on my own, though.

User avatar
Fisher
Posts: 989
Joined: Sat Jul 04, 2015 9:58 am
Location: -29.794229 -55.795374

Re: Questions about Ray Casting

Post by Fisher » Wed Apr 26, 2017 3:47 pm

Bisqwit has made some nice videos about programming.
This one is about creating a Doom-style 3D engine in C.
I hope it helps.

User avatar
tokumaru
Posts: 11469
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Questions about Ray Casting

Post by tokumaru » Wed Apr 26, 2017 3:49 pm

DementedPurple wrote:I don't know anything about Trigonometry
Then I simply can't explain how raycasting works in detail, since I'd have to teach you trigonometry as well. You need to at least understand what sines, cosines and tangents are, how they relate to the unit circle and their basic formulas.

I can still give you a basic overview of how things work, though. Like I said, raycasting is all about triangles:

Code: Select all

+----------------+----------------+----------------+----------------+
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                X WWWWWWWWWWWWWWW|
|                |                |              / |WWWWWWWWWWWWWWWW|
|                |                |            /   |WWWWWWWWWWWWWWWW|
|                |                |          /     |WWWWWWWWWWWWWWWW|
|                |                |        /       |WWWWWWWWWWWWWWWW|
+----------------+----------------+------/---------+----------------+
|                |                |    /           |WWWWWWWWWWWWWWWW|
|                |                |  /             |WWWWWWWWWWWWWWWW|
|                |                |/               |WWWWWWWWWWWWWWWW|
|                |           HYP /|                |WWWWWWWWWWWWWWWW|
|                |             /  |                |WWWWWWWWWWWWWWWW|
|                |           /    |            OPP |WWWWWWWWWWWWWWWW|
|                |         /      |                |WWWWWWWWWWWWWWWW|
|                |       /        |                |WWWWWWWWWWWWWWWW|
+----------------+-----/----------+----------------+----------------+
|                |   /            |                |WWWWWWWWWWWWWWWW|
|                | /              |                |WWWWWWWWWWWWWWWW|
|                /                |                |WWWWWWWWWWWWWWWW|
|         +---+/ | A              |                |WWWWWWWWWWWWWWWW|
|         | P |--+----------------+----------------+WWWWWWWWWWWWWWWW|
|         +---+  |                |  ADJ           |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
+----------------+----------------+----------------+----------------+
To draw a scene, you have to cast several rays away from the player at successive angles. When you cast a ray from player P at angle A, it hits a wall W at point X, forming a triangle. You need to apply trigonometric formulas to find the distance from the player to the wall (HYP), and use that information to calculate how tall that wall slice will appear on screen (greater distances result in smaller walls). Then you also need to find the opposite side (OPP), so you know the exact coordinate that was hit by the ray, in order to know what texture slice to use. Then you just need to do the actual scaling of the texture.

This is a very basic overview of the process, since each of these processes has to be broken down into smaller tasks. Casting the ray for example involves the use of smaller triangles, aligned to the grid:

Code: Select all

+----------------+----------------+----------------+----------------+
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                X WWWWWWWWWWWWWWW|
|                |                |              / |WWWWWWWWWWWWWWWW|
|                |                |            /   |WWWWWWWWWWWWWWWW|
|                |                |          /     |WWWWWWWWWWWWWWWW|
|                |                |        /       |WWWWWWWWWWWWWWWW|
+----------------+----------------+------/---------+----------------+
|                |                |    /           |WWWWWWWWWWWWWWWW|
|                |                |  /             |WWWWWWWWWWWWWWWW|
|                |                |/---------------+WWWWWWWWWWWWWWWW|
|                |               /|                |WWWWWWWWWWWWWWWW|
|                |             /  |                |WWWWWWWWWWWWWWWW|
|                |           /    |                |WWWWWWWWWWWWWWWW|
|                |         /      |                |WWWWWWWWWWWWWWWW|
|                |       /        |                |WWWWWWWWWWWWWWWW|
+----------------+-----/----------+----------------+----------------+
|                |   /            |                |WWWWWWWWWWWWWWWW|
|                | /              |                |WWWWWWWWWWWWWWWW|
|                /----------------+                |WWWWWWWWWWWWWWWW|
|         +---+/ |                |                |WWWWWWWWWWWWWWWW|
|         | P |--+                |                |WWWWWWWWWWWWWWWW|
|         +---+  |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
|                |                |                |WWWWWWWWWWWWWWWW|
+----------------+----------------+----------------+----------------+
The formulas necessary to do this have a lot of divisions and multiplications, things the 6502 is just not good at, so you'll need to be clever and find ways to optimize those as much as possible. In my engine I was able to turn all divisions into multiplications or table look-ups, and I significantly reduced the number of multiplications by combining multiple operations into a single step.

User avatar
pubby
Posts: 548
Joined: Thu Mar 31, 2016 11:15 am

Re: Questions about Ray Casting

Post by pubby » Wed Apr 26, 2017 3:56 pm

Christ dude, just make a pong game or something. You'll learn a lot more by actually doing things than posting questions on forums.

I'm cheering for you. :beer:

User avatar
tokumaru
Posts: 11469
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Questions about Ray Casting

Post by tokumaru » Wed Apr 26, 2017 4:17 pm

It's true, you do seem a little ambitious for someone who's so young and is just starting out with NES development. I'm an example of someone who has learned a lot of stuff over the years without releasing anything, but I wouldn't recommend that to anyone... I'd rather have released some simpler games! Try to set a simpler goal (not pong though, please, we have enough of those!) and go through with it.

User avatar
dougeff
Posts: 2617
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Questions about Ray Casting

Post by dougeff » Wed Apr 26, 2017 4:37 pm

I don't know anything about Trigonometry, I'm only in middle school. I'll try learning it on my own, though.
In that case, you should learn C# and try to make a game in Unity. Or use GameMaker, which I think, has it's own language, similar to Java (?).
nesdoug.com -- blog/tutorial on programming for the NES

User avatar
Drew Sebastino
Formerly Espozo
Posts: 3503
Joined: Mon Sep 15, 2014 4:35 pm
Location: Richmond, Virginia

Re: Questions about Ray Casting

Post by Drew Sebastino » Wed Apr 26, 2017 10:08 pm

tokumaru wrote:It's true, you do seem a little ambitious for someone who's so young and is just starting out with NES development.
12?!? I thought me being 15 when I signed up here was bad!

Yeah though, I hate to be the bearer of bad news, but starting out programming by building a raycasting engine (which I have no clue how to do!) will only lead to frustration. I don't think this is something you can really cut into steps where you can see your progress, unlike with a traditional 2D engine.

Just because you probably shouldn't do this doesn't mean you shouldn't avoid the NES. My first programming experience was actually with the SNES, although I didn't understand memory modes for more than my whole first year... I can't say for sure, but I heard learning higher level programming languages will not help you very much for learning assembly, because they are completely different. I know anything other than assembly or machine code still looks like gibberish to me.

Pokun
Posts: 1274
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Questions about Ray Casting

Post by Pokun » Thu Apr 27, 2017 1:54 am

I think being young and ambitious is a good thing though. I wish I started real programming when I was 12 or even 15, but computers weren't as common back then as they are now so it can't be helped.

Failed projects also makes you stronger, haha! But yeah I'd also say do something simpler first, but still something that interests you.

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: Questions about Ray Casting

Post by DementedPurple » Thu Apr 27, 2017 11:35 am

I mean yeah, That does seem kind of complicated and hard to do, but I don't mean to brag, but I've learned Java when I was 7. I could probably learn a lot doing this, But to keep it simple, I'll start with a ray-casting engine in C++. I don't see any harm in at least trying.

Rahsennor
Posts: 475
Joined: Thu Aug 20, 2015 3:09 am

Re: Questions about Ray Casting

Post by Rahsennor » Thu Apr 27, 2017 6:15 pm

DementedPurple wrote:I mean yeah, That does seem kind of complicated and hard to do, but I don't mean to brag, but I've learned Java when I was 7. I could probably learn a lot doing this, But to keep it simple, I'll start with a ray-casting engine in C++. I don't see any harm in at least trying.
I wrote my first game when I was 7. It was a silly little text-mode BASIC thing - no Java in those days! - but it had two players, an AI and win/loss conditions.

I wrote my second game when I was 23 and a half. I spent the intervening time doing exactly what you're doing - aiming for the stars and falling short of the clouds. I'm trying not to sound judgemental, and if that's what you want to do then by all means go for it - but if you want to make games now, you're on the wrong trajectory. Trust me, I've been there.

My advice: whip up something easy, say a roguelike. Doesn't matter if it's awful, doesn't matter if nobody else ever sees it, doesn't matter if you throw the whole thing out and start over. You can learn much, much faster by doing than by reading, and nothing I've tried quite lives up to playing your own game, thinking "wouldn't it be cool if...", and two hours of furious coding and testing later, having found and implemented something even better.

Post Reply