Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 6th, 2012, 11:04 AM
 lisa92
Contributing User

Join Date: Oct 2011
Posts: 43
Time spent in forums: 10 h 33 m 29 sec
Reputation Power: 3
Ghost algorithm in pacman, find distance btw target

Im implementing ghost algorithm for chasing pacman. I'm using the euclid forumla to find the distance btw the two objects, however I'm supposed to compare two different distances. How can i have two different distances from the same method? And next question is how do I associate this shortes distance with a direction (up,down,left,right)?

This is a rough code snippets:
Code:
```
public void moveGhost() {

while(!checkMove(maze.nextTile(getCurrentPos(), ghostDirection))){
maze.nextTile(getCurrentPos(),ghostDirection);

Tiles TargetRedGhost = maze.nextTile(pacman.getCurrentPos(), pacman.getDirection());
Tiles ghostRed = getCurrentPos();

ghostDirection = calculateDistanceToTarget(ghostRed, TargetRedGhost );
move();
}

public int calculateDistanceToTarget(Tiles ghostRed, Tiles pacman ) {
//Tiles coords1 = character.getCurrentPos();
double x1 = ghostRed.getX();
double y1 = ghostRed.getY();
//Tiles coords2 = character.getCurrentPos();
double x2 = pacman.getX();
double y2 = pacman.getY();

double distance;
distance = Math.sqrt(( Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))); //or this d=<int>(sqrt(xd*xd+yd*yd));?

//need associate shortest path with direction

return ghostDirection;

}

public void move() {
x += xinc;
y += yinc;
}```

#2
November 7th, 2012, 02:22 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,584
Time spent in forums: 2 Weeks 3 Days 3 h 8 m 43 sec
Reputation Power: 347
Quote:
 How can i have two different distances from the same method?

Pass the method different arguments.
Quote:
 how do I associate this shortest distance with a direction

Return an object of a class that contains the direction and the distance.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Ghost algorithm in pacman, find distance btw target