Sorry, but geometry never was my favorite subject in school, so...
Currently I coding 2D MMORPG server emulator and want to improve my current coords detection algorithm.
Client send to server packets with start point coords and finish point coords.
Server receive it and assign movement start variable to DateTime.Now.
Server know what distance character can travel for 1 second (that's speed).
How to calculate character position after some seconds since he started movement?
Since I am bad at geometry, I coded this... junk... based on percents of traveled distance. It's working through, but too much calculations.
public ushort X;
public ushort Y;
public ushort TargetX;
public ushort TargetY;
public DateTime MovementStartTime;
public ushort CalculatedX
{
get
{
if (this.X == this.TargetX && this.Y == this.TargetY)
return this.X;
float total_dst = CF.GetEuclideanDistanceBetween(this.X, this.Y, this.TargetX, this.TargetY);
float seconds_since_movement_start = (float)(DateTime.Now - this.MovementStartTime).TotalSeconds;
float passed_dst = Limits.CharacterSpeed * seconds_since_movement_start;
if (passed_dst > total_dst)
return this.TargetX;
float passed_dst_in_normalized_percents = (passed_dst * 100 / total_dst) / 100;
return (ushort)(this.X - ((this.X - this.TargetX) * passed_dst_in_normalized_percents));
}
}
public ushort CalculatedY
{
get
{
if (this.X == this.TargetX && this.Y == this.TargetY)
return this.Y;
float total_dst = CF.GetEuclideanDistanceBetween(this.X, this.Y, this.TargetX, this.TargetY);
float seconds_since_movement_start = (float)(DateTime.Now - this.MovementStartTime).TotalSeconds;
float passed_dst = Limits.CharacterSpeed * seconds_since_movement_start;
if (passed_dst > total_dst)
return this.TargetY;
float passed_dst_in_normalized_percents = (passed_dst * 100 / total_dst) / 100;
return (ushort)(this.Y - ((this.Y - this.TargetY) * passed_dst_in_normalized_percents));
}
}
The idea is to determine how much percents of the line character already passed based on it's speed and then combine it with the flat distance between start X and end X.
