0
\$\begingroup\$

I was able to implement the A* pathfinding algorithm to calculate a path from the enemy to player. enter image description here

After trying to do movement well, I realized I ran into all sorts of problems. I was able to add clearance to the pathfinding, so I'm now going to have sprites of variable size, which leads to even more issues.

I am currently doing movement like - path[0] is first Tile in path and it is removed when you get to it:

            if (path.Count > 0) {
                if (path[0].isTouching(this))
                    path.RemoveAt(0);                           
                }
            }

                            if (path [0].getCenterX() > this.getCenterX()) {
                                speedX = speed; 
                                speedY = 0;
                                System.Console.WriteLine("moving right");
                            }
                            else if (path [0].getCenterX() < this.getCenterX()) {   
                                speedX = -speed; 
                                speedY = 0; 
                                System.Console.WriteLine("moving left");
                            }

                            if (path [0].getCenterY() > this.getCenterY()) { 
                                speedX = 0; 
                                speedY = speed;
                                System.Console.WriteLine("moving down");

                            }
                            else if (path [0].getCenterY() < this.getCenterY()) {       
                                speedX = 0; 
                                speedY = -speed; 
                                System.Console.WriteLine("moving up");
                            }

                            this.move(speedX, speedY);

This doesn't work very well for a variety of reasons.

I am certain the pathfinding I implemented calculates the proper path of Tiles. Now I think I have to move from a pixel to pixel movement -> tile to tile in order to have this working better. This would probably help, right? How is movement usually done to work with A*? If the tile-based movement would aid, any tips on implementing this?

\$\endgroup\$
  • \$\begingroup\$ What about moving to the center of the tile (or the bottom) instead of the top-left corner? \$\endgroup\$ – ashes999 Apr 3 '14 at 10:55
  • \$\begingroup\$ the .getCenter() gets the center of the tile :P \$\endgroup\$ – atkayla Apr 3 '14 at 18:15
  • \$\begingroup\$ I didn't read that part. Okay, nice. \$\endgroup\$ – ashes999 Apr 3 '14 at 19:37
1
\$\begingroup\$

If you are talking about move horizontally and vertically at a same time, the problem is not here. You are doing fine. Maybe problem belong to input module.

About A*, it's a path finding algorithm, it return series of point present the path you ask for. Movement is another problem, don't stick it with A*. The easiest way to implement moving system are storing local goal, and once you reach local goal, go to next goal in path.

int position_x, position_y;
float speed_x, speed_y;
int local_goal_x, local_goal_y;
bool stop;
void next_goal(){
    path.removeAt(0);
    if(path.empty()){ stop = true; return;}
    local_goal_x = path[0].x;
    local_goal_y = path[0].y;
    speed_x = (local_goal_x - position_x)/10.0;
    speed_y = (local_goal_y - position_y)/10.0;
}
void update_moving()
{
    if(stop) return;
    position_x += speed_x;
    position_y += speed_y;
    if(position_x == local_goal_x && position_y == local_goal_y) next_goal();
}
\$\endgroup\$
0
\$\begingroup\$

I would do something like this:

int pixelPerFPS; // pixel per FPS
void move(int x, int y);
void update(int time)
{
    character.currentX += (character.goalX - character.currentX) / abs(character.goalX - character.currentX) * pixelPerFPS;
    character.currentY += (character.goalY - character.currentY) / abs(character.goalY - character.currentY) * pixelPerFPS;
    // do something
}
int gameStart()
{
    time2 = currentTime();
    while(running)
    {
        deltaTime = currentTime() - time2;
        time2 = currentTime();
        update(deltaTime);
        // do something
        // do other thing
    }
    return 0;
}

This is very simple function like an example

If you want to make it more complex and flexible, you have to take care about frame per seconds (FPS)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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