© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•12mo ago•
7 replies
Theos

Optimize graph generation for 2D grid

Hey, I'm working on a 2D game. Right now I'm adding enemies and decided to use A* for their pathfinding.
Player can place/destroy tiles so the graph needs to be updated based on that.
I was thinking about placing path nodes above each tile and connect them with their neighbors. The hardest part is making connections that require enemies to jump.
Only approach that comes to my mind is something like this (pseudocode):
foreach node in nodes
  var nodesInRange = GetNodesInRange(range)
  foreach nodeInRange in nodesInRange
    if(canJumpTo(from: node, to: nodeInRange)) node.addConnection(nodeInRange, type: jump)
foreach node in nodes
  var nodesInRange = GetNodesInRange(range)
  foreach nodeInRange in nodesInRange
    if(canJumpTo(from: node, to: nodeInRange)) node.addConnection(nodeInRange, type: jump)

I would somehow need to check if jump is possible, based on monster's jump power, mass and gravity. Monsters can have different sizes and jump powers so I would need to calculate graphs for each monster separately. Each tile place/destroy means recalculating the graph. Also I can't generate graph for the whole world because world is huge (4096x4096 tiles), so I would need to generate graph for only some chunk of the map, and then when the monster moves generate graph again for the new terrain thats in range.
There is a lot of problems and I'm not sure how to approach it.
Could you guide me a bit? Thanks!
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Convert 2D grid into graph
C#CC# / help
3y ago
❔ Optimize Compiler Code Generation
C#CC# / help
4y ago
Making 2d world generation deterministic
C#CC# / help
2y ago
2D Movement
C#CC# / help
11mo ago