Building on my previous ray casting toy I've been playing around with shadow casting.
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:
- 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.
- 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:
- Made casting rays more efficient by in-lining the wall/ray intersection calculations.
- Made removing duplicates more efficient by using a set-like data structure.
- 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.
[Please log in to post a comment]