Tank movement
I am making a game tank
like Atari tank game
and I am facing some problems. I am trying to do it enemy tank move towards the player tank
, but it can't move diagonally
is since the player is not allowed to do that either. However, the way I implemented it, it goes diagonally when the distance from the x and y axis is equal to each other. Is there a way to do this so that it is forced to walk in one direction for a while after changing direction? How to do this, it will be compare its x and y values
with the player's tank (the tank that passes), and four cases for if x-component is bigger than y
and whether it is on right or left
, and if y-component is bigger than x
it is also the above or below
player's tank. Thanks for the help!
public void enemyMove(Tank t) {
if ( Math.abs(getX() - t.getX()) >= Math.abs(getY() - t.getY()) && getX() > t.getX() )
goLeft();
else if ( Math.abs(getX() - t.getX()) > Math.abs(getY() - t.getY()) && getX() < t.getX() )
goRight();
else if ( Math.abs(getX() - t.getX()) <= Math.abs(getY() - t.getY()) && getY() > t.getY() )
goUp();
else if ( Math.abs(getX() - t.getX()) < Math.abs(getY() - t.getY()) && getY() < t.getY() )
goDown();
setX(getX() + dx);
setY(getY() + dy);
}
public void goUp() {
dx = 0;
dy = -1;
}
public void goDown() {
dx = 0;
dy = 1;
}
public void goLeft() {
dx = -1;
dy = 0;
}
public void goRight() {
dx = 1;
dy = 0;
}
source to share
The code below is a little rough, but should do what you want. In your current code, the enemy moves one pixel in the X direction in the first frame, then one pixel in the Y direction in the next frame, which makes the movement horizontal. The code below sets a short term target point for an enemy tank pointing in the X or Y direction, at some distance defined by MOVE_BLOCK. The enemy will move until it passes through that target point, then re-calculate which direction it should move in. Note that enemyMove will be called every time a new frame is created, so probably 60 times per second.
// The tank will move this distance in x or y before changing directions
private final int MOVE_BLOCK = 120;
// The short-term target of the enemy tank
Point target = null;
public void enemyMove(Tank t) {
/* true if enemy is moving left and enemy is already left of target point,
* or moving right and right of target... */
boolean passedTarget = target == null ||
(dx < 0 && getX() < target.getX()) ||
(dx > 0 && getX() > target.getX()) ||
(dy < 0 && getY() < target.getY()) ||
(dy > 0 && getY() > target.getY());
// Calculate a new target point if the old target was passed
if(passedTarget) {
int xDist = Math.abs(getX() - t.getX());
int yDist = Math.abs(getY() - t.getY());
if ( xDist > yDist ) {
// Cover the remaining distance when close to the target
int moveLength = xDist < MOVE_BLOCK ? xDist : MOVE_BLOCK;
if( getX() >= t.getX() )
goLeft();
target = new Point(getX() - moveLength, getY());
else
goRight();
target = new Point(getX() + moveLength, getY());
} else {
int moveLength = yDist < MOVE_BLOCK ? yDist : MOVE_BLOCK;
if ( getY() >= t.getY() ) {
goUp();
target = new Point(getX(), getY() - moveLength);
} else {
goDown();
target = new Point(getX(), getY() + moveLength);
}
}
}
setX(getX() + dx);
setY(getY() + dy);
}
source to share