Mugen87 / yuka

JavaScript library for developing Game AI.
https://mugen87.github.io/yuka/
MIT License
1.1k stars 90 forks source link

Is there any way to add obstacles dynamically on a 2D grid? #64

Closed LeCommandeur closed 2 years ago

LeCommandeur commented 2 years ago

Hi !

I'm currently working on a small video game where players have to barricade their characters inside a house by putting furnitures along some "open walls" to block the path to prevent ennemies from getting to them.

The main problem is the dynamic nature of the game, we move blocking objects thus we change the valid paths present in the grid.

I have created a 2D grid using GraphUtils.createGridLayout and tried two different ways to "update" the graph dynamically.

First, I tried removing nodes in the graph where there are blocking objects but alas, it did not work well, the paths I then got were incorrect and sometimes not even continuous.

Then, I tried to simply update manually the cost of certain nodes' edges so that they would be less likely to be consider better suited for the path. It works but I wonder if there is a better approach?

Could you recommand anything? Is there a special value we can use to make certain nodes "unwalkable"?

Thanks in advance!

Mugen87 commented 2 years ago

Then, I tried to simply update manually the cost of certain nodes' edges so that they would be less likely to be consider better suited for the path. It works but I wonder if there is a better approach?

This approach is actually good! I know from older strategy games that they use the same technique to guide game entities over the map. Meaning they represent the entire map as a grid and assign different costs to the edges depending on the terrain's quality. E.g. roads and ways have low costs, fields have mid costs and stuff like rivers, mud or dangerous terrain (think of a mine field) high costs (like Infinity).

Depending on how your game is structured I would start with this approach and see how good it works.

Mugen87 commented 2 years ago

BTW: Of course you could change the structure of the grid depending on the terrain but updating edge costs is more flexible. Think of a bridge in a strategy game. If it is destroyed, game entities should not pass it. That can be achieved by assigning a high cost to the respective edge so game entities will try to find a way around it. When the bridge is repaired (assuming the game provides such a mechanic), the original edge cost is restored.

LeCommandeur commented 2 years ago

Thank you for your quick answer and your advice. I finally manage to achieve what I was looking for, I'm closing the issue!