Im having trouble making the enemy walking intelligence, what road to take to reach the target.
So even though i have try this for 3 days, i have no clue what to do now.

Say a enemy have a target. and the enemy should aproach it. but i want the enemy to take the right and shorter road, considering collisions. i have write and erased many code,right now whats left is this...

Vector2 destPos         = findPosibleDestination(); //find the nearest posible destination with no collision

boolean hasToMoveRight  = (int)destPos.x > (int)pos.x;
boolean hasToMoveLeft   = (int)destPos.x < (int)pos.x;
boolean hasToMoveUp     = (int)destPos.y > (int)pos.y;
boolean hasToMoveDown   = (int)destPos.y < (int)pos.y;

//and here it goes the craziness i did xD
//where each move returns true if the current movement has no collision or false if has collision and doesnt move

        if(hasToMoveRight){
            if(!moveRight()){
                if(!moveUp()){
                    if(!moveDown()){
                        moveLeft();
                    }
                }
            }
        }
        else if(hasToMoveLeft){
            if(!moveLeft()){
                if(!moveDown()){
                    if(!moveUp()){
                        moveRight();
                    }
                }
            }           
        }

        else if(hasToMoveUp){
            if(!moveUp()){
                if(!moveRight()){
                    if(!moveLeft()){
                        moveDown();
                    }
                }
            }       
        }
        else if(hasToMoveDown){
            if(!moveDown()){
                if(!moveLeft()){
                    if(!moveRight()){
                        moveDown();
                    }
                }
            }       
        }

Now im pretty sure im far from the real thing here, i tried to make a walking track with an Array of Vector2 first but failed.
What should i try next?
Thank you.

Recommended Answers

All 10 Replies

this is the first thread i did not get an answer within 1 hour, im starting to think i might be asking a lot xD sorry guys. I just need an idea on how to do it.

I didn't answer because I didn't understand the question!
Can you explain in english or preudo-code what you are trying to achieve?
Eg, why not
if (hasToMoveRight) moveRight(); etc?

Eg, why not
if (hasToMoveRight) moveRight(); etc?

cuz if has to move right and there is collision in the right well he gotta move somewhere else

here a picture.. problem.png

OK, that makes it clear.
So, something like

if (wants to move right)
   if (can move right) move right       // ideal answer
   else if (can move up) move up        // equal second best
      else if (can move down) move down // equal second best
   else if (can move left) move left    // bad, but no other choice
   else cannot move in any direction!

(times 4 for up/down/left as well).
It's pretty clunky, and there must be ways to optimise that code, but getting it working is more important.
Ideally you could use some kind of look-ahead to see whether up or down would be better when right is unavailable.

James thats something like i have in the code i posted first, the enemy should be able to detect de shorter road so, with this it just start rambling till it gets to the target.
Look at the example here...
problem.png

so he will go up until he can move right, instead of going one step down and reach the target

the best idea i can think of is something like this....
problem02.png

That looks like a search. Using the algorithm I posted earlier you can call that recursively based on the previous position until you reach the target square. For simplicity you could try assuming that a move in the right direction is always best, and a move in the opposite direction is always worst, so you only need to search the up/down choice when moveRight is unavailable. (You can construct bizarre maps where that won't work well, but it should be pretty good for ordinary maps)

that sounds great.. gonna test when i get to my pc, thanks

As an aside, the wording here in the original post caught my eye:

i have write and erased many code,right now whats left is this...

I was wonder, do you use any kind of source control? Even in small projects, revision control is very useful, if only because it gives a record of where you have been and what you have tried (well, within a certain level of granularity, depending on where you check in your changes and how often, but still).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.