I had a question about How to calculate the enemy to arrive in the middle of cell.
How to calculate the enemy to arrive in the middle of cell?
The posters pointed out that the direction of the enemy isn't needed because the velocity already has a direction. I modified the code -
if(position.x < start.position.x) {
velocity.x = ENEMY_VELOCITY;
}
else if(position.x > start.position.x){
velocity.x = -ENEMY_VELOCITY;
}
else {
velocity.x = 0;
}
if(position.y < start.position.y) {
velocity.y = ENEMY_VELOCITY;
}
else if(position.y > start.position.y){
velocity.y = -ENEMY_VELOCITY;
}
else {
velocity.y = 0;
}
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
The enemy's movements were choppy after it go around the tower I added the range of the middle of a cell but it looks like it'll stop in certain places.
public void plotPath(Cell start, float deltaTime) {
if(position.x < start.position.x && Math.abs((start.position.x - position.x)) > 0.01f) {
velocity.x = ENEMY_VELOCITY;
}
else if(position.x > start.position.x && Math.abs((position.x - start.position.x)) > 0.01f){
velocity.x = -ENEMY_VELOCITY;
}
else {
velocity.x = 0;
}
if(position.y < start.position.y && Math.abs((start.position.x - position.x)) > 0.01f) {
velocity.y = ENEMY_VELOCITY;
}
else if(position.y > start.position.y && Math.abs((position.y - start.position.y)) > 0.01f){
velocity.y = -ENEMY_VELOCITY;
}
else {
velocity.y = 0;
}
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
}
I think Math.abs((position.y - start.position.y)) is a wrong way to calculate. I tried to fix 0.1f to different value, but it'll stop somewhere after passing the tower or having choppy movements, it's much less than before.
deltaTime. But this of course will slow your whole game down, unless you run through this whole calculation more often. – Paul Z Jun 5 '11 at 15:01