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

  • Home
  • SEARCH
  • 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 6039247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:20:26+00:00 2026-05-23T06:20:26+00:00

I started working on a concept that requires me to find a way to

  • 0

I started working on a concept that requires me to find a way to move a rectangle toward a given point at a given speed. I’m developing for Android so this is relatively speed critical (it’s going to be calculated every frame for potentially hundreds of objects, as well.)

The solutions I could think of are as follows:

float diff_x = x2 - x1;
float diff_y = y2 - y1;
float length = sqrt((diff_x * diff_x) + (diff_y * diff_y));
float dir_x = diff_x / len;
float dir_y = diff_y / len;

float move_x = dir_x * MOVE_SPEED;
float move_y = dir_y * MOVE_SPEED;

As you can see, this way requires a square root, which I know to be quite costly. I thought of an alternative, which uses trigonometry, but it’s costly as well.

float diff_x = x2 - x1;
float diff_y = y2 - y1;
float angle = atan2(diff_y, diff_x);

float move_x = sin(angle) * MOVE_SPEED;
float move_y = cos(angle) * MOVE_SPEED;

Are there any other ways? If not, which of my solutions would be faster? Thanks for any help.

  • 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-05-23T06:20:27+00:00Added an answer on May 23, 2026 at 6:20 am

    A very common tric you can use is to put everything squarred/power of two/ ^2

    this way instead of using sqrt you just use

    length = (diff_x * diff_x) + (diff_y * diff_y);
    diff_x*diff_x/length
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started working with ASP.NET MVC now that it's in beta. In my
I have recently started working on a very large C++ project that, after completing
I just discovered this component and started working with it. I understand that the
Just started working with android and ran into a small problem. I am have
I have recently started working on a legacy application that has most of its
I started working on my master branch thinking that my task would be easy.
I am a new developer in Android. Currently I have started working on an
I started working on a wordpress theme for a client that has some strange
I started working on mysql c++ connector API. But I can't find any online
We have started working on samsung bada platform for our existing android apps and

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.