RayCaster

RayCaster

by ADDmonkey55

👁 47 ❤️ 0 ⭐ 0 🔄 0
Created: Jan 23, 2021 Last modified: Feb 2, 2021 Shared: Jan 23, 2021

Description

~:~ Best to view this on turbowarp.org/477817645 My Ray caster that supports angled walls. So I learned about Ray casting from Youtuber 3DSage (youtu.be/gYRrGTC7GtA), and I did a little bit of studying up from Lode's tutorial (lodev.org/cgtutor/raycasting.html). I wrote my own code in OOP pseudo code, but I legit couldn't figure out how to draw pixels (I'm still learning non-Scratch coding). Then one day I came up with an idea for slanted walls ray casting... which is actually not very efficient speed wise (calculations have to be done for each ray for each existing segment, as opposed to the common grid based algorithm where calculations are needed just for each ray regardless of how many "walls" there are). I'm sure this algorithm is fairly common, but I'm still proud that I figured it out. Basically for each ray cast, it checks where it would intersect each wall, then checks if that intersection is within the range of the wall, then just gets the closest intersection. I decided this was easier to implement here in Scratch than the Grid based algorithm (but I could be wrong...idk) When running in Turbowarp with Screen Width set to 480, it can maintain 60 fps up to about 100 segments, and starts to drop under 30 fps past 200 segments (dropping close to 20 fps at around 300 segments). Of course these numbers double when Screen Width is instead set to 240, which still looks just fine. There's still a lot I want to do to make this into a more fully fleshed out actual textured ray caster, which includes: TODO: ▸ Implement portals (an idea I've had for a while that will allow for non-Euclidean. I'm very excited for this!) ▸ Fix collision so it's not "sticky" (this is a lot harder than it should be...) ▸ Implement a Binary ray casting method (another idea I had to speed things up, and I'm sure it's been done before but I don't know what it's officially called) that will greatly speed things up (although there's a drawback). Basically cast a ray on both sides of the fov and then recursively cast a ray in the middle and keep dividing until 2 rays hit the same segment so we know what all the in-between rays will be. This limits islands (if you can see the same segment to the left and right of another segment, that latter segment may not be rendered at all). ▸ Implement textures. ▸ Add floor/ceiling textures. ▸ Add sprites ▸ Walls with transparency ▸ Different height walls ▸ Better gui for creating levels ▸ Other stuff for a full game ^_^ These things are just too complex (for me) to implement in Scratch. I want to carry this onto C#, which I am currently learning (more specifically Unity, which I know Unity can do 3D graphics anyway, but that's besides the point) LET ME KNOW WHAT YOU THINK!!!!

Instructions

