Heuristic Path Finding

My game is a top down 2d twin stick hack n slash.

I have an enemy object that I want to continuously move towards the player object. I figured a simplified version of Heuristic pathfinding would be the best way to do this whereby every time enemy.update() is called the following happens:

  • Create a point for each of the cardinal directions (N, NE, E…etc) around the enemy object at a distance of one Step(predefined movement amount in pixels) away from the enemy object
  • Loop through these and check which point would make the enemy object closer to the PC
  • Move the enemy object in that direction

My thoughts on how to do this are to create an ArrayList containing the possible movement directions then subtract the elements from the player position in turn to give the distance from the player, then select the smallest distance as the direction the object moves.

In terms of implementation I am not sure whether to use points or vectors. I have done a little research and found nothing that implies I can use mathematical operators on points, is this possible? And if not should I use vectors and can someone point me to a resource on how to implement this?

Thanks in advance :slight_smile:

You can store locations in vectors though, and multiply with the movement you want to travel in.

Having never really used vectors before, could you point me to a good resource?

Read a little more into it and I’m going to use pythagoras to calculate the difference between points, thanks for the help :slight_smile:

For movement, you can do this:


Vec2f.x += sin(rotation) * step;
Vec2f.y += cos(rotation) * step;

You’ll move in the direction of the rotation.

It’s sounding like you want an A* or D*-Lite (or similar) implementation…there are bagillions of ways to do this kind of thing.

It’s less that I need an understanding of path finding and more the implementation of it in Java.

I’d really appreciate being pointed to a article about using vectors in Java.

Humm…well vectors aren’t what you really need if you’re sticking to 8 directions…which is what I’m understanding.

That’s what I thought I just needed some clarification, here’s some pseudo code of my thinking so far:


private double dx;
private double dy;
private float distance;
private float smallDistance = 1000.0f;
ArrayList moveList = new ArrayList;

Point currentPos = new Point;
Point move = new Point;

Point nw = new Point;
Point n = new Point;
//... create points for each direction

currentPos.setX(enemy.posX);
currentPos.setY(enemy.posY);

nw = (currentPos.translate(-step,-step))
n = (currentPos.translate(0,-step))
//...set values for each direction.


moveList.add(nw);
moveList.add(n);
//...add all directions to list

for(i=0; i < moveList.size(); i++){
dx= (player.getX() - moveList[i].getX();
dy= (player.getY() - moveList[i].getY();

distance = sqrt((dx*dx) + (dy*dy));

if(distance < smallDistance){
smallDistance = distance; //if current point has shortest distance from Player set smallDistance = distance
move.setLocation(moveList[i]); //set the final move position to current point
}//endif
}//end for

It would be really helpful if you would describe what you want. Like CAN you only move in 8 directions or is it unconstrained?

I have simplified the path finding to make it easier for me to code as this is my first from scratch project.

So using my pathfinding method the enemy object would be constrained to one of the 8 directions.

All I really want at the moment is to get a rudimentry pathfinding algorithm working so that the enemies move towards the player character with roughly the shortest path on every call of enemy.update().

So does my pseudo code look like it would achieve that?

I’d strongly advise that you look-up a description of doing A* on a grid, implement and understand that. Reinventing the wheel can be fine, but you should understand how others have gone about making wheels first. This is one of those topics that seems simple…but it is far from.

A-star is a very simple algorithm. Going from sort of understanding it to completely understanding it requires a mental leap. And sort of understanding it is almost like not understanding it at all. It is the type of thing where a premade library will not help you if you do not understand how it is implemented.