1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Rep Power

    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:
     public void moveGhost() {
        	while(!checkMove(maze.nextTile(getCurrentPos(), ghostDirection))){
        		Tiles TargetRedGhost = maze.nextTile(pacman.getCurrentPos(), pacman.getDirection());
            	Tiles ghostRed = getCurrentPos();
    			ghostDirection = calculateDistanceToTarget(ghostRed, TargetRedGhost );
    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. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    How can i have two different distances from the same method?
    Pass the method different arguments.
    how do I associate this shortest distance with a direction
    Return an object of a class that contains the direction and the distance.

IMN logo majestic logo threadwatch logo seochat tools logo