Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:45:55+00:00 2026-06-05T04:45:55+00:00

Edit: I will summerize my question since it is very long (Thanks Len for

  • 0

Edit: I will summerize my question since it is very long (Thanks Len for pointing it out)

What I’m trying to find out is to get a new position of a player after an X amount of time.
The following variables are known:
– Speed
– Length between the 2 points
– Source position (X, Y)
– Destination position (X, Y)

How can I calculate a position between the source and destion with these variables given?

For example:

source: 0, 0
destination: 10, 0
speed: 1

so after 1 second the players position would be 1, 0

The code below works but it’s quite long so I’m looking for something shorter/more logical

======================================================================

I’m having a hard time figuring out how to calculate a new position of a player ingame.

This code is server sided used to track a player(It’s a emulator so I don’t have access to the clients code).

The collision detection of the server works fine I’m using bresenham’s line algorithm and a raycast to determine at which point a collision happens.

Once I deteremined the collision I calculate the length of the path the player is about to walk and also the total time.

I would like to know the new position of a player each second.

This is the code I’m currently using. It’s in C++ but I am porting the server to C# and I haven’t written the code in C# yet.

        // Difference between the source X - destination X 
        //and source y - destionation Y
        float xDiff, yDiff;

        xDiff = xDes - xSrc;
        yDiff = yDes - ySrc;

        float walkingLength = 0.00F;

        float NewX = xDiff * xDiff;
        float NewY = yDiff * yDiff;

        walkingLength = NewX + NewY;

        walkingLength = sqrt(walkingLength);



        const float PI = 3.14159265F;
        float Angle = 0.00F;

        if(xDes >= xSrc && yDes >= ySrc)
        {
            Angle = atanf((yDiff / xDiff));
            Angle = Angle * 180 / PI;
        }
        else if(xDes < xSrc && yDes >= ySrc)
        {
            Angle = atanf((-xDiff / yDiff));
            Angle = Angle * 180 / PI;
            Angle += 90.00F;
        }
        else if(xDes < xSrc && yDes < ySrc)
        {
            Angle = atanf((yDiff / xDiff));
            Angle = Angle * 180 / PI;
            Angle += 180.00F;
        }
        else if(xDes >= xSrc && yDes < ySrc)
        {
            Angle = atanf((xDiff / -yDiff));
            Angle = Angle * 180 / PI;
            Angle += 270.00F;
        }

        float WalkingTime = (float)walkingLength / (float)speed;



        bool Done = false;
        float i = 0;
        while(i < walkingLength)
        {
            if(Done == true)
            {
                break;
            }
            if(WalkingTime >= 1000)
            {
                Sleep(1000);
                i += speed;
                WalkTime -= 1000;
            }
            else
            {
                Sleep(WalkTime);
                i += speed * WalkTime;
                WalkTime -= 1000;
                Done = true;
            }

            if(Angle >= 0 && Angle < 90)
            {
                float xNew = cosf(Angle * PI / 180) * i;
                float yNew = sinf(Angle * PI / 180) * i;

                float NewCharacterX = xSrc + xNew;
                float NewCharacterY = ySrc + yNew;
            }

I have cut the last part of the loop since it’s just 3 other else if statements with 3 other angle conditions and the only change is the sin and cos.

The given speed parameter is the speed/second.

The code above works but as you can see it’s quite long so I’m looking for a new way to calculate this.

btw, don’t mind the while loop to calculate each new position I’m going to use a timer in C#

Thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:45:57+00:00Added an answer on June 5, 2026 at 4:45 am

    Forget the angles. Trigonometry are both code- and time-intensive. You should use vectors instead.

    Here is an rewrite of your code:

    float xDiff = xDes - xSrc;
    float yDiff = yDes - ySrc;
    
    // Euclidean distance from source to destination
    float walkingLength = sqrt(xDiff * xDiff + yDiff * yDiff);
    
    float WalkingTime = walkingLength / speed;
    
    bool Done = false;
    float i = 0;
    while(i < walkingLength && !Done)
    {
        if(WalkingTime >= 1f)
        {
            Sleep(1000);
            i += speed;
            WalkingTime -= 1f;
        }
        else
        {
            Sleep((int) (WalkingTime * 1000f));
            i += speed * WalkingTime;
            WalkingTime -= 1f;
            Done = true;
        }
    
        float xNew = xDiff / walkingLength * i;
        float yNew = yDiff / walkingLength * i;
    
        float NewCharacterX = xSrc + xNew;
        float NewCharacterY = ySrc + yNew;
    }
    

    xDiff / walkingLength will be the same as cosf(Angle * PI / 180) in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Late edit: This question will hopefully be obsolete when Java 7 comes, because
I am trying to implement a control to edit text that will display the
I need the form that will edit the array of entities and their related
I would like to write a script which will edit multiple XML files, I
My current solution will suck sometimes EDIT For those who don't understand,see this example:
I have a function that will allow me to edit the manager property of
Edit Make sure you don't juxtapose the request and response objects. things will be
I am looking for a tool which will make able to edit parts of
I am writing a PHP application that will have the ability to edit settings
Which ORM will give me compile-tested queries? Is linqtosql compile time tested? Edit: Say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.