# Ray Casting

## What is Ray Casting?

The ray casting typically is a specific type of rendering process that is used in computer graphics primarily. It is also a process that is used extensively in computational geometry. The most unique aspect of ray casting is that it allows creating a 3D perspective in a 2D map.

Ray casting is a fast process that can transform a limited form of data from the view point into 3D projection by tracing rays to the viewing volume.

The basic principle of ray casting is to cast and trace rays based on groups on particular geometric constraints.

## What is Ray Casting?

Ray casting technique was developed in the 1960s by two scientists at the Mathematical Applications Group and is considered to be one of the fundamental graphics rendering algorithms that use the similar geometric algorithm as used in the ray tracing technique.

The ray casting process involves obtaining a ray from the pixel via the camera and the intersection of the objects present in the scene.

The value of the pixel of the nearest intersection is taken and then it is set further to be the base of a projection.

Ray casting is much different from ray tracing because the algorithm never traces secondary rays recursively like ray tracing does.

It is also a very dynamic and effective process to use as compared to other rendering algorithms including ray tracing.

Ray casting, historically, was one of the most popular rendering tools in 3D games earlier because of several good reasons such as:

• It is a faster and simpler process
• It needs one single computation for each vertical line of the screen and
• It is limited by one or a number of geometric constraints.

However, the images produced by ray casting are not as realistic as ray tracing.

This is because all objects of all shapes cannot be rendered by ray casting because of the geometric constraints related to the process.

Perhaps, the best and easiest way to have a fair bit of idea about what ray casting is and its working process is to consider light being emitted from your eye.

According to the basic principle of physics, the light ray will travel in a straight line till a point where it hits an object.

After that it will be either reflected and/or refracted depending on the transparency of the surface.

At this point, when the light hits the surface of the object, a portion of the light will be absorbed or stopped by the object.

The remaining portion will be either reflected in all possible directions or refracted, as said earlier.

Considering all these three possibilities namely, absorption, reflection and refraction of light, the ray casting process helps in identifying the percentage of light that is dedicated to each of these three possibilities.

This eventually helps in accounting for the light fully.

In Computer Generated Imagery or CGI, ray casting is considered to be the most crucial form of ray tracing with the only difference in the origination of the rays.

This process helps in determining the brightness as well as color of an object at the point of intersection of the ray and expressed as a value of a pixel on the final image.

The ray casting process allowed creating a 3D world from a 2D map to make the first person games more exciting.

A one-dimensional scan is used for this purpose and the horizontal width of the screen is scanned with it.

Though the world appears to be 3D, the players have limited movements since they cannot move up, down, or in limited angles only with cut off distortion.

Due to this particular style of rendering there was no need for firing a ray for every pixel in the frame.

When the hit point is identified the whole vertical column is copied into the frame after the projection distortion is applied on the surface and the result thereof.

## What is Ray Casting Used for?

With the development in the technology, the newer techniques followed in ray casting have had a significant impact on the world of animation.

Hence, this process is used extensively by the animators. This is because this technique allows them to construct images easily and quickly.

Hence, ray casting has also made it possible for the designers and animators to create more effective designs for television and movies pretty quickly which has extended the use of this technique in the TV and movie sector as well.

Images now can be designed with more details and depth which is pretty hard, if not impossible, to accomplish by using any other image rendering methods.

It is for this reason, ray casting grew in popularity exponentially and by the mid-1980s it became a common tool to be used in most of the animation studios.

Apart from its extensive use in the field of movies and television, ray casting technique is perhaps most extensively used by the developers of video games.

It can be said that ray casting has helped significantly in the evolution of the video games which has brought in a revolution in the 3D gaming industry.

Since the late 1970s and early 1980s, application of ray casting in video games was somewhat primitive.

However, it was only in the 1990s that the technology enabled the developers of the video games to add more compelling and crispier 3D images and graphics to the games.

In 3D computer games the ray casting process is extensively used in spite of its less-realistic image rendering simply due to its speed.

One of the most successful games that use ray casting was also the first one to be designed in 1992 and released on May 5.

This game, Wolfenstein 3D, featured a map of rooms resembling a maze.

The floors, walls and ceilings of all the rooms were of the same height and this specific feature helped in ray casting quite significantly.

In the modern world, video games have developed significantly and now faster and more advanced GPUs are used that come with more memory which expedites the computation process of 3D graphics.

Still, even the most advanced video game 3D engines use advanced forms of ray casting since it is considered to be the most resourceful way to deliver a 3D image even today.

## What is the Ray Casting Method?

In this process a direction vector is used in order to signify the orientation of the viewer extending forward.

Also, there is a perpendicular camera plane to the direction vector. This represents the shape of the image that is rendered eventually.

