This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_wow
K_wow wrote ...

2013/7/29

Trouble With Finding Closest Player

K_wow K_wow

2013/7/29

#
As part of my 2 player top-down survival combat game, I want the zombies to detect the nearest player and head for them. Here is the code I use:
Board board = (Board)getWorld();

        int playerBluePos = board.playerBlueX + board.playerBlueY;
        int playerRedPos = board.playerRedX + board.playerRedY;
        int zombiePos = getX() + getY();
        if (playerBluePos - zombiePos < 0 && playerRedPos - zombiePos > 0 || playerRedPos - zombiePos < 0 && playerBluePos - zombiePos > 0)
        {
            playerRedPos = -playerRedPos;
        }
        if (playerRedPos < 0)
        {
            if (playerRedPos > playerBluePos)
            {
                turnTowards(board.playerRedX, board.playerRedY);
                move(speed);
                walkProgress += speed;
            }
            else
            {
                turnTowards(board.playerBlueX, board.playerBlueY);
                move(speed);
                walkProgress += speed;
            }
        }
        if (playerRedPos > 0)
        {
            if (playerRedPos < playerBluePos)
            {
                turnTowards(board.playerRedX, board.playerRedY);
                move(speed);
                walkProgress += speed;
            }
            else
            {
                turnTowards(board.playerBlueX, board.playerBlueY);
                move(speed);
                walkProgress += speed;
            }
        }
I know it's a bit messy, but I thought it would still do the job. However, it doesn't work properly. sometimes, the zombie will go for one player even though they are not the closest, other times it will walk to one player, then walk to the other, then back to the first etc. Does anyone know how to fix this?
youngpro youngpro

2013/7/29

#
tell me how to make a game
kxsfnwsladlka
Zamoht Zamoht

2013/7/29

#
Rewrite the whole thing and use pythagoras. int playerBlueDistance = Math.sqrt((getX() - board.playerBlueX)*(getX() - board.playerBlueX)+(getY() - board.playerBlueY)*(getY() - board.playerBlueY)); int playerRedDistance = Math.sqrt((getX() - board.playerRedX)*(getX() - board.playerRedX)+(getY() - board.playerRedY)*(getY() - board.playerRedY)); if (playerRedDistance < playerBlueDistance) { //Red player is the closest. } else { //Blue player is the closest. }
K_wow K_wow

2013/7/29

#
Zamoht wrote...
Rewrite the whole thing and use pythagoras. int playerBlueDistance = Math.sqrt((getX() - board.playerBlueX)*(getX() - board.playerBlueX)+(getY() - board.playerBlueY)*(getY() - board.playerBlueY)); int playerRedDistance = Math.sqrt((getX() - board.playerRedX)*(getX() - board.playerRedX)+(getY() - board.playerRedY)*(getY() - board.playerRedY)); if (playerRedDistance < playerBlueDistance) { //Red player is the closest. } else { //Blue player is the closest. }
Thanks so much, it works like a charm!
youngpro wrote...
tell me how to make a game
kxsfnwsladlka
What type of game do you want to make?
K_wow K_wow

2013/7/29

#
Ok, now I've run into another problem. I'm trying to get the zombie to be knocked back if it is close enough to the player, using this code:
public void hurtPlayers()
    {
        Board board = (Board)getWorld();
        
        if (withinRange(board.playerBlueX, board.playerBlueY, 20, 20))
        {
            turn(180);
            move(20);
            turn(180);
        }
        if (withinRange(board.playerRedX, board.playerRedY, 20, 20))
        {
            turn(180);
            move(20);
            turn(180);
        }
    }
withinRange looks like this:
public boolean withinRange(int posX, int posY, int radiusX, int radiusY)
    {
        if (getX() > posX - radiusX && getX() < posX + radiusX && getY() > posY - radiusY && getX() < posY + radiusY)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
But the zombie acts as though it doesn't check if the player is within range at all, resulting in the zombie getting stuck on the player. Is there a better way to do this?
K_wow K_wow

2013/7/29

#
Never mind, found the problem, a simple typo.
You need to login to post a reply.