Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
What exactly do you mean by "choppy"? Is it swinging wider than you wanted it to? or, too many corners? If you want the course to be smoother, reduce 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
    
the enemy isn't reaching the center of a cell, so it'll zig zag between the middle of a cell. I tried to expand the middle of a cell but it seems it's not working. –  Tashu Jun 5 '11 at 15:38
    
I'm thinking to go back where I posted first to get the distance == 0 by changing velocity somehow. –  Tashu Jun 5 '11 at 15:39
add comment

1 Answer

up vote 2 down vote accepted

Conceptually, the problem you're running in to is that you want to stop at an exact point and don't know how to handle the problem with time sliced velocity calculation.

One way to solve this is to consider not moving the enemy at only your ENEMY_VELOCITY number. When the distance between your current position and your desired position is less than your per-frame velocity, set your velocity for that frame to the distance. That'll put you pretty much where you want to be (of course with float inaccuracies) and you can stop at that frame.

share|improve this answer
    
Ok, I'll try it out. Thanks. –  Tashu Jun 6 '11 at 0:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.