CCS347 GD - Unit 3
CCS347 GD - Unit 3
1. Vertex Processing:
The pyramid and cube are defined by vertices (their corner points) in 3D space. These vertices
also store data like color (red for pyramid, blue for cube) and normals (imaginary lines used
for lighting calculations).
The vertex shader (a program) takes these vertices and transforms them into clip space. Clip
space considers the camera's position and what the camera "sees" within a viewing area called
the frustum (like a pyramid extending from the camera). Vertices outside the frustum are
discarded for efficiency.
2. Primitive Assembly:
The transformed vertices are grouped into triangles. In this case, both the pyramid and cube
can be broken down into multiple triangles.
3. Rasterization:
The triangles are converted into fragments, which are tiny samples corresponding to
individual pixels on the screen. The rasterizer figures out which triangles contribute to each
pixel and calculates where on the screen those fragments should be placed.
4. Fragment Processing:
Here's where the fragment shader comes in. This program runs on each fragment, considering
lighting, textures (if applied), and other factors.
o For the pyramid's fragments, the shader would likely use the stored red color and
lighting calculations to determine the final shade of red for each pixel that the pyramid
covers.
o Similarly, the cube's fragments would use the blue color information and lighting to
determine the final shade of blue for its pixels.
5. Output:
Finally, all the calculated fragment colors are assembled into the final image. Pixels with
overlapping fragments might go through additional blending processes to determine the final
displayed color. The result is the image you see on the screen with the red pyramid and blue
cube in their respective positions.
This is a simplified example, but it highlights how the rendering pipeline takes raw 3D data and
transforms it into the final image through a series of software and hardware coordinated stages.
1. Rendering
Rendering is the process of generating a visual representation of a 3D scene or object. It involves
transforming mathematical descriptions of objects into images that can be displayed on a screen.
Rendering can be broadly categorized into two main approaches:
1. Software rendering
2. Hardware rendering.
1. Software Rendering:
Software rendering refers to the process of generating images entirely through software
algorithms running on the CPU (Central Processing Unit). In this approach, all rendering
computations, including geometric transformations, lighting calculations, texture mapping,
and rasterization, are performed by the CPU.
Software rendering offers flexibility and can be implemented on a wide range of hardware
configurations since it does not rely on specialized graphics hardware. However, it tends to
be slower compared to hardware rendering, especially for complex scenes or real-time
applications.
Despite its performance limitations, software rendering is still used in certain applications
where hardware acceleration is not available or where precise control over rendering
algorithms is required.
2. Hardware Rendering:
Hardware rendering, on the other hand, leverages specialized graphics hardware, such as
Graphics Processing Units (GPUs), to accelerate the rendering process. Modern GPUs are
highly optimized for rendering tasks and can perform computations in parallel, leading to
significant performance gains.
In hardware rendering, many rendering tasks, including vertex transformations,
rasterization, pixel shading, and texture mapping, are offloaded to the GPU, which can
execute them in parallel across multiple processing units.
Hardware rendering is commonly used in real-time applications such as video games,
simulations, and interactive graphics applications, where high performance and
responsiveness are critical.
The use of hardware rendering has enabled the development of increasingly realistic and
immersive graphics experiences, with capabilities such as advanced lighting and shading
effects, high-resolution textures, and complex geometric detail.
Rendering : Software vs. Hardware (with Examples)
Imagine you're drawing a 3D scene on your computer. Both software and hardware rendering achieve
this final image, but they take different approaches:
1. Software Rendering:
Think of it like hand-drawing the scene.
Your CPU, the "artist," performs all the calculations for lighting, shadows, textures, and colors
for each pixel on the screen.
This is a flexible method, allowing for custom code and unique effects.
Example: Imagine drawing a sphere. The software renderer would calculate the lighting and shading
for every tiny point on the sphere's surface, similar to how you'd shade a sphere with a pencil.
Pros:
Flexible for creating unique visuals
Doesn't require specialized hardware (useful for older computers)
4204 – APEC K. Chairmadurai, AP/CSE
CCS347 – GD Unit 3
Cons:
Slow and time-consuming, especially for complex scenes
Less efficient use of computer resources
2. Hardware Rendering:
Think of it like using a specialized paintbrush designed for detailed scenes.
The GPU, a powerful graphics card, takes over the rendering tasks.
It's much faster and more efficient, handling complex calculations in parallel.
Example: The GPU can process the entire sphere at once, applying textures, lighting, and shadows
efficiently, like using a pre-made brush with all the details included.
Pros:
Significantly faster rendering times
More efficient use of computer resources
Enables complex lighting and effects for realistic visuals
Cons:
Less flexible for creating completely custom effects
Requires a dedicated graphics card
The Choice:
Software rendering is still used for specific applications like simple games or creating unique
visual effects in animation. However, for most modern graphics needs, hardware rendering with its
speed and efficiency is the preferred choice.
Software Rendering Vs Hardware Rendering
Software rendering and hardware rendering are the two main approaches to creating images on a
computer screen from a digital model. Here's a breakdown of their differences:
1. Processing Power:
Software Rendering: Relies solely on the CPU (Central Processing Unit) for all calculations.
The CPU is a general-purpose processor that can handle various tasks, but rendering can be
quite demanding.
Hardware Rendering: Utilizes the GPU (Graphics Processing Unit), a specialized chip
designed specifically for graphics processing. GPUs are much faster and more efficient at
handling the complex calculations involved in rendering.
2. Speed and Efficiency:
Software Rendering: Generally slower and less efficient than hardware rendering.
Rendering complex scenes can take a significant amount of time and consume a lot of CPU
resources.
Hardware Rendering: Significantly faster and more efficient. GPUs are optimized for
rendering tasks, allowing for smoother visuals and faster rendering times, especially for
complex scenes.
3. Flexibility:
Software Rendering: Offers greater flexibility. Since the CPU handles everything, developers
have more control over the rendering process and can create unique visual effects with custom
code.
Hardware Rendering: Less flexible. While offering a wide range of features and effects,
hardware rendering is often bound by the capabilities of the GPU and its drivers. Creating
completely custom effects can be more challenging.
4. Common Use Cases:
Software Rendering:
o Early days of computer graphics (when GPUs weren't as powerful)
o Specialized applications where unique visual effects are needed (e.g., creating custom
filters)
o Fallback option for systems without a dedicated graphics card
Hardware Rendering:
o Modern games and 3D applications
o Video editing and animation workflows
o Any scenario where smooth visuals and fast rendering times are crucial
Here's an analogy:
Software Rendering: Like hand-drawing a scene. It's flexible but time-consuming, requiring
the CPU to do all the calculations for each pixel.
Hardware Rendering: Like using a specialized paintbrush designed for detailed scenes. The
GPU takes over the rendering tasks, making it faster and more efficient.
GPU rendering (right) shows lines that CPU (left) doesn't
Example:
Imagine a simple 3D cube model. By default, the cube might be positioned at the center of the scene. A
vertex shader can be used to:
Rotate the cube: The shader program can take an angle as input and rotate all the vertices of
the cube around a specific axis (say, the Y-axis) by that angle. This effectively rotates the entire
cube in the scene.
Translate the cube: The shader can take a position vector as input and add it to the original
position of each vertex. This effectively moves the entire cube to a new location in 3D space,
like placing it on a virtual table within the scene.
Scale the cube: The shader can take a scale factor as input and multiply it by the original
position of each vertex. This makes the cube bigger or smaller, depending on the scale factor.
By manipulating the positions of the vertices, vertex shaders can create various effects on the final
rendered image. They are a fundamental building block in the graphics pipeline, allowing for dynamic
and complex 3D scenes.
2. Back-Face Culling
Back-face culling is a technique used in 3D computer graphics to improve rendering
performance by selectively discarding polygons that are not visible to the viewer.
What it does:
Back-face culling analyzes the orientation of faces (polygons) in a 3D model.
It determines which faces are facing away from the viewer (the camera).
These back faces are then culled, meaning they are not processed for rendering.
Benefits:
Faster rendering: By skipping calculations for invisible faces, rendering becomes significantly
faster, especially for complex models.
Improved efficiency: Resources like processing power and memory are used more efficiently
by focusing on the visible parts of the model.
Example:
Consider a simple cube model. Here's how back-face culling works:
1. Identify faces: The graphics pipeline analyzes each face (square) of the cube.
2. Determine orientation: It checks if the face is oriented with its vertices forming
a clockwise or counter-clockwise order when viewed from the camera's perspective.
(Conventionally, counter-clockwise is considered the "front face.")
3. Cull back faces: If a face has a clockwise vertex order, it's identified as a back face. This face is
then culled, meaning it's skipped during the rendering process.
Visualization:
Imagine looking directly at one side of the cube. The three visible faces will have a counter-
clockwise vertex order from your viewpoint. The back face, which you can't see, will have a
clockwise vertex order. Back-face culling discards calculations for this unseen back face, focusing
processing power on the visible portions of the model.
Back-face culling is a fundamental optimization technique that significantly improves rendering
speed and efficiency. It's a crucial step that ensures your computer doesn't waste resources
rendering invisible parts of 3D objects.
3. Clipping
Clipping is a crucial step in the rendering pipeline where objects or parts of objects that lie
outside the viewing frustum (the portion of 3D space visible in the camera's view) are
removed or clipped.
This ensures that only the portions of objects that are visible within the camera's field of view are
rendered, improving efficiency and reducing rendering artifacts.
Imagine looking through a window; you only see what's within the frame, not everything beyond.
Here's clipping explained with two examples and corresponding images:
1. Point Clipping:
Scenario: A 3D cube model positioned partially outside the camera's view frustum (the
pyramid shape).
Point Clipping: This process analyzes the individual vertices (points) of the cube and
compares their positions with the clipping planes of the frustum.
Clipped Points: Vertices that fall outside the frustum (red points) are discarded (clipped).
Only vertices inside the frustum (blue points) are kept for rendering.
Result: The final rendered image shows only the portion of the cube visible within the
camera's view, avoiding rendering unnecessary geometry.
2. Line/Polygon Clipping:
Scenario: A 3D pyramid model positioned at an angle, with some faces partially inside and
outside the camera's view frustum.
Line/Polygon Clipping: This process analyzes the edges (lines) and faces (polygons) of the
pyramid.
Clipped Portions: The algorithm identifies the parts of faces that fall outside the frustum (red
portions) and clips them away. The remaining visible portions (blue) are kept for rendering.
Result: The final rendered image shows only the visible faces of the pyramid, with the clipped
portions removed, ensuring only the geometry within the camera's view is displayed.
Benefits of Clipping:
Faster Rendering: By discarding invisible geometry, clipping significantly improves
rendering speed.
Improved Visuals: Clipping prevents glitches and ensures only the intended scene is
displayed.
Efficient Resource Usage: Clipping reduces the workload on the graphics processing unit
(GPU), making rendering more efficient.
In essence, clipping acts as a filter, removing unnecessary information before rendering. This
streamlines the process, leading to smoother visuals and a more enjoyable 3D graphics experience.
4. Rasterizing
Rasterizing is the process of converting geometric primitives (such as lines, polygons, and curves)
into a raster image composed of pixels.
It is a crucial step in the rendering pipeline that determines which pixels on the screen are affected
by the primitives and assigns appropriate colors to them.
Rasterization typically occurs after the vertex processing stage in the rendering pipeline.
It involves interpolation to determine pixel colors based on the properties of vertices and the
geometry of primitives.
Rasterization is highly optimized for efficiency, often utilizing hardware acceleration for rapid
processing, especially in real-time applications such as video games.
The rasterization process can handle various types of primitives, including points, lines,
polygons, and more complex shapes.
5. Edge Buffers
Edge buffers, sometimes called Z-buffers, play a crucial role in the 3D graphics pipeline by storing
depth information for each pixel on the screen.
Imagine having a separate map that tells you how far away each object is from the camera in your
scene. This "depth map" is essentially what an edge buffer provides.
What they do:
Store a depth value for each pixel on the screen. This value represents the distance from that
pixel's corresponding point in 3D space to the camera.
This depth information is crucial for determining which objects are closer and should be
displayed in front of others.
Example:
Rasterization: Both the tree and house models are rasterized, creating fragments for each pixel
they cover on the screen.
Edge Buffer: The depth value (distance from camera) for each fragment is stored in the edge
buffer. For example, fragments from the tree will have a smaller depth value (closer to the camera)
compared to fragments from the house.
Hidden Surface Removal: By comparing depth values, the renderer determines which object is
closer for each pixel. In areas where the tree overlaps the house, the renderer would prioritize
fragments from the tree (closer depth value) and discard fragments from the house (further depth
value). This ensures the tree appears in front of the house in the final image.
6.Scan Line Processing
Scan line processing is a fundamental step in the 3D graphics pipeline responsible for filling the
pixels on the screen.
Imagine you're coloring a complex picture line by line. Scan line processing works similarly,
processing each horizontal line of pixels to determine the final color displayed.
What it does:
Processes each horizontal scan line (row of pixels) on the screen.
Analyzes the fragments (color information) generated during rasterization for each pixel along
that scan line.
Determines the final color of each pixel based on various factors like depth, shading, and
textures.
Scan line processing acts like a meticulous artist, filling the canvas (screen) line by line, ensuring
each pixel displays the appropriate color based on fragment information, depth, and shading
calculations.
It's a crucial step that brings your 3D scenes to life with proper depth perception and realistic
visuals.
7.Pixel Shaders
Pixel shaders, also known as fragment shaders, are programs executed on the GPU (Graphics
Processing Unit) that manipulate individual pixels during the rendering process.
They are a critical component of modern graphics pipelines and are used to apply various effects
and computations to pixels, such as lighting, shading, texturing, and post-processing.
Pixel shaders are the tiny artists of the 3D graphics world.
Imagine having a vast canvas (the screen) and millions of tiny brushes (pixels).
Pixel shaders are like those brushes, applying color, lighting effects, and textures to each
individual pixel, ultimately creating the final image you see.
What they do:
Pixel shaders are programs that run on the graphics card (GPU).
They receive information about each pixel on the screen, including:
o Fragment data (color information) generated during rasterization.
o Texture coordinates that map textures onto the 3D models.
o Lighting information from the scene (light position, intensity, etc.).
Based on this information, pixel shaders calculate the final color of each pixel. They can
perform various operations like:
o Applying textures: Mapping textures like wood grain or brick onto the surface of 3D
models.
o Lighting calculations: Simulating how light interacts with the objects, creating
shadows, highlights, and reflections.
o Special effects: Adding fog, water ripples, or other visual enhancements.
The pixel pipeline involves the following ordered list of operations:
Pixel shader Depth test
Occlusion queries Fog effects
Scissor test Alpha blending.
Alpha test Dithering.
Stencil test Color masking
Display
Example:
Imagine a simple 3D scene with a textured sphere:
1. Rasterization: The sphere is rasterized, creating fragments for each pixel it covers on the
screen.
2. Pixel Shader: Each fragment is passed to the pixel shader program.
3. Texture Mapping: The pixel shader uses texture coordinates to apply a wood grain texture
to the sphere's surface.
4. Lighting Calculations: The pixel shader also considers lighting information from the scene
to create shadows and highlights on the textured sphere.
Pixel shaders are the workhorses of modern 3D graphics. They take the raw fragment data and
transform it into stunning visuals by applying textures, lighting effects, and various calculations on
a per-pixel basis. They are essential for creating realistic and visually compelling 3D experiences.
8.Stencil Buffering
Stencil buffering is a technique used in computer graphics to control the rendering of pixels
based on a stencil value associated with each pixel.
It allows for advanced rendering effects such as masking, selective rendering, and creating
complex shapes or patterns.
What it does:
Stencil buffers are typically 8-bit buffers that store integer values for each pixel, separate
from the color and depth information.
These values can be used to create masks that define which pixels are affected by certain
rendering operations.
Example 1: Outlines
Imagine a simple 3D scene with a red cube:
Example 2: Reflections
Imagine a scene with a reflective floor and a blue sphere:
Stencil buffering offers a versatile tool for enhancing the visual complexity of 3D scenes. By
controlling which pixels are rendered based on stencil values, you can achieve effects like outlines,
reflections, shadows, and more, adding depth and realism to your graphics.
9. Depth Buffering
Depth buffering, also known as z-buffering, is a technique used in computer graphics to
determine which objects or parts of objects are visible in a scene based on their distance from
the viewer.
It helps ensure that only the closest visible surfaces are rendered, preventing objects from
overlapping incorrectly and creating a realistic sense of depth in the scene.
Depth buffering, also known as Z-buffering, is a fundamental concept in 3D graphics responsible
for ensuring objects are rendered in the correct front-to-back order.
Imagine looking at a scene with multiple objects; depth buffering acts like a behind-the-scenes
organizer, making sure closer objects appear in front of further ones.
What it does:
Depth buffers are typically 16-bit or 32-bit buffers that store a depth value for each pixel on
the screen. This value represents the distance from that pixel's corresponding point in 3D
space to the camera (closer objects have smaller depth values).
During the rendering process, the depth value of each fragment (color information)
generated during rasterization is compared to the existing depth value stored in the depth
buffer for that pixel.
10.Alpha Blending
Alpha blending is a technique used in computer graphics to combine multiple layers of images
or objects with varying levels of transparency (alpha values) to create the appearance of
transparency and translucency.
It allows for the smooth blending of overlapping objects and textures, resulting in visually
appealing effects such as transparency, translucency, and fading.
Alpha blending is a technique in computer graphics used to combine two images or textures,
creating a sense of transparency or layering.
Imagine having two overlapping transparencies on a projector; alpha blending achieves a
similar effect digitally.
What it does:
Alpha blending considers the alpha channel, which is an additional channel (often the fourth
channel) in image formats like PNG that stores transparency information for each pixel. A
value of 0 represents fully transparent, and 255 represents fully opaque.
The blending process combines the color information (RGB) of two images with their
corresponding alpha values to determine the final color displayed for each pixel in the
resulting image.
1. Color Selection: You use a color picker to select the red color of the flower.
2. Mask Creation: The software creates a mask that highlights the red areas of the image,
primarily focusing on the flower.
3. Color Adjustment: With the mask active, you can change the hue of the flower to a different
color (e.g., blue) without affecting the rest of the image.
Example 2: Selective Brightness
Imagine an image with a landscape and a bright sky:
1. Color Selection: You choose a color range targeting the blue sky area.
2. Mask Creation: The software creates a mask that highlights the blue sky region.
3. Brightness Adjustment: With the mask active, you can selectively decrease the brightness
of the sky to create a more balanced exposure without affecting the landscape elements.
12.Texture sampling
Texture sampling is a fundamental concept in 3D graphics responsible for applying textures
(images) to the surfaces of 3D models.
Imagine having a sheet of wrapping paper with a beautiful pattern; texture sampling acts like
the process of adhering that paper (texture) onto a 3D object to create a visually interesting
surface.
What it does:
Textures are essentially images that store color and lighting information for a surface.
Texture sampling involves determining which color value from the texture image should be
applied to a specific point on a 3D model's surface.
The Process:
1. UV Mapping: 3D models are unwrapped into a 2D layout (similar to flattening a globe)
called a UV map. This map defines how the texture image will be mapped onto the model's
surface.
2. Texture Coordinates: Each point on the 3D model's surface has corresponding UV
coordinates within the UV map (like longitude and latitude on a globe).
3. Sampling the Texture: During rendering, for each fragment (point on the model's surface)
being processed, its UV coordinates are used to locate the corresponding pixel within the
texture image.
4. Color Lookup: The color value stored in that specific pixel of the texture image is then used
to determine the final color of the fragment on the 3D model.
Summary
1. Vertex Shaders:
Vertex shaders are programs executed on the GPU (Graphics Processing Unit) that
manipulate properties of vertices in 3D models. They perform tasks such as
transforming vertices, applying animations, and performing lighting calculations.
2. Back-Face Culling:
Back-face culling is a graphics optimization technique where polygons that are facing
away from the viewer are discarded during rendering. This helps improve rendering
performance by reducing the number of polygons that need to be rendered.
3. Clipping:
Clipping is a process in computer graphics where objects or parts of objects that lie
outside the view frustum (the portion of 3D space visible in the camera's view) are
removed or clipped. This ensures that only the visible portions of objects are rendered.
4. Rasterizing:
Rasterizing is the process of converting geometric primitives (such as lines or polygons)
into a raster image composed of pixels. It involves determining which pixels on the
screen are affected by the primitives and assigning appropriate colors to them.
5. Edge Buffers:
Edge buffers, also known as edge tables, are data structures used during rasterization to
store information about the edges of polygons. They help efficiently determine pixel
coverage for polygons during scan-line rendering.
6. Scan Line Processing:
Scan line processing is a rendering technique where each horizontal line of pixels on a
display or render target is processed sequentially. It involves determining which pixels
are affected by primitives and applying appropriate rendering operations.
7. Pixel Shaders:
Pixel shaders, also known as fragment shaders, are programs executed on the GPU that
manipulate individual pixels during the rendering process. They perform tasks such as
lighting calculations, texture mapping, and post-processing effects.
8. Stencil Buffering:
Stencil buffering is a technique in computer graphics where a separate buffer, called the
stencil buffer, is used to selectively enable or disable rendering of pixels based on stencil
values associated with each pixel. It allows for advanced rendering effects and
optimizations.
9. Depth Buffering:
Depth buffering, also known as z-buffering, is a technique used to determine the
visibility of objects in a scene based on their distance from the viewer. It ensures that
only the closest visible surfaces are rendered, preventing rendering artifacts such as
object overlap.
10. Alpha Blending:
Alpha blending is a technique used to combine multiple layers of images or objects with
varying levels of transparency to create visually appealing effects such as transparency,
translucency, and fading.
11. Color Masking:
Color masking is a technique used to selectively enable or disable rendering of specific
color channels (such as red, green, blue, or alpha) for certain objects or portions of the
scene. It allows for precise control over which color components are affected during
rendering.
12. Texture Sampling:
Texture sampling is the process of retrieving texel (texture element) values from a
texture map to apply them to the surface of a 3D object during rendering. It is used to
add surface detail and realism to objects by applying textures such as colors, patterns,
or images.
13. Frame Buffers:
Frame buffers are regions of memory used to store the contents of each pixel on a
display or render target. They hold the final rendered image before it is displayed on the
screen, along with additional information such as depth values and stencil values.
Applications:
o Collision detection in 2D games.
o Image compression (like JPEG) where quadtree nodes can store average colors of sub-
regions.
o Terrain simplification for efficient rendering.
Example: Imagine a map with houses scattered across it. A quadtree could subdivide the map
into squares, with squares containing multiple houses being further subdivided until reaching a
manageable number of houses per square. This allows for efficient searching of houses within a
specific area.
Octrees:
Concept:
o An octree represents a 3D space, starting with a single cube encompassing the entire
volume.
o Similar to quadtrees, if the cube contains a high number of points (or objects) or needs
further detail, it's recursively subdivided into eight equal-sized sub-cubes (octants).
o This subdivision continues until a set criteria is met.
Applications:
o Collision detection in 3D games.
o Ray tracing for efficient intersection calculations.
o Particle systems for managing and rendering large numbers of particles in 3D space.
Example: Imagine a scene with trees and buildings. An octree could subdivide the scene into
octants, grouping nearby trees within specific octants. This allows for faster processing when
checking for collisions between objects or performing ray tracing calculations that determine
which objects light rays intersect with.
Benefits of Quadtrees and Octrees:
Efficient Search: By organizing points or objects within a hierarchical structure, searching
for specific elements becomes faster. You only need to traverse relevant branches of the
tree based on your search area, significantly reducing the number of objects to consider
compared to a linear search.
Dynamic Updates: Both quadtrees and octrees can be dynamically updated as objects
move or are added/removed from the scene. The tree structure can be adjusted by
subdividing or merging squares/cubes as needed to maintain efficiency.
Data Compression (for specific applications): In some cases, quadtrees can be used for
image compression by storing average colors of sub-regions within the tree.
Choosing Between Quadtrees and Octrees:
The choice depends on the dimensionality of your data:
Use quadtrees for strictly 2D applications like managing objects on a map or image
compression.
Use octrees for 3D applications like collision detection, ray tracing, or managing particle
systems in a 3D world.
b) Binary Space Partitioning (BSP) Trees
BSP trees are another type of spatial partitioning algorithm used for efficient organization of
objects in 2D or 3D space. Unlike quadtrees and octrees that subdivide space into grids, BSP
trees recursively partition the space using dividing planes (2D) or hyperplanes (3D).
This creates a binary tree structure where each node represents a sub-space divided by a plane.
Concept:
Imagine a scene with various objects.
A BSP tree starts by selecting an object and creating a plane that divides the scene in half
(based on an axis like X or Y) relative to that object.
Objects on one side of the plane are placed in one child node of the tree, and objects on the
other side are placed in the other child node.
This process continues recursively, with each child node potentially being further divided
by a plane based on another object within its sub-space.
The recursion stops when a predefined criteria is met (e.g., minimum number of objects per
sub-space or reaching a certain depth in the tree).
Benefits:
Efficient Object Culling: By organizing objects based on their location relative to dividing
planes, BSP trees allow for faster culling (discarding) of objects outside the view frustum
(camera's viewable area). This improves rendering performance.
Collision Detection: BSP trees can be used for faster collision detection, as objects on
opposite sides of a dividing plane cannot collide by definition.
Visibility Determination: In some cases, BSP trees can be used to determine object
visibility based on their position relative to dividing planes.
Example (2D):
Example (3D):
Benefits:
Flexibility: User-defined maps allow you to tailor the spatial organization to perfectly
match your application's requirements.
Efficiency: By structuring the map in a way that aligns with how objects are accessed or
interact, you can potentially achieve better performance compared to more generic spatial
partitioning techniques.
Domain-Specific Optimization: You can leverage your knowledge of the specific problem
and object behavior to create a map that optimizes search, collision detection, or other
spatial operations.
Examples:
2D Game World: A grid map where each cell stores objects located within its boundaries.
This allows for efficient neighbor finding and pathfinding within the game world.
3D Race Track: The track can be divided into segments, and objects (cars) are assigned to
specific segments based on their current position. This allows for optimized collision
detection between nearby cars and selective rendering of objects within the current and
adjacent segments.
Voxel Terrain: A 3D world can be represented by a voxel grid, where each voxel stores
information about its content (e.g., solid ground, air, water). This allows for efficient ray
tracing and collision detection within the 3D voxel space.
User-defined maps provide a powerful tool for spatial sorting when you need a high degree of
customization and control over how objects are organized within your application's space.
By understanding the trade-offs and carefully designing your map structure, you can achieve
significant performance improvements for tasks like searching, collision detection, and efficient
rendering in your spatial applications.
2. Node-based sorting
Node-based sorting isn't a single specific spatial sorting algorithm, but rather a general concept that
can be applied in various ways. It involves using a node-based data structure, like a tree or a graph,
to organize and sort elements based on their spatial properties (location in space).
Each node in the data structure represents an element or a sub-region of the space.
Nodes can store information about the element itself (e.g., its position) and pointers to other
nodes representing neighboring elements or sub-regions.
Sorting happens by traversing the nodes in a specific order based on a defined criteria, such
as distance from a specific point, position along an axis, or hierarchical containment within
the space.
Benefits:
1. Flexibility: Node-based structures can be adapted to represent various spatial relationships
between elements.
2. Dynamic Updates: Nodes can be easily added, removed, or updated to reflect changes in the
spatial arrangement of elements.
3. Potential Efficiency: By efficiently traversing the nodes based on the sorting criteria, you can
achieve faster search and processing compared to linear searches through all elements.
Examples:
K-d Trees: These are spatial data structures where each node represents a hyperplane
(dividing plane in 3D) that splits the space. Points are stored in leaves (terminal nodes) based
on their position relative to the dividing planes in the tree. This allows for efficient searching
and nearest neighbor queries in 3D space.
BVH (Bounding Volume Hierarchy): This uses a tree structure where nodes represent
bounding volumes (simplified shapes like spheres or boxes) that enclose groups of objects. By
efficiently traversing the tree and discarding branches where the bounding volume doesn't
intersect the search area, BVHs enable faster collision detection and ray tracing in 3D scenes.
An example of a bounding volume hierarchy using rectangles as bounding volumes
3.Portals
In computer graphics, portals (sometimes referred to as visibility portals) are a technique used
to optimize rendering and improve performance in 3D environments.
They act as gateways or connections between different parts of a scene, allowing the graphics
processing unit (GPU) to selectively render only what's visible through the portal.
Concept:
Imagine a large, complex scene like a maze or a building with multiple rooms. Rendering the
entire scene at once can be computationally expensive.
Portals are defined as pairs of connected surfaces within the scene.
When the camera approaches a portal, the GPU only renders the scene visible through that
portal, essentially teleporting the view to the connected area.
This avoids unnecessary processing of parts of the scene that are hidden behind walls or
outside the current viewing frustum (camera's viewable area).
Benefits:
Improved Performance: Portals significantly reduce the amount of geometry the GPU
needs to render, leading to smoother frame rates, especially in complex scenes.
Level of Detail (LOD): Portals can be combined with level-of-detail techniques, where
distant objects are rendered with less detail for further performance gains.
Hidden Area Culling: Portals act as a form of hidden area culling, automatically discarding
parts of the scene that are not visible through the portal.
Challenges:
Portal Placement: Carefully designing and positioning portals is crucial for optimal
performance. Incorrect placement can lead to visual artifacts or unnecessary rendering of
hidden areas.
Recursion: Portals can lead to recursive rendering if not carefully managed. This can occur
when portals lead to interconnected areas, potentially causing the GPU to get stuck in an
infinite loop.
Complexity: Implementing portal rendering can be more complex than traditional
rendering methods.
Applications:
Large Open Worlds: Portals are commonly used in games with vast open worlds, allowing
for seamless transitions between different areas without compromising performance.
Buildings and Interiors: In games or simulations with complex buildings, portals can be
used to optimize rendering within rooms, corridors, and other connected spaces.
First-Person Shooters: Portals can be used to create realistic transitions between rooms
and hallways in first-person shooter games, enhancing the sense of immersion.
Example:
Imagine a hallway in a game level with a doorway leading to another room. The doorway
acts as a portal. As the player approaches the doorway, the scene visible through the doorway is
rendered, effectively teleporting the player's view to the connected room without rendering the
entire hallway and the other room simultaneously.
Portals are a valuable technique for optimizing rendering in complex 3D environments. By
strategically using portals, developers can create visually rich and immersive experiences while
maintaining smooth performance.
4. Occlusion Culling
Occlusion Culling is a technique used in computer graphics to improve rendering performance
by only rendering objects that are visible to the camera and not hidden by closer objects.
This significantly reduces the workload on the GPU, leading to smoother frame rates, especially
in complex scenes.
How it Works:
1. Depth Buffer: The graphics processing unit (GPU) maintains a depth buffer, which stores
the distance of each pixel on the screen from the camera. During rendering, when a
fragment (potential pixel) is processed, its depth is compared to the existing depth value
stored in the depth buffer for that pixel. If the fragment's depth is closer to the camera
(smaller value), it replaces the previous value and becomes visible. This ensures closer
objects always occlude (block) further ones, creating a realistic sense of depth.
2. Early Culling: Various techniques can be used for occlusion culling before even processing
fragment data. These techniques leverage the depth buffer or other methods to identify
objects that are likely hidden by closer objects and discard them from further rendering
pipelines.
Benefits:
Improved Performance: By eliminating unnecessary rendering of occluded objects,
occlusion culling significantly improves rendering speed and frame rates.
Increased Efficiency: The GPU can focus its processing power on objects that will actually
be visible in the final image.
Scalability: Occlusion culling becomes even more critical in complex scenes with numerous
objects, where it can make a substantial difference in performance.
Examples of Early Culling Techniques:
Back-Face Culling: Polygons facing away from the camera are discarded early on, as they
cannot be visible in the final image.
Frustum Culling: Objects entirely outside the view frustum (camera's viewable pyramid)
are culled, as they cannot contribute to the final image.
Depth Buffer Occupancy Tests: Techniques like occlusion queries can be used to check the
depth buffer and estimate if an object is likely hidden by closer objects before fully
processing it.
Occlusion culling is an essential technique for optimizing rendering performance in modern
computer graphics. By leveraging depth information and early culling strategies, it ensures efficient
use of GPU resources and smoother visual experiences.
3. Collision Detection
Collision detection is a fundamental concept in computer graphics, simulations, robotics, and
many other fields.
It essentially involves determining when two or more objects in a virtual space intersect
or come into contact with each other.
This information is crucial for various purposes, such as:
1. Realistic Interactions: In games and simulations, collision detection enables realistic
interactions between objects. For example, a car colliding with a wall should come to a stop, or a
character's movement should be blocked by a solid object.
2. Physical Simulations: Accurate collision detection is essential for physics simulations where
objects should bounce, roll, or break realistically based on their interactions.
3. Error Prevention: In robotics and virtual reality applications, collision detection helps prevent
virtual objects from passing through each other or the environment, ensuring safe and realistic
interactions.
1. Static Line-Object Intersections
Static line-object intersections in collision detection refer to identifying whether a static line
segment intersects with a static object in a scene.
This technique is commonly used in computer graphics, physics simulations, and games to
detect collisions between objects and prevent them from intersecting with each other.
1. Line Segment Representation: The static line segment is defined by two endpoints in 2D or
3D space. Each endpoint has coordinates (x1, y1, z1) and (x2, y2, z2), representing the start and
end points of the line segment, respectively.
2. Object Representation: The static object is represented by its geometry, typically as a
collection of vertices, edges, and faces. In collision detection, the object is often simplified to its
bounding volume (e.g., bounding box, bounding sphere) for efficiency.
3. Intersection Test: To check for intersection between the line segment and the object, various
algorithms can be employed. One common approach is to use ray intersection tests.
Simplified version of the ray intersection tests algorithm:
For each face or edge of the object:
Determine if the line segment intersects with the face or edge.
If an intersection is found, determine the intersection point.
Check if the intersection point lies within the bounds of the line segment.
4. Example: Consider a 2D scenario where you have a static line segment defined by two points
A(1, 1) and B(5, 5), and a static square object with vertices (2, 2), (2, 4), (4, 4), and (4, 2). To
check for intersection:
Check if the line segment intersects with any of the four edges of the square.
If an intersection is found, calculate the intersection point.
Determine if the intersection point lies within the bounds of the line segment (between
points A and B).
5. Collision Response: If an intersection is detected, collision response mechanisms can be
triggered, such as stopping the movement of the object, applying forces, or triggering events in
a game.
Static line-object intersection tests are essential for collision detection in computer graphics
and physics simulations.
Various algorithms can be used for intersection tests, including ray casting, line-segment
intersection tests, and separating axis theorem (SAT).
Efficiency considerations may lead to the use of bounding volumes or spatial partitioning
structures to reduce the number of intersection tests required.
Accurate collision detection is crucial for maintaining realism and preventing objects from
penetrating each other in simulations and games.
This technique is fundamental in computer graphics, physics engines, and games to prevent
objects from overlapping or penetrating each other, ensuring realism and accuracy in
simulations and virtual environments.
1. Object Representation: Each static object is represented by its geometry, typically as a
collection of vertices, edges, and faces. In collision detection, objects are often simplified to their
bounding volumes (e.g., bounding boxes, bounding spheres) for efficiency.
2. Intersection Test: To check for intersection between two static objects, various algorithms can
be used. Common approaches include:
Bounding Volume Intersection: Check if the bounding volumes of the objects
intersect. If they do, perform a more detailed collision detection test.
Separating Axis Theorem (SAT): Test if there exists an axis along which the projection
of the objects does not overlap. If such an axis exists, the objects do not intersect;
otherwise, a detailed collision check is needed.
Mesh Intersection: For complex geometry, perform detailed mesh intersection tests to
check for overlaps between faces, edges, or vertices.
3. Example: Consider two static objects: a cube and a sphere. To check for intersection:
Use bounding volume intersection tests to determine if the bounding box of the cube
intersects with the bounding sphere of the sphere.
If an intersection is found, perform a more detailed intersection test. For example, use
the SAT algorithm to check for overlaps along the separating axes of the cube and
sphere.
4. Collision Response: If an intersection is detected, collision response mechanisms can be
triggered, such as stopping the movement of the objects, applying forces, or triggering events in
a game.
Static object-object intersection tests are crucial for collision detection in computer
graphics, physics simulations, and games.
Various algorithms can be used for intersection tests, including bounding volume tests, SAT,
and mesh intersection tests.
Efficient collision detection often involves using bounding volumes or spatial partitioning
structures to reduce the number of detailed intersection tests required.
Accurate collision detection is essential for maintaining realism and preventing objects
from penetrating each other in simulations and games.
3. Specialized algorithms like those used for static line-object intersection (e.g., line-AABB
intersection, line-segment vs. other objects) can be employed.
Benefits:
Accuracy: This approach can provide more precise collision points compared to the
distance-based method.
Better Control: It allows for more granular control over collision detection by using
specific intersection algorithms.
Challenges:
Performance: Continuously checking for intersections can be computationally expensive,
especially for complex objects or many moving objects.
Missed Collisions: If the simulation steps are too large (low frame rate), fast-moving
objects might pass through objects between checks.
4. Dynamic Object-Object Intersections
Dynamic object-object intersections are fundamental in game development for various
aspects such as collision detection between moving entities, physics simulations, and
gameplay mechanics.
Example:
o A car and a motorbike are both moving on a road.
o You want to detect if they will crash into each other.
o In a fighting game, two characters might collide while fighting. Both characters are
moving, so the game checks if they intersect and if something happens (like damage)
when they collide.
Let's delve into how the distance-based and intersection-based approaches are utilized in game
development:
a. Distance-Based Approach:
In the distance-based approach, the intersection between two dynamic objects (entities that
can move or change position) is determined by calculating the distance between their
respective geometries or bounding volumes. If the distance falls below a certain threshold, a
collision is detected.
Example:
Consider a racing game where two cars are approaching each other. Using the distance-based
approach:
1. Calculate the distance between the bounding volumes of the two cars, such as their
bounding boxes or spheres.
2. If the distance between the bounding volumes is less than a predefined collision threshold,
consider it a collision.
3. Apply appropriate collision responses such as applying forces to the cars, reducing their
health points, or triggering visual effects.
b. Intersection-Based Approach:
In the intersection-based approach, the intersection between two dynamic objects is
determined by directly testing for overlaps or intersections between their geometries.
Example:
In the same racing game scenario:
1. Perform intersection tests between the geometries (e.g., meshes or hitboxes) of the two
cars.
4204 – APEC K. Chairmadurai, AP/CSE
CCS347 – GD Unit 3
2. If any intersections are detected between the car geometries, consider it a collision.
3. Apply collision responses as described earlier.
Both approaches have their applications depending on the specific requirements and
constraints of the game mechanics and performance considerations.
The distance-based approach is simpler and computationally less expensive but may lack
accuracy, especially for complex geometries.
The intersection-based approach provides more accurate results but may be
computationally more intensive, especially for scenes with many objects or complex
geometry.
Game developers often employ optimization techniques such as spatial partitioning,
bounding volume hierarchies, and parallel processing to improve the efficiency of dynamic
object-object intersection tests in real-time game environments.
The integration of game logic, AI, and pathfinding contributes to creating immersive and
dynamic gameplay experiences in modern video games.
Pathfinding is the solution to a deceptively simple problem: Given points A and B, how does an
AI intelligently traverse the game world between them?
The complexity of this problem stems from the fact that there can be a large set of paths
between points A and B but only one path is objectively the best. For example, in below Figure,
both the red and blue paths are potential routes from point A to B. However, the blue path is
objectively better than the red one, simply because it is shorter.
So it is not enough to simply find a single valid path between two points. The ideal pathfinding
algorithm needs to search through all of the possibilities and find the absolute best path.
Advantages:
Easy to understand and implement.
Efficient for pathfinding algorithms designed for grids.
Disadvantages:
May not accurately reflect the actual geometry of the game world, especially for
environments with diagonals or curves.
Can be computationally expensive for large worlds with high-resolution grids.
2. Graph-Based Search Space:
The world is represented as a network of nodes (locations) connected by edges (possible
paths).
Nodes can represent specific points of interest, areas of the environment, or intersections
between walkable areas.
Edges can be weighted to represent different movement costs (e.g., walking on flat ground
vs. climbing a slope)
A graph contains a set of nodes , which can be connected to any number of adjacent nodes
via edges .
There are many different ways to represent a graph in memory, but the most basic is an
adjacency list. In this representation, each node contains a list of pointers to any adjacent
nodes.
The entire set of nodes in the graph can then be stored in a standard container data
structure. The following Figure illustrates the visual and data representations of a simple
graph.
.
Advantages:
More flexible than grids, allowing for representation of complex environments with
diagonals and curves.
Can incorporate different movement costs for different types of terrain.
Disadvantages:
Designing the graph can be more time-consuming compared to a grid-based approach.
Pathfinding algorithms for graphs might be slightly more complex than those for grids.
3. Navigation Meshes (NavMeshes):
A pre-processed, simplified representation of the walkable areas within the environment.
Often used in modern game engines for pathfinding.
NavMeshes are typically created from the game world geometry using specialized
algorithms.
This method represents the walkable space as connected polygons (usually convex shapes
like triangles or rectangles).
AI can move freely within each polygon, not just along fixed paths.
Used in modern 3D and complex 2D games for smoother and more realistic navigation.
Advantages:
Efficient for pathfinding due to their simplified nature.
Accurately represent walkable areas in complex environments.
Commonly used in game engines with built-in tools for NavMesh generation.
Disadvantages:
Require pre-processing, which can be time-consuming for large or dynamic environments.
Limited control over the exact path taken by the agent compared to a graph-based
approach.
Admissible Heuristics
All pathfinding algorithms need a way to mathematically evaluate which node should be
selected next. Most algorithms that might be used in a game utilize a heuristic , represented by
h(x) . This is the estimated cost from one particular node to the goal node. Ideally, the heuristic
should be as close to the actual cost as possible.
Admissible heuristics play a crucial role in guiding path finding algorithms towards finding
optimal or near-optimal solutions while minimizing computational overhead.
An admissible heuristic is a function that provides an estimate of the cost from a given node to
the goal node in a search space.
A heuristic is considered admissible if the estimate is guaranteed to always be less than or equal
to the actual cost. If a heuristic overestimates the actual cost, there is decent probability that the
pathfinding algorithm will not discover the best route.
Admissible heuristics are commonly used in informed search algorithms such as A* and Greedy
Best-First Search to prioritize nodes for exploration.
Two common admissible heuristics used in path finding algorithms.
1. Manhattan Heuristic:
The Manhattan heuristic, also known as the city-block distance or L1 norm, provides an
estimate of the shortest distance between two points in a grid-based search space.
It calculates the distance by summing the absolute differences in the x and y coordinates
between the current node and the goal node.
Formula: The Manhattan distance ℎManhattan between two points (x1,y1) and (x2,y2) is given by:
hManhattan=∣x2−x1∣+∣y2−y1∣
Properties:
The Manhattan heuristic provides a conservative estimate of the true cost to reach the goal.
It is suitable for grid-based search spaces where movement is restricted to horizontal and
vertical directions (e.g., tile-based maps).
Example: Consider a grid-based map where the current node is at coordinates (x1,y1) and the goal
node is at coordinates (x2,y2). The Manhattan distance between these points would be the sum of
the absolute differences in their x and y coordinates: hManhattan=∣x2−x1∣+∣y2−y1∣
2. Euclidean Heuristic:
The Euclidean heuristic, also known as the straight-line distance or L2 norm, estimates the
shortest distance between two points in continuous space.
It calculates the distance using the Pythagorean theorem, treating the nodes' coordinates as the
lengths of the sides of a right-angled triangle.
Formula: The Euclidean distance ℎEuclidean between two points (x1,y1) and (x2,y2) is given by:
hEuclidean=(x2−x1)2+(y2−y1)2
Properties:
The Euclidean heuristic provides a more accurate estimate of distance in continuous spaces
or when movement is not restricted to orthogonal directions.
It tends to be more computationally expensive to compute compared to the Manhattan
heuristic due to the square root operation.
Example: In a 2D continuous space, consider the current node at coordinates (x1,y1) and the goal
node at coordinates (x2,y2). The Euclidean distance between these points would be the square root
of the sum of the squares of their absolute differences in x and y coordinates: 2hEuclidean=(x2−x1
)2+(y2−y1)2
Summary:
The Manhattan heuristic provides a conservative estimate suitable for grid-based search
spaces with orthogonal movement.
The Euclidean heuristic offers a more accurate estimate appropriate for continuous spaces
with unrestricted movement.
Both heuristics guide pathfinding algorithms towards the goal while ensuring admissibility
by never overestimating the true cost to reach the destination.
Steps:
1. Start at the current node (character's location).
2. Evaluate all neighboring nodes using the heuristic function. This provides an estimate of how
"close" each neighbor is to the goal.
3. Move to the neighbor with the lowest estimated cost (appearing closest to the goal based on the
heuristic).
4. Repeat steps 2 and 3 until the goal node is reached or a dead end is encountered (no neighbors
with lower estimated costs).
Advantages:
Simple to understand and implement: The core logic is straightforward, making it a good
starting point for learning pathfinding algorithms.
Can find reasonable paths quickly: By prioritizing seemingly close neighbors, it can often
find a solution relatively fast, especially for simple environments.
Disadvantages:
Can get stuck in local optima: The algorithm might choose a seemingly close neighbor that
leads away from the actual goal. Imagine a maze with a dead end that appears to be the exit
based on the heuristic.
Not guaranteed to find the shortest path: The focus on estimated cost can lead to sub-
optimal paths, especially in complex environments.
Example:
The Greedy Best-First Search Algorithm prioritizes exploring paths that seem closest to the goal
based on a chosen heuristic function. Here's a step-by-step example to illustrate its workings:
Scenario:
Imagine a character in a maze-like environment represented as a grid. The goal is to find a path
from the starting point (S) to the exit (E). We'll use a simple heuristic: the Manhattan distance (sum
of horizontal and vertical steps) between a node and the exit.
Grid Representation:
Steps:
1. Start at the current node (S).
Current node: S
Neighbors: All surrounding nodes (North, East, South, West) that are not walls. In this case,
neighbors are (1, 0) and (0, 1).
2. Evaluate neighboring nodes using the heuristic (Manhattan distance to the exit).
Node (1, 0): Manhattan distance = 1 (one step east)
Node (0, 1): Manhattan distance = 1 (one step north)
3. Move to the neighbor with the lowest estimated cost.
Both neighbors have the same estimated cost (1). Arbitrarily choose one (let's pick east, so
the new current node is (1, 0)).
4. Repeat steps 2 and 3 until the goal is reached or a dead end is encountered.
Current node: (1, 0)
Neighbors: (2, 0) and (1, 1)
Heuristic for neighbors:
o (2, 0): Manhattan distance = 1 (one step east)
o (1, 1): Manhattan distance = 2 (one step north, one step east)
Move to the neighbor with the lowest estimated cost (east again, so the new current node is
(2, 0)).
5. Repeat steps 2 and 3.
Current node: (2, 0)
Neighbors: (3, 0) and (2, 1) (assuming no wall to the south)
Heuristic for neighbors:
o (3, 0): Manhattan distance = 1 (one step east) Selects this one
o (2, 1): Manhattan distance = 2 (one step south, one step east)
6. Continue repeating until the goal is reached.
The algorithm will continue prioritizing eastward moves due to the heuristic, eventually
reaching the exit (E) at (4, 1).
Observations:
The Greedy Best-First Search Algorithm did not find the shortest path to the treasure due to the
obstacle and its focus on eastward moves.
It might have found a better path if it had explored the south neighbor of (2, 0) earlier, but the
eastward bias due to the heuristic prevented that.
Key Points:
Greedy Best-First Search can be a good starting point for pathfinding due to its simplicity.
It can get stuck in local optima, prioritizing seemingly close neighbors that don't lead to the optimal
path, especially in the presence of obstacles.
A* Search, which incorporates the actual cost traveled so far, can often find the shortest path and
avoid such dead ends.
2. A* Search Algorithm:
A* Search is a widely used pathfinding algorithm in artificial intelligence (AI) for games and
other applications.
It builds upon Greedy Best-First Search by incorporating both the estimated cost to the goal
(heuristic) and the actual cost traveled so far, leading to more efficient path finding with a focus
on finding the shortest path.
Instead of solely relying on the h(x) admissible heuristic, the A* algorithm (pronounced A-star)
adds a path-cost component.
The path-cost is the actual cost from the start node to the current node, and is denoted by g (x) .
The equation for the total cost to visit a node in A* then becomes:
Core Idea:
Imagine a character navigating a game world. A* Search maintains a list of nodes (locations) to
explore, evaluating each one based on two factors:
g(n): The actual cost traveled so far from the starting point to the current node (n).
h(n): A heuristic estimate of the cost from the current node (n) to the goal node.
A* prioritizes exploring nodes with the lowest f(n), which is the sum of g(n) and h(n). This
combines the strengths of both considering the progress made so far (g(n)) and the
estimated remaining cost (h(n)).
There is only one other major change to the code for the A* algorithm, and that is the
concept of node adoption. In the best-first algorithm, adjacent nodes always have their
parent set to the current node. However in A*, adjacent nodes that are already in the open
set need to have their value evaluated to determine whether the current node is a superior
parent.
Steps:
1. Start at the start node.
2. For each neighbor of the current node:
o If it's already visited (in closed set), skip it.
o If it's in the open set:
Check if going through the current node gives a cheaper path.
If yes, update parent and g, f values.
o If it's not in the open set:
Set its parent.
Compute g, h, and f.
Add it to the open set.
3. Pick the node with the lowest f in the open set as the new current node.
4. Repeat until you reach the goal or open set is empty.
5. If the goal is reached, reconstruct the path by following parent pointers.
Advantages:
Guaranteed to find the shortest path (if the heuristic is admissible): Unlike Greedy
Best-First Search, A* Search considers the actual cost traveled so far, preventing it from
getting stuck in local optima and ensuring it finds the most efficient path to the goal when
the heuristic is admissible (never overestimates the actual cost).
Balances efficiency and optimality: A* often finds the shortest path quickly, especially
with a good heuristic function.
Disadvantages:
More complex to implement compared to Greedy Best-First Search: It involves
maintaining two lists (Open and Closed) and additional calculations for f(n).
Relies on a good heuristic function: The performance of A* depends on the quality of the
chosen heuristic. An inaccurate heuristic can lead to suboptimal paths.
3.Dijkstra’s Algorithm
One final pathfinding algorithm can be implemented with only a minor modification to A*.
In Dijkstra’s algorithm , there is no heuristic estimate—or in other words:
This means that Dijkstra’s algorithm can be implemented with the same code as A* if we use
zero as the heuristic.
If we apply Dijkstra’s algorithm to our sample data set, we get a path that is identical to the one
generated by A*.
Provided that the heuristic used for A* was admissible, Dijkstra’s algorithm will always return
the same path as A*. However, Dijkstra’s algorithm typically ends up visiting more nodes, which
means it’s less efficient than A*.
The only scenario where Dijkstra’s might be used instead of A* is when there are multiple valid
goal nodes, but you have no idea which one is the closest.
But that scenario is rare enough that most games do not use Dijkstra’s; the algorithm is mainly
discussed for historical reasons, as Dijkstra’s algorithm actually predates A* by nearly ten years.
A* was an innovation that combined both the greedy best-first search and Dijkstra’s algorithm.