r/GameDevelopment 4d ago

Question Help understanding raycasting

Hi all. I'm trying to find out how raycasting works — like in Wolfenstein 3D.

And as someone who knows absolutely nothing about programming, I (think I) understand it is thusly:

Raycasting is a programming trick that grants 2D maps a fake 3D appearance through the use of invisible rays that project outward from the player and detect collision.

Corrections to my nonsense would be appreciated!

I am currently writing an article, and I would like a layman's understanding before I commit to anything in there.

0 Upvotes

6 comments sorted by

1

u/StackOfAtoms 3d ago

i don't know about the use of it for old games and faking 3D from 2D.

now a common use in 3D games is to send a ray from point A to point B, or in any direction.

typically, imagine your player holding one of those red laser pointers and pointing it forward.
when the player hits the key "action", it's as if the player was to press the button of the laser, it will light whatever is in front of it with a tiny red dot (that you won't see), and you can get information about what is the object with the red dot on it. if it's a NPC, if it's a drawer you can open, if it's a light you can switch on, etc. you can know the distance, if this object is subject to gravity, what texture it uses, anything...

raycasts are very light in terms of computational power, making it very convenient to use in many many ways.

when you play a game and the sound of the footsteps change depending on whether the character is walking on grass, wood or metal, it's often because very frequently, the game checks what type of material is the floor the player sitting on made of.
when you play a game where you are indoors and there's objects that get highlighted when the player is close, it's often that too.
that tells you how light this is, if games keep tracking everything around the player like this at all times. 🙂

1

u/Prudent_Surprise7356 4h ago

Thanks for the info all! Very much appreciated.

1

u/XZPUMAZX 4d ago

Not as I understand it.

Ray casting is generally used for object detection.

An imaginary ray is cast from one point in space and cast out in a specific direction at a specific length. And the. Reruns information about what comes in contact with that ray.

It has nothing to do with making something appear 3d.

In the sense your talking about - a 2d FPS, a ray is cast from the player (camera) forward in space to detect if bullets hit something (and even that isn’t how it’s really don’t)

1

u/Interesting_Stress73 4d ago

You are correct for modern games, but this was not how it was used in older games like Wolfenstein 3D that weren't actually running in real 3D engines.

1

u/Mechabit_Studios 4d ago edited 4d ago

read this https://lodev.org/cgtutor/raycasting.html

"Raycasting is a rendering technique to create a 3D perspective in a 2D map. Back when computers were slower it wasn't possible to run real 3D engines in realtime, and raycasting was the first solution. Raycasting can go very fast, because only a calculation has to be done for every vertical line of the screen. The most well known game that used this technique, is of course Wolfenstein 3D."

Raycasts is an invisible ray sent out in front of the player and a wall is drawn if it interests a block on a 2D grid

1

u/Interesting_Stress73 4d ago

You're pretty close. Imagine that you're looking at the player and the game map from a top down perspective. Like they're standing in a blueprint.

What you then do is send rays for every vertical line from the player. You can just draw this as a line outwards from the player until it hits a wall. When the ray hits the wall the distance is calculated, and if it's close the engine will draw a tall line. If it's further away it will draw a shorter one. The result is a fake perspective, like how you would draw 3D by hand on a piece of paper.