Moving object from point to point...

BlitzPlus Forums/BlitzPlus Programming/Moving object from point to point...

If you dont care about the angle but just want to know how much the object has to move in the x and y directions each frame to move at a speed of 1 pixel then you could just do the following ( using the example you gave for the values:

1. Find the total distance in the x and y directions.

xmove = 400 - 200 = 200
ymove = 400 - 100 = 300

2. If you treat this as 2 lines of a right angled triangle then you can find the straight line distance between the start and end position.

distance = SQRT(200 * 200 + 300 * 300 ) = 360.5551

So in a straight line the distance is 360.5551 pixels.

3. If you divide the xmove and ymove by the total distance you will get the distance you have to move in the x and y directions to move 1 pixel distance.

xmove = 200 / 360.5551 = 0.5547
ymove = 300 / 360.5551 = 0.8320

There you have it. If every frame you add xmove and ymove to your sprite x and y position it will move towards your target at a speed of 1 pixel.

I hope that makes sense and is actually correct. Im not at my machine so I cant prove it but Im sure I will be corrected if this is wrong.