The method used in ray casting is perhaps a bit more focused than ray tracing in its scope, in spite of its ability to render images that hardly resembles the real world in the real sense.

It is for this reason this technique can render 3D images most effectively on a 2D screen.

This is done by tracing the direct path of the rays of light from the eye to its source.

However, the method discounts the influence of the objects that may come in the way of the ray or between the source and the eye though these effects come into play at the point of termination.

These influences are reflection, refraction, and shadows.

In this method every light ray is traced per pixel and the nearest object blocking the path is found.

This is the object viewed through that pixel. Effects of the lights as well as the material properties are used in the scene and the algorithm determines the shading of the object.

This is further modified with the use of conventional 3D computer graphics shading models.

There are three particular algorithms used in ray casting.

One is for line drawing, one to create the shaded pictures, and one to calculate the volumes as well as other physical properties.

For every algorithm there is a specific camera model and it casts a single ray per pixel on the screen.

When it comes to computing the volume, the pixel screen resolution that needs to be used depends on the preferred precision of the solution.

And, as for picture shading and line drawings, the quality of images is determined by the resolution.

For line drawings, the visible edges of the solid object are drawn.

This is done by using one ray per pixel and by moving it to the left, to the right, to the top and to the bottom of the screen.

Each of these rays is evaluated in order to determine the visible surface. This is ideally the first surface pointer of the rays intersecting the surface in the sorted list.

If the visible surface at one pixel point is different from the other point of intersection which is lower but in the same vertical line, another vertical line is displayed at the center which is one pixel long.

Similarly, a horizontal line of one pixel long is also displayed at the center if the visible surface is different on two horizontal planes.

As a result, the final drawing will look jagged consisting of vertical and horizontal edges only and with course resolutions.

Read Also:  What is Optane Memory? Uses, Pros & Cons

In the line drawing process, the images of the solid objects are generated and in order to optimize it dynamic bounding, box enclosures, and coherence are used.

Depending on the density of the screen new edges and rays are located by using binary search.

After that, all these edges are traced by casting supplementary rays for every pixel increment on both sides of the edges.

As for creating the shaded pictures, once again a single ray for every pixel on the screen is cast.

However, this time the visible surface pointer is used for each pixel in order to access the surface description.

The surface normal is computed at this stage at the visible point. Some trigonometric formulas are used in this case.

For example, the cosine of the angle created by the light source to the object surface vector and the surface normal.

This value is proportional to the value of the pixel as well as the light intensity that is displayable.

When all the pixels are processed in this way, it generates a raster-like image of the scene.

The developers at this stage use a lot of mathematical and other formulas to create the volume and moments of inertia.

They do it quite easily by computing the approximate sum by using an integration method.

In this method the solid is approximated with a group of rectangular parallelepipeds.

The in-depth picture of the object is taken in a parallel view for this matter and rays are cast into the solid divisions through the screen and to the volume elements.

The two dimensions of the rectangular parallelepipeds are invariable and are typically defined by the two-dimensional spacing of the rays on the screen.

The remaining dimension is variable and is defined by the entry and exit point computed.

Though the concept of ray casting seems to be simple, it is the implementation of the details involved in it that is trickier and involves a lot of calculations.

If the solid object is in the shape of a sphere, then finding the point of intersections is fairly easy.

There is a formula to use for the line of intersection with the sphere.

Typically, there will be a normal vector on the sphere at a particular point which will be in the same direction of the line to that particular point to the center of the sphere.

If the sphere is made of a uniform material, then all the necessary data can be had this way.

However, in most of the cases, the surfaces of the objects are in the form of triangular meshes.

This means that every vertex of the triangle will have specific properties.

In such situations, if the ray caster finds that the point of intersection lies on a triangle, the properties of that particular point of intersection needs to be computed.

This is typically done by interpolating the values of the properties of the vertices of the triangle.

Here, a particular algorithm is used called the interpolation algorithm. It usually uses Barycentric coordinates or areal coordinates on the triangle.

This is complex math and needs a bit more explanation.

Suppose, A, B, and C are the three vertices of a triangle with a point P inside it.

The point P can be determined exclusively in the form of a*A + b*B + c*C. Here, a, b, and c are numbers that range between 0 and 1, and a + b + c = 1.

In this case, it is the coefficients of a, b, and c that are called the Barycentric or areal coordinates of the point P.

However, if the values at the vertices of the triangle are different, say V (A), V (B), and V(C) for example, then an interpolated value V (P) is determined by using the Barycentric coordinates of P.

The formula for this is however the same as: V (P) = a*V (A) + b*V (B) + c*V (C).

The final value obtained in this method of ray casting may signify the texture coordinates, diffuse color, or the normal vector.

