Skip to main content
1 of 3
Ray Dey
  • 6.9k
  • 3
  • 38
  • 45

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.

Ray Dey
  • 6.9k
  • 3
  • 38
  • 45