Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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  
  a* algorithm path finding  (Read 2571 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie

« Posted 2006-09-10 18:35:32 »

Hi, I've implemented a class of Enemies (enemygood) with random movement.
Now I want to implement a new class of Enemies (enemyhunter) that chase the player with an A* algorithm:

I've writed the Node class, it represent a single node from the map, on this class I've:

- The x coordinate of the node
- The y coordinate of the node
- The cost of the node
- The precedent node

Now, I want to know if it's correct to write the rest of A* algorithm  in a new class (for example AI class)
or I can write it into the enemyhunter class??

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline kappa
« League of Dukes »

JGO Kernel

Medals: 123
Projects: 15


« Reply #1 - Posted 2006-09-10 22:15:53 »

i suggest having a look at the article by david brackeen at
its very good and best of all its written java.
Offline JAW

Senior Devvie

Medals: 2

« Reply #2 - Posted 2006-09-13 17:58:54 »

You should defenitely write an A* Pathfinder in a seperate class to use it for different purposes and to reuse it. You might want to find a path for something else later, too.

Generally, Pathfinding is not a part of an actor or any specific actor of the game. Its more a service class with knowledge of the map. You can make your A* class very abstract and flexible and it can perform a lot of tasks.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

« Reply #3 - Posted 2006-09-14 03:03:11 »

As said, implement the algorithm in its own class.  But also set up an interface that the algorithm will work with, such as a Node interface.  Then you can make it easy to use anywhere just by have other classes implement the Node interface.

Offline ravenger

Senior Newbie

« Reply #4 - Posted 2006-09-18 17:36:52 »

What the guys mean is, implement the strategy pattern and make the A* Algorithm a concrete strategy. This way you can easily implement new Pathfinding algorithms without having to change your current code. ( You'll have to define a method in your interface which you then override in the concrete subclasses (one of which is AStarPathfinder, other could be something like a hillclimbing algorithm (, and so on).

For the pathNode in A* you will have to divide the cost of the node into heuristic cost and movement cost.
The formula for calculating the total cost is this; F = G + H

G = the movement cost to move from the starting point A to a given node on the grid, following the path generated to get there.
H = the estimated movement cost or heuristic cost to move from that given node on the grid to the final destination, point B.
F = total cost = movementCost + heuristicCost.

As i said, total cost is something you can calculate so you dont include that into the model itself. You can also think of storing the x and y position in a Dimension, but that is personal preference Smiley
Pages: [1]
  ignore  |  Print  

hadezbladez (295 views)
2018-11-16 13:46:03

hadezbladez (164 views)
2018-11-16 13:41:33

hadezbladez (295 views)
2018-11-16 13:35:35

hadezbladez (75 views)
2018-11-16 13:32:03

EgonOlsen (2156 views)
2018-06-10 19:43:48

EgonOlsen (2183 views)
2018-06-10 19:43:44

EgonOlsen (1370 views)
2018-06-10 19:43:20

DesertCoockie (1988 views)
2018-05-13 18:23:11

nelsongames (1631 views)
2018-04-24 18:15:36

nelsongames (2280 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!