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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:38:55+00:00 2026-06-06T20:38:55+00:00

I have a 2D animation in Javascript, and I need a sprite to move

  • 0

I have a 2D animation in Javascript, and I need a sprite to move from one point (x1, y1) to another point (x2, y2) on a set speed and direction. For example,

function update(speedX, speedY){
    x1 += speedX;
    y1 += speedY;
    if("(x1, y1) reach (x2, y2)"){
        // do other stuff
    }
}

In most cases, speedX and speedY are not equal, nor do they factor evenly into the distance needed to travel for each axis. I calculate the speedX and speedY values using a tangent function to compute the necessary velocity for a given speed and angle.

My question is, is there an algorithm that can do this? I would prefer something efficient since this has to be done 30 times a second, and it’s float addition! Thanks!

  • 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-06T20:38:56+00:00Added an answer on June 6, 2026 at 8:38 pm

    SOLUTION:

    Okay, with some work I figured it out. First, I defined a function

    function getSign(val) { ... }
    

    that simply returns -1, 0, or 1 depending on whether the number passed in is negative, 0, or positive, respectively. Then, I did the following at the beginning (before any updates happened):

    var xSign = getSign(x1-x2);
    var ySign = getSign(y1-y2);
    

    Finally, to check whether or not the point is currently at or just past the target (x2, y2), just check with this code:

    function update(speedX, speedY){
        x1 += speedX;
        x2 += speedY;
        var curXsign = getSign(x1-x2);
        var curYsign = getSign(y1-y2);
        if( curXsign != xSign || curYsign != ySign ||
            (curXsign == 0 && curYsign == 0)){
                // do other stuff
        }
    }
    

    Basically, if either sign of the relative vector distance between X or Y changes sign, that means that axis went past the point. If both curXsign and curYsign are 0, that means it’s right on the target point. Either of these three cases mean that you are on or just went past the target point. Tested and works.

    Another possible solution would be to test the distance between the start point and the current location (curDist), and compare it to the distance between the start point and the target point (dist). Once curDist >= dist, you know you are at or past the target point.

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

Sidebar

Related Questions

I have a javascript animation code - function animate(position) { .... .... if(position++ <
In Javascript/JQuery I have a need to copy one elements CSS style object to
I have an animation set to slide a DIV down when the DOWN link
I know css animation have a shorthand of -webkit-animation: div { -webkit-animation-name: example; -webkit-animation-duration:
I'm working with animation in JavaScript, and I have a bunch of functions you
I have a problem with my Colorbox not loading any content. javascript: $(document).ready(function(){ $(.exampleterms).colorbox({inline:true,
I have a question regarding animations via javascript in HTML using DOM. In this
I have a animation of moving blocks but I would like to add feature
I have a animation in my navigationbased application. [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.5]; [UIView
I have an animation on a partially hidden div container that will execute when

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.