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'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();
    }
}
share|improve this question
    
Hello Mark, welcome to gamedev. You will find your answer here: gamedev.stackexchange.com/questions/7540/… . –  Notabene Feb 20 '11 at 19:12
add comment

1 Answer

up vote 2 down vote accepted

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();
}
share|improve this answer
    
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!! –  user5583 Feb 20 '11 at 22:28
    
@Mark edited for your updated question :) –  Ray Dey Feb 20 '11 at 22:49
    
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 –  user5583 Feb 20 '11 at 23:10
    
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!! –  user5583 Feb 20 '11 at 23:15
    
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 :) –  Ray Dey Feb 20 '11 at 23:30
add comment

Your Answer

 
discard

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