I've been thinking about how to do path finding for my 2D platformer, I want the enemies to be able to find their way towards the player and jump on to higher platforms to reach them... I looked into the A* algorithm but it seems to be more suited to top down games and I was wondering if anyone had any good ideas regarding platformer pathfinding?

A* can be used on any graph to find a route from one node to another, not just top down maps. You just need to find a way of representing your platform level as a suitable graph that can be traversed.

Previously I've done that by generating waypoints at regular intervals along the surface of the platforms, then generating links between adjacent waypoints, like this:Here, the greed diamonds are waypoints and the white lines the available routes between nearby waypoints. You'll need to generate links not just from waypoints on the same platform, but between waypoints on different platforms that you can jump between.

I think, what Orangy Tang writes, is something really intresting, but I think you should go with a simplified AI, if that would fit your need:

Enemys just walk towards your player (in the direction to the player, which means either left or right), and if they have no ground below anymore, they just jump. Thats the simliest AI. It is used really often.

A* can be used on any graph to find a route from one node to another, not just top down maps. You just need to find a way of representing your platform level as a suitable graph that can be traversed.

Previously I've done that by generating waypoints at regular intervals along the surface of the platforms, then generating links between adjacent waypoints, like this:Here, the greed diamonds are waypoints and the white lines the available routes between nearby waypoints. You'll need to generate links not just from waypoints on the same platform, but between waypoints on different platforms that you can jump between.

How would I make this look natural, so the entity doesn't just fly there following straight lines, it jumps, and the jump curves like a normal jump...?

How would I make this look natural, so the entity doesn't just fly there following straight lines, it jumps, and the jump curves like a normal jump...?

The A* result will only describe the waypoints the enemy needs to take - how they actually navigate between the two waypoints is up to you. Ideally you want your waypoints to be dense enough that navigating from one to the next is trivial (eg. just move left or right, or jump in a direction).

In order for them to reach the player or some position like the powerup, they try out (simulate) many combinations of movement-commands.

The combination resulting in the position closest to the target is chosen.This is quite CPU intensive though.

But when combining this with an A* based search and a well defined navigation-mesh or waypoints, this approach looks very dynamic.

As your game will probably not have the enemies find you from like 100 tiles away, but follow you in close distance, an A* based actualpathfinding might be overkill. In this case I would just give the AI some information about "jump-point" to reach a level above, andelse just walk into the direction closest to the player.

How would I go about simulating the movements?It would mean anticipating collision, the NPCs knowing when/where to jump...Also, in the future I want to have some team-mate NPCs who follow and fight with the player, would a similar system work for this?

The same way you simulate the movements for the player? We're rapidly getting into "write my game for me" territory I think. What have you tried? What didn't work?

For jumping, I originally had the enemy follow a bezier curve between the two waypoints. Later I changed it to just simulate a proper jump - finding the launch speed and angle is the only tricky bit, but that's just a bit of trig really.

What I did in the 4k game (since its 4 kilobytes, there was no way to insert a node-based pathfinding)was to create an array of like 2 second movement-command sequences.They where partly random, and partly based on some repeating patterns (like walking left for 1 secon, then jumping)

The commands included left,right,and jump, in like a 0.1sec resolution

In the n end, its basicaly just a sequence of how to move for the next 2 seconds.

I think I made like 100 sequences per AI to simulate.

Next, these sequences are taken and simulated (basically just moving the AI based on these commands, why applying the collisionphysics)The endposition and possible HP reduction (lava) are evaluated.The sequence wich brings the AI closest to the goal (other AI or pickup) is then favored as best movesequence.

Then the movesequence is executed for 2 seconds, until the next simulation is done.

The effect is that the AI can handle the environment very dynamic without knowing too much about it.It will most of the time find a meaningful way, UNLESS the 2 seconds are not enough time (local maxima, the AI might walk to a trap-point)

It works well for the short-distance search.But for long distance search you also would need to add some node-based pathfinding. The short distance AI can then be used to reach the waypoints calculated by thelong distance search.

The same way you simulate the movements for the player? We're rapidly getting into "write my game for me" territory I think. What have you tried? What didn't work?

For jumping, I originally had the enemy follow a bezier curve between the two waypoints. Later I changed it to just simulate a proper jump - finding the launch speed and angle is the only tricky bit, but that's just a bit of trig really.

What I meant was, I'm not simulating the movement for my player, I'm just moving them, by simulating I assume that means playing through possible movements without actually moving the NPC and seeing which one is best, I'm not asking anyone to write my game for me, as then I wouldn't learn anything and I couldn't feel pride in the final product, I'm sorry if it came across that way, but this is my first real game attempt and I'm new to this...

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org