QB64 Phoenix Edition
Fun with Ray Casting - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: a740g (https://qb64phoenix.com/forum/forumdisplay.php?fid=56)
+---- Thread: Fun with Ray Casting (/showthread.php?tid=3372)

Pages: 1 2


Fun with Ray Casting - a740g - 01-13-2025

It all started with this post.

This is an implementation of the Voxel Space ray casting in QB64-PE. The algorithm is explained here.

You'll need QB64-PE v4 to compile and tinker. This is just a demo, not a complete game. At best, it can be called a walking simulator. Big Grin

The demo is configured to best perform for my Ryzen 5600x system. If you see low FPS, tweak the RENDER_DISTANCE and RENDER_DELTA_Z_INCREMENT constants at the top of the source code.

Code: (Select All)
CONTROLS:

KEYBOARD:
   FORWARD:        W / UP
   BACK:           S / DOWN
   STRAFE LEFT:    A
   STRAFE RIGHT:   D
   TURN LEFT:      LEFT
   TURN RIGHT:     RIGHT
   NEXT MAP:       SPACE

MOUSE: MOUSE LOOK

Mouselook may be broken on Linux because of how _MOUSEMOVEMENTX/Y is implemented in QB64. We'll fix this soon.

Have fun. Cheers!

[Image: Screenshot-2025-01-13-063920.png]

Adding my original solid-wall RayCaster here for completeness. I'll keep posting other ray-casting-related stuff in this thread in the future.

[Image: Screenshot-2025-01-13-063807.png]


RE: Fun with Ray Casting - Bhsdfa - 01-13-2025

Code: (Select All)
Type Player
    position As Vector2f
    height As Single
    camera As Camera
End Type


I didn't know that making a type variable as a type was even possible :o


RE: Fun with Ray Casting - a740g - 01-15-2025

(01-13-2025, 01:45 AM)Bhsdfa Wrote:
Code: (Select All)
Type Player
    position As Vector2f
    height As Single
    camera As Camera
End Type


I didn't know that making a type variable as a type was even possible :o

Yes, it is. What's strange is that this is allowed in a case-insensitive language. This isn't new to QB64, but rather something that has been possible since the days of QuickBASIC (even older I think). I simply adjust the variable name and type name case to make it look a bit nicer.


RE: Fun with Ray Casting - SierraKen - 01-15-2025

WOW!!!!!! I remember years ago someone made something similar to this, but there were over 1000 lines, maybe many 1000. Thanks for showing me this. I did notice that the Esc key doesn't work for some reason. You might want to use LOOP until inkey$=chr$(27) or .... 
chr$(27) is Esc on IBM compatibles. 
I don't know if I can use this code, but it sure amazes me, I'm just not to that level of programming. But I know others here are I'm sure. Thanks for posting this.


RE: Fun with Ray Casting - NakedApe - 01-15-2025

Killer!! That's like Mindcraft in 400 lines. Very impressive. I can't wait for more enhancements...  Exclamation


RE: Fun with Ray Casting - Petr - 01-15-2025

Thanks for sharing. I don't understand the GetHertz function at all, SUB RenderFrame is very interesting and the placement of the heightmap and surface map in the form of a three-dimensional array is simply brilliant. Thanks for sharing. I also found a few new commands there that I might not have known about. It's a very nice job.


RE: Fun with Ray Casting - a740g - 01-16-2025

(01-15-2025, 07:45 PM)NakedApe Wrote: Killer!! That's like Mindcraft in 400 lines. Very impressive. I can't wait for more enhancements...  Exclamation
Thanks! Smile 

Minecraft is way more advanced. Aaditya and Petr did some awesome work:
https://qb64phoenix.com/forum/showthread.php?tid=2875
https://qb64phoenix.com/forum/showthread.php?tid=1565


RE: Fun with Ray Casting - a740g - 01-16-2025

(01-15-2025, 08:02 PM)Petr Wrote: Thanks for sharing. I don't understand the GetHertz function at all, SUB RenderFrame is very interesting and the placement of the heightmap and surface map in the form of a three-dimensional array is simply brilliant. Thanks for sharing. I also found a few new commands there that I might not have known about. It's a very nice job.

Thanks!

Maybe the GetHertz function name is misleading. It calculates the number of times it is called in a second when called repeatedly inside a loop. In this case, we get the FPS.

In the latest version, I made some optimizations, added auto-LOD, and integrated the FPS calculation. I'll update the zip after work.

In the meantime, it can be found here: https://github.com/a740g/QB64-PE-Museum/tree/main/a740g/VoxelSpace


RE: Fun with Ray Casting - MasterGy - 01-16-2025

OMG!! I tried it! This is incredible!! Such a short code and such a beautiful 3d drawing...congratulations! I really like it!


RE: Fun with Ray Casting - MasterGy - 01-16-2025

Sorry, I don't want to trash your code Smile I just got carried away because I really like it and studied it a bit.
If you rewrite this in 'Renderframe', the landscape will darken proportionally to the distance and improve the 3d experience a bit. The bigger the ".001", the bigger the effect. At ".01" it's like walking at night with a flashlight.

Code: (Select All)
 
If heightOnScreen < hiddenY(i) Then
  Dim col As Long
  col = map(MAP_COLOR, mapPos.x, mapPos.y)
  Dim multi As Single
  multi = 1 - Abs(z) * .001
  Line (i, heightOnScreen)-(i, hiddenY(i)), _RGB32(_Red(col) * multi, _Green(col) * multi, _Blue(col) * multi)
  hiddenY(i) = heightOnScreen
End If