简体   繁体   中英

Pathfinding in platform game in C++

I want to find path in 2d platform game, teeworlds . The player there can move left/right, jump, and use hook, that lets you move upward the wall or move under ceiling. Well, its hard becouse normal pathfinds like a* or bfs cant exist here, cuz you cant just move up. I need to find path btw 2 players so 1 can go to the second one. There are 3 types of tiles, collide, nohook (you cant hook it) and nothing (air). I have the map in format int map[w][h] where 0=air, 1=collide, 2=nohook. map isnt modified for whole game time.

I have completly no idea how to do that. If you can help me, I'd be pleased.

PS. The question is general about platform games, teeworlds is only one of them...

从寻路算法的角度来看,您可以将可爬行墙视为正常走道,因此算法并不止于此。

I don't think I completely understand the possibilities in your game. But this is how it works in general:

Path finding algorithms work on graphs (directed, undirected, with costs or equal costs for all edges doesn't matter). All you have to do is model your graph according to your game's rules. Ie if there is only a normal way between field i and j than cost(i,j) = normal, if there is an additional way to use a hook, it could would be that cost(i,j) = min(hook, normal) . and so on. Once you have a graph (i assume it has to be directed for your game) all the normal Pathfinding algorithms will work.

if there are requirements like "you can only use hook n-times", a multi-label dijsktra can work.

pathfinding algorithms work on graphs in general

movement from one cell to another means that there is a connection between two cells (for instance in four directions). But you can also link to some other cell (the ceiling).

A* as a general search algorithm can still be applicable here, you just need to find a way to represent your platform game path-finding problem as a general search problem. So while you might have been introduced to A* as an algorithm to find a path through a grid-like maze, that is just a specific case of such a general search problem.

一个迷宫

The more general case being graphs, where edges represent moves or actions and nodes represent positions or states.

搜索图表

One way to use A* for the game you mention is by taking an example from this Infinite Mario AI . This implementation of A* works locally and is rerun every tick to get the most optimal actions at that exact moment which is great for a fast-paced game like Mario or the game you mention, but be mindful that this is different from A* when it's used to solve a maze, in which case the path is found only once and it is like a global plan to be followed until its end.

无限马里奥AI

使用简单的BFS方法,将对预处理到节点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM