Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (542) Games in Android Showcase (133) games submitted by our members Games in WIP (604) 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 1482 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?
 Games published by our own members! Check 'em out!
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?
 Games published by our own members! Check 'em out!
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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 CopyableCougar4 (14 views) 2014-12-28 02:10:29 BurntPizza (17 views) 2014-12-27 22:38:51 Mr.CodeIt (13 views) 2014-12-27 04:03:04 TheDudeFromCI (17 views) 2014-12-27 02:14:49 Mr.CodeIt (25 views) 2014-12-23 03:34:11 rwatson462 (56 views) 2014-12-15 09:26:44 Mr.CodeIt (46 views) 2014-12-14 19:50:38 BurntPizza (92 views) 2014-12-09 22:41:13 BurntPizza (113 views) 2014-12-08 04:46:31 JscottyBieshaar (86 views) 2014-12-05 12:39:02
 Rayvolution 50x BurntPizza 30x basil_ 29x HeroesGraveDev 25x LiquidNitrogen 24x appel 18x kpars 18x princec 16x Riven 16x KevinWorkman 15x NegativeZero 13x pitbuller 13x gouessej 13x KudoDEV 12x SHC 11x Ecumene 11x
 How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50
 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