Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Heuristic Path Finding  (Read 1306 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2013-01-25 10:35:43 »

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 Smiley
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #1 - Posted 2013-01-25 10:38:26 »

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

Offline westloar

Junior Member





« Reply #2 - Posted 2013-01-25 10:51:01 »

Having never really used vectors before, could you point me to a good resource?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #3 - Posted 2013-01-25 11:38:06 »

Read a little more into it and I'm going to use pythagoras to calculate the difference between points, thanks for the help Smiley
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2013-01-25 12:05:25 »

For movement, you can do this:
1  
2  
Vec2f.x += sin(rotation) * step;
Vec2f.y += cos(rotation) * step;


You'll move in the direction of the rotation.

Offline Roquen
« Reply #5 - Posted 2013-01-25 12:24:23 »

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.
Offline westloar

Junior Member





« Reply #6 - Posted 2013-01-25 12:53:36 »

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.
Offline Roquen
« Reply #7 - Posted 2013-01-25 13:34:21 »

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

Junior Member





« Reply #8 - Posted 2013-01-25 14:14:25 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
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
Offline Roquen
« Reply #9 - Posted 2013-01-25 14:16:58 »

It would be really helpful if you would describe what you want.  Like CAN you only move in 8 directions or is it unconstrained?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #10 - Posted 2013-01-25 14:26:15 »

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?
Offline Roquen
« Reply #11 - Posted 2013-01-26 12:12:53 »

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.
Offline Best Username Ever

Junior Member





« Reply #12 - Posted 2013-01-26 16:45:50 »

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.

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (10 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!