~:~ Best to view this on turbowarp.org/477817645 CONTROLS: ▸WASD - Move ▸Left/Right arrows - Turn ▸Click & Drag (while in 3D view) - Turn ▸2 - Switch to 2D map mode ▸3 - Switch to 3D view ▸V - Show/Hide variables 2D MAP MODE: ▸ZX - Zoom in/out (about mouse pointer) ▸F - Toggle follow mode on/off ▸Click & Drag - Move camera (with follow mode off) ▸Hold Space to select a line segment. ▸While a segment is selected, you can: ▸Click & Drag - Move and end point ▸C - Recolor ▸0 - Delete Segment ▸Hold C (when not selecting) to: ▸Click & Drag - Create a new segment ▸G - Toggle Grid snapping on/off 3D MODE: ▸B - Change Background ▸Hold M to Save, Load, or Clear the current map. Saving and loading works via generated code. Simply copy and paste the code. (Saving will show a list that you can copy the text from. Pressing M will hide the list) ````````````````````````````````````````````````` EXPLANATION ON ADJUSTABLE VARIABLES: ▸Screen Width: How many rays are cast per frame. Turbo Warp can support the full 480 pixels. Scratch will start to lag past 30 columns. ▸FOV: Field of View. 60 looks natural and is used by most ray casters. 360 turns you into a floating eyeball creature whose pupil wraps all around. Have fun playing with extreme values! ▸Mouse Sensitivity: For turning with the mouse in 3D View. ▸Turn Speed: For turning with the arrow keys. ▸My Speed: Player's move speed. If it is set to anything over the player's radius, it is automatically set back to the radius to prevent clipping, unless the radius is 0. ▸Player R: Player's radius. Used for collision. Set to 0 for no collision. 3D VIEW: ▸Fisheye: 0 means no fisheye, or in other words fixes the fisheye effect that comes naturally with all ray casters. 1 will actually give you that default fisheye effect (and also makes 360 FOV look a bit better). Have fun playing with extreme values! ▸Height Ratio: Adjusts how high the walls are drawn in 3D view. ▸Shading Number: (for lack of a better variable name) Affects how far you have to be for the shading to go black on a wall. Basically, if this is set to 100 (which I've found to be a good value for now), then when you are 100 units away from a texture, the brightness will decrease by 100%. Keep in mind that not all wall colors will have a default value of 100 brightness, for example, a 0000ff (blue) wall will turn black once it's brightness is decreased by 100%, however a 000080 (dark blue) wall will turn black once it's brightness is decreased by only 50%. 2D VIEW: ▸Grid Spacing: Grid size for 2D map. ▸Arrow Size: How big you want the player's arrow to show (There is a hidden variable that essentially multiplies the overall speed, which automatically gets set to 3 when loaded in Scratch, but set to normal 1 when loaded in Turbo Warp)

Project Details

Visibility
Visible
Comments
Enabled

More Cool Projects

Project thumbnail
View Project

NumberBlocks Band Retro 421-430 (Each Sound)

by lucas_tiger

Project thumbnail
View Project

The Witness - A Hollow Knight Platformer (Mobile Compatible) v.03

by ScarletSpiderKaine

Project thumbnail
View Project

(DEMO) Six Hours with Scratch Cat 2 DE

by Tbug20LovesRedstone

Project thumbnail
View Project

beatmania IIDX: Tresury

by windowscj

Project thumbnail
View Project

Smooth Intro

by cs5122271

Project thumbnail
View Project

Coordinate Grid Battleship

by jgordon510

Project thumbnail
View Project

Goku Black and Exetior vs Goku and Vegeta (Part 1)

by TurboMechSonic

Project thumbnail
View Project

dynamic lighting

by RedMonster12

Project thumbnail
View Project

Sling Drift Hacked

by ZOIDS-KENJI

Project thumbnail
View Project

VSカーネル・サンダース ~ドナルドの戦い~

by _rannrannru-_

Project thumbnail
View Project

Pen Animation Maker + Demo

by bidulule

Project thumbnail
View Project

Ninja Impostor Platformer ✨Mobile Friendly✨ #platform

by Grobbyy

Project thumbnail
View Project

Oldblocks band 2

by paumung2021

Project thumbnail
View Project

FNF Nightmare of the Mushroom Kingdom - (Add Yourself/Your OC Singing) Triple Treat (v3)

by ourbupboi

Project thumbnail
View Project

Goomba Clicker

by DBJ210

Project thumbnail
View Project

3D Splatoon many features :mobile ok:

by sousoutnk

Project thumbnail
View Project

hard mode sans fight!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

by saaaaaanschaaaaaaan

Project thumbnail
View Project

★ 3 Years Dodge ★

by betelgeuse_17y

Project thumbnail
View Project

Sonic Movie 4 (2026) Trailer

by TailsTheFox5000

Project thumbnail
View Project

Griffpatch's Tower Defense HACKED!

by fidgetcube11

Project thumbnail
View Project

War game v0.6

by H1perT3sla

Project thumbnail
View Project

Scratch Translate

by H2010-Scratch

Project thumbnail
View Project

500 Ways to Hurt Evil Scratch Cat Part 1 (Remix & Continue | 2 / 50 Ways)

by Twin138956Scratch

Project thumbnail
View Project

hide and seek Online 4 V1.4 [#Games #All]

by maDU59_