Main Menu

Wolf3D

Started by ScottB, December 16, 2024, 10:43:27 AM

Previous topic - Next topic

kevin

#15
RayCasting Wolf 3D Demo 3 -  Sprite Rendering

In this version of the RayCasting demo I've added sprite rendering to the game. 

Sprites in Wolf 3D style games can be tricky to conceptualize but really fit into a second rendering pass of transparent objects.  So first we ray cast from the players position out into the map to not only find what wall we hit, but the depth of that walls from the camera.  We'll use the depth later when clipping out sprites. 

I'm not actually sure how it's done in the original Wolf 3D game, but my approach was much like ray casting for the walls.  The moving sprites (objects) are stored in a list,  then each frame we run through the list of objects performing a basic culling pass.    In this demo the culling just checks the direction from the player (camera) to the target.  Then throws away characters that are outside of the cameras FOV (field of view)

This gives us a short list of character sprites.  In this version I store the distance from the sprite to the camera.  This is needed for two reasons,  it  gives us something to sort the short list of visible sprites upon and we also use the depth to clip the sprite against the raycast walls.

  For sorting i'm using a depth map.  It's basically a lossy tally sort,  where the code runs through the visible sprites, get the Z depth of the sprite, then uses that depth as index into a depth array.  The array is big enough to hold the max required z of the scene.  For each sprite we repeat the process.  Generally there's no collisions, by which i mean there's rarely two sprites at exactly the same depth on the same frame that share screen space.  but if thus does occur we look for a unused depths in the depth array and use that. 

  The final pass is to render the sprites in back to front order.  Since we have an array of all the possible depths (indexed from 0 to MaxDepth)  we just loop through that array backwards.  If the element at this position is zero we keep looping; if it's something else it's the sprite we need to draw. 

  The sprite drawing work by using the screen viewport and textured quad commands in unison.  Since the walls are drawn first, sprites might actually be in front of the camera but occluded by walls.

  So In order to draw the sprites we need to compute the sprites size and it's position on screen.  Then we run across the sprites X axis and check the depth of the sprite with the walls depth at each this location as we go across.  If the sprite is greater than the walls Z depth..  We ignore it,  if it's smaller we draw that strip.

  I've made a slight opt in the demo where the scanning routine checks for how many columns of the sprite are in front of the walls; then draws the visible section in one blit.   

Well #coders  that's pretty much how this all works...    Enjoy !




#WOLF3D #RETRODEV #LEARNGAMEDEV #SPRITES #RAYCASTING #BASIC  #BASICProgramming #StructuredProgramming #ProgrammingHistory #RetroComputing #EarlyComputing #ProgrammingEducation #TechSpeculation #CodingForBeginners #ProgrammingLanguages #StructuredCode #TechNostalgia #ComputerHistory #HobbyistProgramming #SoftwareDevelopment


Download RayCasting Wolf 3D Demo 3 Source Code

   Attached bellow


stevmjon

cool kev. did you want to include the code too?
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

ScottB

Nice work! but do you have source worked out texturemapping or is it a distance bug.
Looks great but I still see the texturemapping bug.
And Do you give up here or do we keep going?

Thanks,
ScottieBro

stevmjon

to fix the distance bug for texture mapping, there is 2 options:
1) increase the length of the ray, at line 596 increase variable 'Magnitude'
2) if no wall collision then draw nothing or black or a chosen color (effect like fog fade). add at line 616 by adding 'else'

also if you turn off the map (press M key) it speeds up the FPS.

   stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#19
Quote from: ScottB on January 15, 2025, 01:43:27 PMNice work! but do you have source worked out texturemapping or is it a distance bug.
Looks great but I still see the texturemapping bug.

  Well, that's outlined in the videos..

    See ->  What to do when Rays don't Impact Walls ? 

        ->  What happens if there's no ray intersection?


QuoteAnd Do you give up here or do we keep going?

    for what ?

kevin

#20
  Mocking up Wolf 3d floors....

  Clean the code up a little the other day,then I remembered didn't Steve write a floor mapping routine.. 

  Fest you eyes on the this beautiful creation...  ;) 

  And NO  they don't fit together in the demo.. Runs well though, so if the projection can be matched d.. you'll have a floors (and ceilings) .. 


  Related Articles:

    -- Mode 7 - Texture Mapped Floors


Download:

      --> Attached bellow