3 Replies - 484 Views - Last Post: 07 October 2012 - 07:36 AM Rate Topic: -----

#1 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Zombie AI - not animating independantly

Posted 07 October 2012 - 01:02 AM

Hey all, I am still working on a little game of mine. I am currently working on a VERY simplistic zombie AI that will literally just follow the player however the problem I have currently is with my zombie AI code:

	public void zombieAI() {
		for (int x = 0; x < MAX_ZOMBIES; x++) {
			int randomAngle = (int) (Math.random() * 45); // random angle so
															// that zombies do
															// not face the same
															// way all the time
			int XorY = (int) (Math.random() * 2); // zombies should move X or Y
													// seperately to encourage
													// some random movement
			zombie[x].setFaceAngle(player.getFaceAngle()); // zombie
																			// must
																			// move
																			// towards
																			// player
			if (XorY == 0) {
				if (zombie[x].getX() < player.getX()) { // zombie's X is to the
														// left of player's so
														// move right
					// set velocity
					double velx = zombie[x].calcAngleMoveX(zombie[x]
							.getFaceAngle()) * 1;
					zombie[x].setVelX(velx);

					zombie[x].setX(zombie[x].getX() - zombie[x].getVelX());

				}

				if (zombie[x].getX() > player.getX()) { // zombie's X is to the
														// right of player's so
														// move left
					// set velocity

					double velx = zombie[x].calcAngleMoveX(zombie[x]
							.getFaceAngle()) * 1;
					zombie[x].setVelX(velx);
					zombie[x].setX(zombie[x].getX() + zombie[x].getVelX());

				}

			} else { //otherwise move Y
				if (zombie[x].getY() < player.getY()) { 
					// set velocity

					double vely = zombie[x].calcAngleMoveY(zombie[x]
							.getFaceAngle()) * 1;
					zombie[x].setVelY(vely);

					zombie[x].setY(zombie[x].getY() + zombie[x].getVelY());
				}

				if (zombie[x].getY() > player.getY()) { 

					double vely = zombie[x].calcAngleMoveY(zombie[x]
							.getFaceAngle()) * 1;
					zombie[x].setVelY(vely);
					zombie[x].setY(zombie[x].getY() - zombie[x].getVelY());
				}
			}
		}
	}



However my problem is that the zombie's do the same decision together, I thought that randomizing X or Y would make the zombies move differently but it seems this is not the case... Any help would be greatly appreciated!

(Here is a screenshot of the problem. Note how all zombies are moving in the same direction towards the player, the zombies that are going out of the screen, re-enter at the opposite end.)

Posted Image

This post has been edited by alpha_x: 07 October 2012 - 01:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Zombie AI - not animating independantly

#2 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Re: Zombie AI - not animating independantly

Posted 07 October 2012 - 01:35 AM

Just to add my ZombieAI() is called in my GameUpdate method which is run in the GameLoop thread:

	public void run() {
		Thread t = Thread.currentThread();
		while (t == gameloop) {
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// poll keyboard - check if keys were pressed
			keyboard.poll();

			gameUpdate();
		}
	}



public void gameUpdate() {

		// process keys that are held down
		handleInput();
		// draw background

		// g2d.setColor(Color.WHITE);
		g2d.fill(new Rectangle(0, 0, ScreenWidth - 1, ScreenHeight - 1));

		drawBackground();
		// player.transform();
		checkPlayerBoundaries();
		player.setBoundsEnabled(true);
		drawPlayer(player.getBoundsEnabled());

		zombieAI();
		checkZombieBoundaries();
		for (int k = 0; k < MAX_ZOMBIES; k++) {
			zombie[k].drawBounds(Color.BLUE);
			zombie[k].draw();
		}

		drawBullets();
		checkBulletStatus(); // JUST FOR DEBUG PURPOSES, REMOVE UPON COMPLETION

		drawHealthBar();
		g2d.setColor(Color.BLACK);
		g2d.drawString("Position: " + player.x + "," + player.y, 10, 40);
		g2d.drawString("Velocity: " + player.x + "," + player.y, 10, 60);
		g2d.drawString("Animation: " + player.getCurrentFrame(), 10, 80);

		g2d.drawString("Face Angle: " + player.getFaceAngle(), 10, 100);
		g2d.drawString("Bullets Left: " + (MAX_BULLETS - currentShot), 10, 120);

		g2d.drawString("Bullets Alive: " + getBulletsAlive(), 10, 140);
		repaint();
	}



Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Zombie AI - not animating independantly

Posted 07 October 2012 - 04:27 AM

You seem surprised (disappointed?) that your Zombie AI is working exactly as you've stated it should: "a VERY simplistic zombie AI that will literally just follow the player." I haven't studied your x/y randomization to see how that might change the Zombie's movements, because ultimately the AI is programmed to follow the player.

Even with that simple goal, Zombie characteristics that you could add and vary to show a noticeable difference are speed, a lead angle, and maybe an anticipation factor. The speed and lead angle would be the easiest. The anticipation factor may be a response to the gamespace, so I won't discuss that, but it's something to think about.

Lead angle is from basic intercept theory. Pure pursuit simply points at the target, resulting in the longest possible path to an intercept with a maneuvering target, while a lead angle calculates where an intercept will occur based on the target and interceptor's speeds and the target's current heading or aspect. You could add a lead angle calculation and an aggressiveness or responsiveness factor - the gain of the Zombie's response.

With those changes plus variations in speed, some Zombies will simply point at the player, others will aggressively vary their direction with every change in the player's direction, and some will respond more conservatively. A slow speed but aggressive Zombie will almost appear to stand still if the player moves a lot, while a high speed, conservative Zombie should merge with the player fairly quickly.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Zombie AI - not animating independantly

Posted 07 October 2012 - 07:36 AM

Of course they are. Look at your code. You're picking a random face angle and a XorY and it applying the same value to every zombie. A simple fix would be to iterate over your array of zombies in your gameUpdate(), instead of zombieAi(), calling zombieAi() on each object separately, thus allowing getFaceAngle and XxorY to be different for each zombie object.


for (Zombie z : zombieArray) {
    z.zombieAi();
}



EDIT: At least that's what I think is happening. It's early, so I could have completely missed something :)

EDIT2: Nah, I think I am way off base. Forget my suggestion. I'll leave this reply, so that other can mock me for it. :)

This post has been edited by farrell2k: 07 October 2012 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1