Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Heuristic Path Finding  (Read 1858 times) 0 Members and 1 Guest are viewing this topic.
westloar

Junior Devvie

 « 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?

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.

westloar

Junior Devvie

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

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

Junior Devvie

 « 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

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.

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

Junior Devvie

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

Junior Devvie

 « 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 directioncurrentPos.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 listfor(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 = distancemove.setLocation(moveList[i]); //set the final move position to current point}//endif}//end for`
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?
westloar

Junior Devvie

 « 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?
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.

Junior Devvie

 « 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.

 bilznatch (27 views) 2015-08-04 11:03:17 SHC (44 views) 2015-08-01 03:58:20 Jesse (25 views) 2015-07-29 04:35:27 Riven (48 views) 2015-07-27 16:38:00 Riven (26 views) 2015-07-27 15:35:20 Riven (28 views) 2015-07-27 12:26:13 Riven (19 views) 2015-07-27 12:23:39 BurntPizza (42 views) 2015-07-25 00:14:37 BurntPizza (56 views) 2015-07-24 22:06:39 BurntPizza (35 views) 2015-07-24 06:06:53
 theagentd 50x wessles 48x basil_ 33x KaiHH 26x Riven 21x mooman219 17x bornander 16x orangepascal 16x ags1 16x KudoDEV 13x CelestialCreator 11x princec 11x klaus 11x pquiring 11x israelg99 9x philfrei 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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