Hello people of the internet! It's your dangerously-handsome DOTS-developer here coming to you with a new development diary. YAAAY! I can hear the cheers all the way from the back-end of the Sanctuary engine. Today in the wonderful world of white noise, and blue screens I will be presenting the MVP and my hostage for the last couple weeks (and years of computer graphics research); Can you guess what it is? Yes, it is the sensational stripe that just keeps on going – truly a story worthy of the ages and not one, but ONE devlog. YES! It's the RAY!
Now, I am sure one of you silly sausages is wondering what this has to do with Sanctuary. “No way, FoneE, you implemented ray tracing” said Someone? and to that I say “No Someone, I did not.” But what I have implemented is all things physics detection and today let's talk about the essential ray and its big role in Sanctuary.
Now before we begin to dissect a ray infinitely, it's important to note that although I would love to break it down for all my little minions, the world is a cruel cruel place and so instead of explaining the origins behind the ray, here is a great article that gets straight to the point: What is a Ray? | Virtual Nerd. Of course, you are one of my superior minions and as expected, you don't need a tutorial level!
First we’ll break down the gravitas of rays. Sanctuary’s focus is having a simulated physics environment. What does that mean? That means everything is simulated, silly! Projectiles, units, and even the world we live in is all one big simulation! This is great because it allows some really interesting interactions within the game world which can be very crucial and very fun for a Real-time Strategy game. Just imagine taking out a bomber just so the bomber could crash upon your precious head. Oh the irony. Of course, the downside to making such a dynamic world is we must now solve some of life's biggest problems – just how close am I to that tank!
“But wait,” Someone loudly cuts in, “we have many ways to solve this, like the distance formula!” Someone, stop interrupting me! You were supposed to perish in the first paragraph. Because Sanctuary's environment is simulated, representing things as points is no longer applicable. Just take this point for example. Scene!
This wonderful art was drawn by owlieart! Please give them support for taking this devlog to a whole nother level.
Queue, Big Bertha – a T4 behemoth who is the physical embodiment of all the games you’ve spent losing. Her volume would humble the moon. Now roll in: T1 Mosquito, he was once relevant but since the 25 minute mark he has since felt the solace of insignificance. Long forgotten rally points result in an endless crusade of… walking into point defense. :( You can do it my little SPAM, you can do it!
Remember to show your patronage to owlieart!
If we were to represent the distance between these two units, we would quickly find ourselves realizing that points don't really represent the distance between these units. In fact, representing units with volume (i.e. colliders) – especially with large volumes and unique shapes – with points really breaks down the realism we strive to achieve.
So how does this problem get solved using rays? You’ll likely have figured it out but: Rays allow us to calculate the distance between different volumes. Furthermore, the versatility of the ray means we can intersect a whole plethora of shapes (thus why our shields are able to take on ellipsoidal shapes!) with extreme accuracy and efficiency.
One awesome thing which comes with using rays as tools of intersections is that the distance of these intersections are calculated without the need to use any expensive operations. Furthermore the fact that we return the real distance means we can skip steps we would normally have to do. (like distance formula between the ray origin and ray hit point) Skipping having to square our distances means less calculations and less having to represent our distances in distances squared. - Someone
Really walking the edge of the ray is the perfect path that just keeps on giving. Wait… who said that?
And although we can sum the functionalities of our raycasting four points, these four points cover the fundamental bases for creating a majority of what we need for a simulated world in Sanctuary:
Allow us to precisely measure the distance between things with volume.
Allows us to detect collisions between objects and projectiles.
Allows us to query if colliders are occluded. (i.e. stopping Area of Effect damage from going through shields/objects and hitting units behind.)
Allows us to raycast in our world! (Duuuh!)
So, how was the ride? Did you pass the vertical line test? “No, yea, no…” What does that even mean?!? Anyway, hopefully in the future I will be able to post more of these Development Diaries™ but I need your feedback! What topic do you want to see covered next? Would you like me to go into greater detail, maybe even pulling out some real code or was this just right? Maybe you recently tried broccoli and need a place to express your disgust (you have my condolences). If “Yea, no, yea” feel free to head over to the Sanctuary Discord and message me at FoneE#1533.
Anyway, this has been your OfficialFoneE, signing off.
P.S. For my developers out there who are even more curious about the inner workings of the raycasting system–how we achieve this and that–I will leave some great links to some of the most lifesaving and important resources needed to build one yourself! However if you need MORE feel free to message me on the Sanctuary Discord with my handle FoneE#1533.
Useful links for anyone trying to raycast themselves:
A great introduction to a speedy algorithm to partition your world for Raycasting. Super Fast Ray Casting in Tiled Worlds using DDA - YouTube
This link is of a paper that implemented a 3D version (as well as some optimizations) of the DDA algorithm above. (PDF) Efficient implementation of the 3D-DDA ray traversal algorithm on GPU and its application in radiation dose calculation (researchgate.net)
An insanely useful article that covers ray intersections on a plethora of volumes. I worship this page. Thank you so much Inigo. Inigo Quilez :: fractals, computer graphics, mathematics, shaders, demoscene and more (iquilezles.org)