Ray casting in 2D Game Engines
last updated Jan 12, 2022
In my opinion, ray casting is a beautiful concept that is not that hard to grasp, but the quality resources are rare. We will learn the math behind it so you can implement it in your future projects with ease. I will try to make it as comprehensible as possible, explain all the caveats and issues you may stumble upon. We will also talk about optimization and how can spatial hash maps significantly help you. I will also provide some basic live examples for you to try out. Please note that demos were written to be as simple as possible, do not expect enterprise-grade code - we only learn about the concept, not the implementation.
What is ray casting?
Ray casting is the most basic of many computer graphics rendering algorithms that use the geometric algorithm of ray tracing. Ray tracing-based rendering algorithms operate in image order to render three-dimensional scenes to two-dimensional images... The idea behind ray casting is to trace rays from the eye, one per pixel, and find the closest object blocking the path of that ray – think of an image as a screen-door, with each square in the screen being a pixel. This is then the object the eye sees through that pixel.
— Wikipedia on Ray casting
Well, it does not tell us much, does it? Let me simplify that. Ray casting is a fundamental and popular technique used to determine the visibility of specific objects (polygons) by tracing rays from the eye (for instance, player’s hero) on every pixel (well, not quite in our case - more on that later) and finding the nearest intersections with objects.
Where might ray casting be used?
Ray casting has many uses, especially in three-dimensional space. I outlined three, in my opinion, the most important, which are commonly used in 2D game engines:
Creating 3D perspective in a 2D map
The most well-known game that used this technique is Wolfenstein 3D. Rays were traced to determine the closest objects, and their distance from the player position was used to appropriately scale them.
Point-in-polygon problem
PIP problem asks whether a given point lies inside, outside, or on the boundary of a polygon. Using the ray casting algorithm, we can count how many times the point intersects the edges of the polygon. If the number of the intersections is even, the point is on the outside of the polygon. If the number of the intersections is odd, the point is on the inside or on the boundary of a polygon.
Object visibility and light casting
This is the problem we will specifically tackle in this article - determining which objects are visible by the player and illuminating the visible area.
Object visibility and light casting in 2D games
In this section we will go through basics such as calculating intersection points, casting rays, sorting intersection points to illuminate the visible area, and few words about circles. I will provide interactive demos at each stage so you do not get lost and see the results immediately.
Line-segment - ray intersection point
It is all about those intersection points. Let's learn step by step how to find them and how to use them. We will derive a line parametric equation first, calculate intersection points and finally learn how to efficiently determine which one of them is the closest.
Deriving line parametric equation
Let us talk about lines and their parametric equation first.
We can express vector by the following equation: , where is an equation parameter defining how much do we stretch () or shrink (), and if we flip the direction () of vector in relation to vector .
Let me show you few examples:
As you might have noticed, we can use as a scale factor: , where denotes the euclidean distance between two points. We will use that fact when determining the closest intersection point.
Now we can easily see, that if we want the point to be contained on:
- line: ,
- ray: ,
- line-segment: .
Having all of that sorted out, we can finally derive the parametric equation:
If you still do not know what happens here, I can recommend an awesome short lecture by Norm Prokup: Parametrizing a Line Segment - Concept.
Calculating the intersection point
Assume that point is the intersection point of line-segment defined by points and , and ray defined by points and . Point is then expressed by set of two equations:
Solve for and
Substitute into the second equation
Substitute into the first equation
Having and calculated, we can calculate using one of the equations from the set.
Finding the closest intersection point
We only need the closest intersection point to properly draw the visible area. The naive solution would be to calculate distances between the ray starting point and intersection points using the Pythagorean Theorem: . Remember the line equation parameter, though? I have already mentioned that we can use it as a scale factor. As we want to compare distances on ray, we can check for the smallest parameter value: .
Casting rays
In the following section we will go through two different ways rays might be cast. We will compare them and mention their pros and cons.
Casting rays by offset angle
First way is to cast rays in all directions by specified offset angle. For instance, we could cast 30 rays in total offseted by . Let's see how to generate all those rays first.
Let be an anchor point of our rays, and let be a some point on a line going through at angle :
We can define as and as (our y-axis is inverted hence why the minus sign). Now we need to derive formulas for and :
in our case has an arbitrary (greater than 0) value (we are looking for any point on the line), so we are safe to assume to simplify the calculations. Having it all considered, , where is our angle offset.
Casting rays on vertices
Casting rays on vertices is most likely your go-to solution. Instead of casting rays in all directions, we can simply cast them on our polygons' vertices. Depending on the number of vertices, we can save computing power by not casting useless rays. In the next sections you will see how does it impact animation smoothness and also learn how to further optimize the whole process.
Illuminating the visible area
This is where the real fun begins. We will illuminate the visible area by filling a giant polygon.
Sorting intersection points
To correctly order vertices to build a proper polygon, we need to sort them by angle first. We will use function for that. You can read more about it here.
Let's compare both ray casting methods:
Both of them look glitchy, jumpy and inaccurate. Let's take a closer look why.
Casting slightly offseted rays
Notice what happens when rays are cast directly on vertices - they should go beyond that vertex but we are getting only the closest intersection point. The most common solution is casting two extra rays offseted by small angle (in both directions) for every cast ray. Consider ray starting at going through . We want to find such point that ray would be rotated by with being the origin point. coordinates would be as follow:
Note that we do not need to do it for the first method - simply add or substract the offset from the angle we are calculating from. For further explanation you can check this article.
Let's see how the illumination behaves after changes:
It did not help much for the first method. We could decrease angle offset (hence increase number of rays) but the result will still be poor. On the other hand, the second method looks very smooth and accurate. From now on, we will not be talking about the first method anymore.
Visibility circle and flashlight
We may want to somehow limit player's visibility. We can achieve that by creating a clipping region of desired shape. In the demos below, I used a CanvasRenderingContext2D.clip() method.
As you might have noticed, there is no optimization whatsoever - we are still calculating all the intersection points. We will get back to it once we learn about spatial hashmaps.
What about circles?
I have rarely seen a real use-case scenario for ray casting on circles. Please treat this section as an extra where I only briefly talk about it. I will provide you with equations and basic demos, the rest is up to you.
Circle - ray intersection point
Let be an intersection point, a ray's anchor point, a point on the ray, a circle's center point and a circle's radius.
Equation set
Solve for
Solve the quadratic equation
- : ray does not intersect the circle,
- : ray intersects the circle in one point (tangent),
- : ray intersects the circle in two points.
Only if and
Only if and
Casting rays on circles
We need to find two tangent lines to a given circle passing through a ray anchor point.
Let be tangent points, a ray's anchor point, a circle's center point and a circle's radius.
We will also be moving all points using translation vector : , so the circle's center is at .
From the circle equation
From the perpendicular line to the circle's radius
Solve the equation system
Substitute into the first equation
Solve the quadratic equation
- : there are no tangent points (ray's anchor point is in the circle),
- : there is only one tangent point (ray's anchor point),
- : there are two tangent points.
Only if
Only if
There is still one issue - it will not work if .
Take a closer look at the following equation: . If , the equation becomes: - we cannot substitute anymore.
Here are the steps for solving the equation system once that happens:
Solve the equation system
Substitute into the first equation
Solve the quadratic equation
- : there are no tangent points (ray's anchor point is in the circle),
- : there is only one tangent point (ray's anchor point),
- : there are two tangent points.
Only if
Only if
Note that we can do the same for , however, it is optional.
If , there are no solutions.
Few optimization techniques
In this section, we will discuss the usage of spatial hashmaps and modified Bresenham's line algorithm. I will not go into implementation details as they are commonly available on the Internet. I will, however, provide you with some demos to try these out and come up with your own ideas on where to use them.
Spatial hashmaps
Currently, we are drawing all polygons and calculating all intersection points, but it is pretty much useless. In most cases, our play area will be much bigger than the viewport. By using spatial hashmaps, we can quickly determine which polygons are visible to the player and perform adequate calculations.
Basically, we want to divide the play area into smaller cells (of predefined size). Each cell consists of a list containing our shapes (most likely line-segments). If a single shape spans across multiple cells, it will be included in all of them.
The name of this technique suggests using hashmaps (eg. cells would be named something like X+":"+Y), but in our case, using a simple 2D array might be more desired.
Here is a simple demo showing how it might work:
We can use them for viewports as mentioned previously, and also visibility circles and flashlights.
Bresenham-based supercover line algorithm
Well, it is high time we did something with finding the closest intersection points. Using spatial hashmaps and modified Bresenham's line algorithm, we can traverse the grid in an efficient manner making as few checks as required. The algorithm should stop when the first cell with an intersection point is found.
You can read more about Bresenham's line algorithm here, and its modified version here.Sorry, unfortunately, I mean this article and not rays. haha, very funny Everything has an end
I think I have covered everything to help you get started. I will try my best to expand it in the future if I spot something needing an additional explanation.
If you enjoyed the read be sure to follow me for more content like that in the future. Also do not forget to message me if one of the topics seems to be poorly explained or you have other suggestions.
Stay tuned for more!