Help calculating x,y position of traveling ship

Started by
1 comment, last by mattd 14 years, 5 months ago
I've got a ship in traveling and I want to represent it's location on a 2d map. The ship starts at an X,Y which is known and ends at a known X,Y. I also know the time the fleet started moving and how long it'll take to arrive. Using that info, is there a way to determine what x,y the ship would be at at a given time into the trip? Like say I wanted to know current, I could calculate the time traveled so far as 10 minutes, and use that to derive the current X,Y... Thanks for any help. Chu EDIT: I should point out that accelleration is constant in my game.
Advertisement
Let startpos=(X,Y), endpos=(Xfinal, Yfinal). Then:

dir = endpos-startpos

intermediateposition = startpos + dir*(elapsedtime/wholetimeoftrip)

Hopefully this makes sense, the position you want is just the start position + the direction of movement scaled by the current time of travelling compared to the total time.
I'm assuming you meant constant speed (therefore acceleration too).

In the following equations these variables are used:
In:
"ship starts at an X,Y" -> startX, startY
"ends at a known X,Y" -> endX, endY
"time the fleet started moving" -> startTime
"how long it'll take to arrive" -> tripTime
Out:
"what x,y the ship would be at at a given time" -> curX, curY

Plug the result of the first equation into the other two to get the ship's (X, Y) coordinate.

relTime = (curTime - startTime) / tripTime
curX = startX + (endX - startX) * relTime
curY = startY + (endY - startY) * relTime

This topic is closed to new replies.

Advertisement