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 8988251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:58:36+00:00 2026-06-15T21:58:36+00:00

I know this has been asked a few times before, but there has been

  • 0

I know this has been asked a few times before, but there has been no answer that actually works that I can find. There is a similar one, but the speed varies depending on the distance travelled.

So my problem is that I am trying to get an object (a player in this case) to move a long a straight line from point A to B at a constant speed. This is done by clicking on the player and dragging to where I want him to walk to, so it can be in any direction and over any distance.

I have some code that very nearly works, but the player always ends up slightly off course, more so the longer the distance he travels is. Here is that code:

window.addEventListener('mouseup', function(e) {
    selectedPlayer.moveX = e.pageX;
    selectedPlayer.moveY = e.pageY;
    movePlayer(selectedPlayer);
});

function movePlayer(player) {

    var xDistance = player.moveX - player.x;
    var yDistance = player.moveY - player.y;
    var travelDistance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));
    var timeToTravel = travelDistance; //This may seem pointless, but I will add a speed variable later
    var playerAngle = Math.atan2(yDistance, xDistance) * (180 / Math.PI);
    var xRatio = Math.atan2(xDistance, travelDistance);
    var yRatio = Math.atan2(yDistance, travelDistance);

    //This function is called in another part of code that repeats it 60 times a second
    walkPlayer = function() {

        setTimeout(function(){
            player.x = player.moveX;
            player.y = player.moveY;
            selectedPlayer = undefined;
            walkPlayer = undefined;
        }, timeToTravel * 20)

        player.angle = playerAngle;
        player.x += xRatio;
        player.y += yRatio;
    };
}

I hope this makes sense, I’ve had to include only the part of the code that is relevant. I think my issue probably lies with the xRatio and yRatio parts, but I can’t figure it out; I’m completely stumped.

EDIT: I’d like to add that playerAngle makes the player face in the direction of the drag, and that part works fine.

  • 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-15T21:58:38+00:00Added an answer on June 15, 2026 at 9:58 pm

    Live Demo

    Below are the basics required to get what you need working,

    var tx = targetX - x,
        ty = targetY - y,
        dist = Math.sqrt(tx*tx+ty*ty),
        rad = Math.atan2(ty,tx),
        angle = rad/Math.PI * 180;;
    
        velX = (tx/dist)*thrust;
        velY = (ty/dist)*thrust;
    
    player.x += velX
    player.y += velY
    

    This is a demo I did a while back which sounds like what you are looking for, I added the ability to click in order to change the target based off of your issue.

    window.addEventListener('mouseup', function(e) {
        targetX  = e.pageX;
        targetY = e.pageY;
    });
    
    var ctx = document.getElementById("canvas").getContext("2d"),
        x = 300,
        y = 0,
        targetX = Math.random()*300,
        targetY = Math.random()*300,
        velX = 0,
        velY = 0,
        thrust = 5;
    
    
    function draw(){   
        var tx = targetX - x,
            ty = targetY - y,
            dist = Math.sqrt(tx*tx+ty*ty),
            rad = Math.atan2(ty,tx),
            angle = rad/Math.PI * 180;;
    
        velX = (tx/dist)*thrust;
        velY = (ty/dist)*thrust;
    
        // stop the box if its too close so it doesn't just rotate and bounce
        if(dist > 1){
          x += velX;
          y += velY;
        }
    
        ctx.fillStyle = "#fff";
        ctx.clearRect(0,0,400,400);
        ctx.beginPath();
        ctx.rect(x, y, 10, 10);
        ctx.closePath();
        ctx.fill();
    
        ctx.fillStyle = "#ff0";
        ctx.beginPath();
        ctx.rect(targetX, targetY, 10, 10);
        ctx.closePath();
        ctx.fill();
    
        setTimeout(function(){draw()}, 30);   
    }
    
    draw();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been asked many times before, but i still can't seem
I know this question has been asked here a few times before. But i
I know this has been asked a few times, but I can't find a
I know that this has been asked a million times before, but nothing that
I know this has been asked a few times before, but the existing solutions
I know that this has been asked many times before, but I believe my
I know this has been asked at least a thousand times but I can't
I know this has been asked thousands of times but I just can't find
Now I know this has been asked a few times, but i'm still not
I know this has been asked a few times here. But none of the

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.