Implementationof Bellman Ford Algorithmfor Dynamic NPCPathfindinin Horror Games
Implementationof Bellman Ford Algorithmfor Dynamic NPCPathfindinin Horror Games
net/publication/379603862
CITATIONS READS
0 318
9 authors, including:
Princess Orbillo
Far Eastern University
1 PUBLICATION 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Princess Orbillo on 06 April 2024.
IN HORROR GAMES”
A Research Study
at FEU Alabang
In CCS0007 Algorithms
Orbillo, Princess M.
April 2024
1
ABSTRACT
Pathfinding in video games is crucial for creating immersive and challenging gameplay
experiences, particularly in horror games where NPC behavior greatly contributes to atmosphere
and tension. The Bellman-Ford algorithm, known for its versatility and suitability, is adapted to
handle dynamic environments and address changing terrains and unpredictable player movements,
even with negative edges present in a weighted graph. This study investigates the implementation
methodology involves integrating the algorithm into Roblox Studio and simulating the NPC's
behavior in response to obstacles, terrains, and the player's actions. Results indicate that the
Studio
2
TABLE OF CONTENTS
TITLE PAGE……………………………………………………………………………………...1
ABSTRACT………………………………………………………………………………………2
INTRODUCTION………………………………………………………………………………...4
METHODOLOGY………………………………………………………………………………..7
RESULTS………………………………………………………………………………………12
RECOMMENDATION………………………………………………………………………...13
REFERENCES…………………………………………………………………………………14
3
INTRODUCTION
Every successful video game features a few common elements, such as clear objectives,
characters, story, and music (Juegoadmin, 2023). Non-player characters, most commonly known
as NPCs, are one of them. According to Foudil et al. (2009) and Hill (2024), an NPC is a game
object not controlled by a player used to identify between characters the player controls and
characters the game controls completely. They are important in games as they assist in the
advancement of the main plot by interacting with players, offering helpful advice, and assigning
the main character to new objectives. NPCs also give the game vitality and add depth, complexity,
and realism, which greatly increases its excitement (Millington & Funge, 2009). In addition,
Montelli (2021) stated that NPCs hold importance in a game's narrative by portraying villains or
other important characters, enriching immersive gaming experiences with backstories, dialogue
In horror video games, non-player characters (NPCs) play a significant role in enhancing
both gameplay mechanics and narrative immersion, shaping the virtual world and enhancing the
player's journey (Millington & Funge, 2009). According to Yu (2022), NPCs contribute to
inducing feelings of fear, anxiety, or panic within players. By effectively integrating NPCs into
the game environment, developers aim to leave a lasting impact on players even after they have
concluded their gaming sessions. They serve as catalysts for eliciting fear and amplifying players'
immersive experiences through strategic integration within gameplay and narrative frameworks.
4
The foundation of NPC behavior in games lies in the design of efficient pathfinding
algorithms, which are key components of the underlying Artificial Intelligence (AI) models
(Morina & Rafuna, 2023). As stated by Hunkeler et al. (2016), pathfinding is a plotting node by a
computer application to find the shortest route between two points from source to destination,
aiming to find the minimum total weight path in a graph between pairs of nodes (Cui & Shi, 2011).
Hunkeler et al. (2016) added that pathfinding plays a significant role in video game applications
as it is essential for developing realistic NPCs in static, dynamic, and real-time game environments.
These techniques used to determine the path in video games are called pathfinding algorithms,
which are used by the non-player character (NPC) to find a path between the starting point to the
desired point. Common pathfinding algorithms used in video games are Dijkstra and A*
algorithms. (Rafiq et al., 2020, p. 2). Pathfinding algorithms address the problem of solving for
the shortest and most efficient path from origin to destination and avoiding obstacles (Barnouti et
al., 2016).
When it comes to finding the shortest path in graphs with weighted edges, the Bellman-
Ford algorithm is another essential technique that programmers can use in pathfinding. The
Bellman-Ford algorithm is a pathfinding algorithm that computes the shortest path in a weighted
graph from a source vertex to all other vertices. Unlike other pathfinding algorithms, such as
Dijkstra’s, which can only work with non-negative weights, the Bellman-Ford is also designed to
handle negative weighted edges. It can recognize and manage negative weight cycles, in which the
sum of the weights along a cycle is negative. The main principle behind this algorithm is to
gradually update the distance estimations for each vertex while iteratively relaxing the graph’s
edges until the shortest paths are identified (Bhuyan, 2023). In video game development, the
5
Bellman-Ford algorithm assists NPCs in navigating through the game world efficiently. It helps
determine the optimal path for NPCs, considering obstacles, terrain, and other dynamic factors,
enhancing the realism and intelligence of in-game entities (Cloud Native Journey, 2023).
The developers aim to implement the Bellman-Ford algorithm for the dynamic pathfinding
of the non-player characters (NPCs) in horror games. This algorithm, known for its efficiency in
finding the shortest paths with negative edges present in weighted graphs, will be utilized to create
immersive experiences within the game environment. This will also assist the developers in
crafting suspenseful paths and unpredictable enemy movements, enhancing players' gaming
experience. By employing the Bellman-Ford algorithm, the NPCs will navigate through the
terrains and respond intelligently to dynamic conditions, adding depth and realism to player
interactions.
6
METHODOLOGY
The developers, inspired by the narrative and gameplay elements highlighted in horror
video games, meticulously defined the eerie game environment within Roblox Studio, crafting its
layout, obstacles, and dynamic elements capable of perturbing NPC navigation. Drawing from the
rich descriptions provided in the introduction, such as inducing feelings of fear, anxiety, and panic,
the developers ensured that the game environment was intricately designed to evoke such
emotions.
Within Roblox Studio, they translated the complexities of this environment into a graph
representation, where each node symbolized a discrete location within the sinister world, while
edges represented connections delineating feasible pathways. These edges were assigned weights
based on factors like distance, terrain complexities, and other game dynamics, ensuring that NPCs
To implement the Bellman-Ford Algorithm, the developers first initialized distances from
the source NPC to all other nodes in the graph within Roblox Studio, setting them to infinity except
for the source node itself, designated as 0. They meticulously iterated through all edges,
systematically relaxing them to identify shorter paths to destination nodes. The process continued
for a set number of iterations, with careful attention paid to identifying and addressing negative
Upon completion of iterations, distances to all nodes from the source NPC were updated
with the shortest paths, laying the foundation for optimized NPC navigation within the eerie game
7
environment. Leveraging Roblox Studio's capabilities, the developers orchestrated NPC
movement along these calculated shortest paths, ensuring NPCs adeptly navigated around
To maintain immersion and suspense, the developers established a framework for dynamic
updates within Roblox Studio, continuously refining the graph representation and recalibrating
shortest paths as the game unfolded. This iterative process involved managing emergent obstacles
and shifts in NPC objectives to sustain the atmosphere of dread and uncertainty.
Furthermore, optimization efforts within Roblox Studio were undertaken to enhance the
efficiency of the pathfinding algorithm, including caching computed paths and employing spatial
partitioning techniques to minimize computational overhead. Rigorous testing within the Roblox
environment evaluated NPC pathfinding across diverse scenarios, ensuring alignment with the
Bellman-Ford algorithm for dynamic NPC pathfinding, enhancing both gameplay mechanics and
The provided code segments implement the Bellman-Ford algorithm for dynamic NPC
pathfinding in a Roblox game environment, specifically for killer and police NPCs. The
initialization phase defines spawn points for both types of NPCs and creates a graph representing
potential paths between these spawn points and various intersections within the game world. The
8
Bellman-Ford algorithm is then applied to calculate the shortest paths from the nearest spawn point
`findNearestPoliceSpawn` determine the closest spawn point based on the player's location.
path from the player to the NPC's spawn point. Finally, the NPCs' movements are orchestrated
using the `SetPrimaryPartCFrame` method, adjusting direction and speed along the computed path
Overall, this integrated approach ensures efficient and adaptable NPC navigation, allowing
them to autonomously traverse the game environment while responding intelligently to changes in
9
Killer NPC Source Code:
10
Police NPC Source Code:
11
RESULTS
The implementation of the Bellman-Ford algorithm for dynamic NPC pathfinding in horror
games yielded several notable results. Firstly, the algorithm demonstrated efficient pathfinding
terrains and obstacles. Its adaptability was evident as it successfully handled unpredictable player
Ford's ability to manage negative weighted edges ensured that NPCs could traverse challenging
terrain configurations without getting stuck, contributing to smoother gameplay experiences. The
algorithm's impact on realism and immersion was significant, with NPCs showcasing intelligent
responses to environmental changes, enhancing gameplay realism and immersing players deeper
into the horror-themed world. Despite the computational complexity inherent in the Bellman-Ford
ensuring smooth gameplay experiences for players navigating through the game world alongside
dynamic NPCs.
12
RECOMMENDATIONS
Lastly, exploring ways to integrate player interactions into NPC pathfinding decisions can
create dynamic and engaging gameplay experiences. By incorporating these recommendations,
developers can further optimize the use of the Bellman-Ford algorithm, contributing to a more
immersive and engaging horror gaming experience overall.
13
REFERENCES
Barnouti, N. H., Al-Dabbagh, S. S. M., & Naser, M. a. S. (2016b). Pathfinding in strategy games and
maze solving using a* search algorithm. Journal of Computer and Communications, 04(11), 15–25.
https://doi.org/10.4236/jcc.2016.411002
Bellman-Ford Algorithm: A pathfinding algorithm for weighted graphs. (2023, June 5). Cloud Native
Journey. https://cloudnativejourney.wordpress.com/2023/06/05/bellman-ford-algorithm-a-pathfinding-
algorithm-for-weighted-
graphs/#:~:text=The%20Bellman%2DFord%20algorithm%20helps,intelligence%20of%20in%2Dgame%
20entities.
Bhuyan, A. (2023, June 5). Bellman-Ford Algorithm: A pathfinding algorithm for weighted graphs.
Medium. https://aditya-sunjava.medium.com/bellman-ford-algorithm-a-pathfinding-algorithm-for-
weighted-graphs-647080392326
C. Foudil, D. Noureddine, C. Sanza, and Y. Duthen, “Path Finding and Collision Avoidance in Crowd
Coyle, N. (2020b, November 19). The Psychology of horror games - Psychology and video games.
Cui, X. and Shi, H. (2011) A*-Based Pathfinding in Modern Computer Games. International Journal of
https://www.digitaltrends.com/gaming/what-is-an-npc/
14
I. Hunkeler, F. Schar, R. Dornberger, and T. Hanne, “FairGhosts - Ant colony controlled ghosts for Ms.
Pac-Man,” 2016 IEEE Congr. Evol. Comput. CEC 2016, pp. 4214–4220, 2016.
Juegoadmin. (2023b, September 22). Major game design elements. Juego Studio.
https://www.juegostudio.com/blog/game-design-elements
Millington, I., & Funge, J. (2009). Artificial Intelligence for Games. CRC Press.
Montelli, C. (2022b, August 23). What does “NPC” mean? Understanding non-player characters, an
meaning
Morina, V., & Rafuna, R. (2023, October). A Comparative Analysis of Pathfinding Algorithms in NPC
https://www.researchgate.net/publication/375895663_A_Comparative_Analysis_of_Pathfinding_Algorith
ms_in_NPC_Movement_Systems_for_Computer_Games
Rafiq, A., Kadir, T. a. A., & Ihsan, S. N. (2020b). Pathfinding Algorithms in game development. IOP
899x/769/1/012021
What is an NPC and Why It Matters for your Game Storytelling. (n.d.-c).
https://www.rosebud.ai/blog/what-is-an-npc-video-games-storytelling
15