Log In  

Building on my previous ray casting toy I've been playing around with shadow casting.

Cart #difepoduko-1 | 2021-10-10 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

Old (30fps):

New (60fps):

Most examples of this I've come across work with tiles/on a grid. This works with arbitrary line segments with the following caveat: the more line segments, the worse the performance. This example runs okay at 30fps but not so much at 60fps.

Most of my time seems to be lost in the actual casting of rays.

Things I plan to do but haven't done yet:

  1. Currently I'm casting two rays, slightly offset, towards every line segment's end-points (after removing duplicates so I'm not casting at the same point more than once.) I figure it shouldn't be too terribly difficult to modify it so a ray which hits a line's end-point is extended to the next wall instead of stopping. Then I'd only need one ray per end-point cutting my total number of rays in half.
  2. Culling the geometry before casting so instead of casting at everything, I only cast at everything within a visible radius. Bonus, that'll also make it easy to only cast in the direction the character is facing instead of casting in all directions.

I do sort things a few times and I'm using bubble sort because it was easy to implement but I'm sorting pretty short lists so I don't think it's a huge time sink.

Suggestions on how to optimize further are very welcome!

Edit:
Optimized a couple things:

  1. Made casting rays more efficient by in-lining the wall/ray intersection calculations.
  2. Made removing duplicates more efficient by using a set-like data structure.
  3. There are enough line segments/end-points that a more efficient search makes sense so threw together a (probably pretty bad) merge sort.

The new version has approximately the same CPU usage at 60fps that the old version had at 30fps. I'm sure some optimization wizards out there can help me squeeze some more out of this.

P#98442 2021-10-09 20:41 ( Edited 2021-10-10 20:30)


[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-11-28 08:18:24 | 0.017s | Q:10