My first animation - Using SDL.NET C#

Posted by Mark on Game Development See other posts from Game Development or by Mark
Published on 2011-02-20T18:32:29Z Indexed on 2011/02/20 23:33 UTC
Read the original article Hit count: 358

Filed under:
|
|

Hi all! 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();
    }
}

© Game Development or respective owner

Related posts about c#

Related posts about animation