1
\$\begingroup\$

I'm trying to animate a player object in my 2D grid when the user clicks somewhere in the screen.

I got the following 4 variables:

  1. oX (Current player position X)
  2. oY (Current player position Y)
  3. dX (Destination X)
  4. dY (Destination Y)

How can I make sure the player moves in a straight line to the new XY coordinates. The way I'm doing it now is really awfull and causes the player to first move along x axis, and finally in y axis.

Can someone give me some guidance with the involved math cause I'm really not sure on how to accomplish this.

Thank you for your time. Kind regards, Mark

Update: It's working now but whats the right way to check if the current positions are equal to the target position?

    private static void MovePlayer(double x2, double y2, int duration)
{
    double hX = x2 - m_PlayerPosition.X;
    double hY = y2 - m_PlayerPosition.Y;

    double Length = Math.Sqrt(Math.Pow(hX, 2) + Math.Pow(hY, 2));
    hX = hX / Length;
    hY = hY / Length;

    while (m_PlayerPosition.X != Convert.ToInt32(x2) || m_PlayerPosition.Y != Convert.ToInt32(y2))
    {
        m_PlayerPosition.X += Convert.ToInt32(hX * 1);
        m_PlayerPosition.Y += Convert.ToInt32(hY * 1);

        UpdatePlayerLocation();
    }
}
\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

You will want to look into vector maths for this. The basic way you would handle this is as follows:

You'd want two variables as your "heading", this will tell you the direction you want to be heading in.

Let's call them hX and hY.

hX = dX - oX
hY = dY - oY

You should probably normalize these values (meaning, the length of the vector is equal to 1). You do that by performing the following:

Length = sqrt( hX^2 + hY^2 )
hX = hX / Length
hY = hY / Length

Now you can use a speed multiplier to determine how quickly you want your agent to move to the destination position.

currentX += hX * SPEED
currentY += hY * SPEED

That should be everything really. To make this a LOT easier, it might be worth looking into incorporating a maths library with SDL so that you can use vectors and vector operations.

EDIT FOR UPDATED QUESTION:
The way you have your while loop at the moment will lock up your entire game until the player has arrived at the position.

A better way is to have a standard game loop with an Update() function like so:

public class Game
{
    // Other implementations left out
    public void Update()
    {
        UpdatePlayer()
    }
};

In your UpdatePlayer() function you can call MovePlayer() and within MovePlayer() since the Length variable is always updated every time you call the function, all you have to do is update MovePlayer() to do something like this:

private static void MovePlayer(double x2, double y2, int duration)
{
    double hX = x2 - m_PlayerPosition.X;
    double hY = y2 - m_PlayerPosition.Y;

    double Length = Math.Sqrt(Math.Pow(hX, 2) + Math.Pow(hY, 2));

    if( Length < 0.1f )    // Less-than rather than Length == 0.0 to handle floating point errors
        return;

    hX = hX / Length;
    hY = hY / Length;

    m_PlayerPosition.X += Convert.ToInt32(hX * 1);
    m_PlayerPosition.Y += Convert.ToInt32(hY * 1);

    UpdatePlayerLocation();
}
\$\endgroup\$
5
  • \$\begingroup\$ Thank you Ray for your explanation, i've tried to make a method with your input and it works but I'm not sure on how to do the loop. Please see my edited function in my question. Thanks!! \$\endgroup\$
    – user5583
    Commented Feb 20, 2011 at 22:28
  • \$\begingroup\$ @Mark edited for your updated question :) \$\endgroup\$
    – Ray Dey
    Commented Feb 20, 2011 at 22:49
  • \$\begingroup\$ Thank you so much Ray this is what I was looking for! I will probably have to buy a gamedesign math book to fully understand everything and come up with own implementations. The player moves to the excact location now but it still doesn't travel in a straight line to the target. Let's say the player is in bottom-left corner and target is top right, then it will travel from left to right first and just before the target x it will travel a diagonal line to the top screen. But maybe its good for me to figure something out myself too instead of spoonfeeding me haha, sometimes it DOES travel straig \$\endgroup\$
    – user5583
    Commented Feb 20, 2011 at 23:10
  • \$\begingroup\$ Good idea of the game update method method cause I saw my FPS dropped to 20 with the way I did it before. So much to learn for me but I really like it!! \$\endgroup\$
    – user5583
    Commented Feb 20, 2011 at 23:15
  • \$\begingroup\$ No problem :) it might be because you have a grid based world, but I'll leave that to you. I would recommend getting Game Coding Complete by Mike McShaffry, it contains a lot of help regarding the game loop and even basic vector maths operations :) \$\endgroup\$
    – Ray Dey
    Commented Feb 20, 2011 at 23:30

You must log in to answer this question.