Well, the ray casting method involves a lot more mathematical computations than these in order to find the Barycentric coordinates of the intersecting point or to find the ways to test the line intersecting the triangle, if at all.

However, these are left out deliberately because those would not have added much significant material to the discussion, conceptually.

## Ray Casting Games

Here are some of the most commonly played and known games, in no particular order, that use ray casting technology to offer a better gaming experience.

Wolfenstein 3D – This video game was built from a cubical based grid of floors and ceilings of solid colors and walls of uniform height that found the ray-wall collisions quickly with lower memory overheads and predictable potential hits.

ShadowCaster – This video game uses a much improved Wolfenstein-based engine. It has variable wall heights as well as additional texturing on the floors and ceilings.

Comanche Series – This is a Voxel Space engine where a ray is traced through every column of the pixels of the screen and tested against a given point in a height-map.

Read Also:  What is FPS (Frames Per Second)? (Explained)

This, in turn, altered every element of the height-map to a column of pixels as determined by their visibility or those that have not been occluded by the pixels drawn. These elements are drawn with the color corresponding to the texture map.

Rise of the Triad: Dark War – This video game comes with a wide variety of explosive weapons and is filled with incredible power-ups.

Also filled with an absurd amount of gibs, this arcade style game is one of the spiritual sequels to Wolfenstein 3D that stands ‘over the top’ with a plethora of new and exciting features included.

Zero Tolerance – This is a typical First Person Shooter game where the gamer plays as a part of a five-person attack squad. They are engaged in a fight with the extraterrestrial beings on a space station that is invaded by them.

Blake Stone: Planet Strike – This video game is the sequel to Blake Stone: Aliens of Gold in which the futuristic secret agent, Blake Stone, breaks into an abandoned training facility located on planet Selon.

The intention is to stop the elusive mad scientist, Dr. Goldfire, from creating an army of dangerous mutants to triumph over humanity.

Blake Stone: Aliens of Gold – Set in the 22nd century, this game has it all. There are soldiers, aliens, robots, and mutants.

There is also a rogue scientist who wants to enslave humanity by using them. A British secret agent, Blake Stone, needs to break through the well-guarded facility and incapacitate his campaign.

Corridor 7: Alien Invasion – This is also a First Person Shooter game that uses the Wolfenstein 3D engine as well. The location of the game is a top-secret American underground facility which is infested by the aliens.

Operation Body Count – The player in this game becomes a member of the UN Elite Force. They have to infiltrate a government building under seize by the terrorists. The main objective is to take out the various threats inside.

The Terminator: Rampage – The gamer plays this game as a solitary soldier who is sent to the past in order to prevent the takeover of an extensive military complex by Skynet.

Spear of Destiny – This is a prequel to Wolfenstein 3D and is also a standalone and official expansion of the game.

In this video game the players have to repeat the role of allied spy William “B.J.” Blazkowicz. They have to creep into a Nazi installation that is heavily guarded and get back the mythical Spear of Destiny.

Catacomb 3-D – This is also a First Person Shooter game which is the predecessor of Wolfenstein 3D.

The Catacomb Abyss – This is the sequel to Catacomb 3-D and is the first part of the trilogy.

VRG – This is also a First Person Shooter game inspired by several different games such as Wolfenstein II: The New Colossus, TitanFall 2, and Wolfenstein 3D.

This game comes with all available and modern operating technicalities such as Sprint, Lean, and Wall Run, Dual Wield, Sliding and more.

Players in this game can carry half a dozen weapons and switch between them by using the weapon wheel in slow motion or the weapon key.

DUGA – This is both a game and a ray casting engine very much like the old Wolfenstein 3D. Built in the Pygame framework, this game comes with an outstanding frame rate and resolution, advanced AI systems, and quite a lot of weapons.

immuNAZItion – This game is an old school First Person Shooter game that uses ray casting to convert 2D maps to 3D.

There are also a few other good games that you may also play and enjoy a great deal such as:

• Ray Castle
• No Return
• Candy War
• Maze
• Lode Runner: The Truth
• The Silver Forest
• T.G.H.N.N. – The Game Has No Name
• Amity’s Samhain
• Duncan’s Schoolhouse
• Burning Glyphs
• Touhou: Splatter Faith 3D
• Knuckles Cakewalk 3D
• Kalvgv’s Descent into the Pith
• Catacombs of the Necromancer and
• Tainted Transmission.

A few of the games mentioned are ages old but still are good to explore and stay entertained.

## Conclusion

Ray casting is a very important technique used in games to present the objects in a better way to offer a much better gaming experience.

Though the images are more block-like and less realistic, it is quite a useful technique as you can see from this article.