2
\$\begingroup\$

I'm working on a small multiplayer game where players can create cities, and these cities will be placed on what's called the "world map." The world map is basically a giant coordinate plane made up of tiles, and the server will load the "world map" in chunks from the database relative to a player's position on the world map.

Now, I want players to be able to send planes from their city to another spot/tile anywhere on the world map. Say for instance player who owns City 1 at position 10,10, wants to create an oil_well at position 70,30. I want a plane to fly from the departure point to arrival point, and client that loads the chunk will see that plane flying across the map.

How can I make the server calculate where the plane is? I have the following information:

departure_time, origin_x, origin_y, arrival_x, arrival_y, speed. speed is the time in seconds that a plane can cross a tile on the world map.

I also have distance, which is simply calculated as (distance formula):

var distance = Math.sqrt(Math.pow( data.arrival_x - data.origin_x , 2) + Math.pow( data.arrival_y - data.origin_y , 2));

Again, how can I calculate the current position of a moving plane on this world map? Because the plane can fly at many different angles, depending on the angle of departure point → arrival point.

\$\endgroup\$
2
\$\begingroup\$

Scalar multiplication.

Assuming the plane moves at constant speed, you can determine how far along it is (0 meaning at departure_point and 1 meaning at arrival_point)

progress = distance / speed * time_elapsed

You can also compute a vector (x,y) representing the translation of the airplane from its departure point to its arrival point, by subtracting one position from the other.

x = arrival_x - departure_x
y = arrival_y - departure_y

translation vector

Then simply multiply that translation vector x,y by the proportion of progress that has been made.

moved_by_x = x * progress
moved_by_y = y * progress

You can then add that vector to the departure point to find the point where the plane should be.

plane_x = departure_x + moved_by_x
plane_y = departure_y + moved_by_y

0.0 0.2 0.5 0.9

departure point red, arrival point blue, plane position orange

\$\endgroup\$
2
  • \$\begingroup\$ Perfect! I was outside trying to wrap my head around this problem, and actually thought about addressing the problem in some fashion similar to this. Thanks! \$\endgroup\$
    – josh
    Aug 7 '15 at 13:51
  • \$\begingroup\$ @josh Happy if it helped. :) \$\endgroup\$
    – Anko
    Aug 7 '15 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.