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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:27:02+00:00 2026-06-06T02:27:02+00:00

My game units have an angle variable, and a targetAngle variable. These variables range

  • 0

My game units have an angle variable, and a targetAngle variable. These variables range from 1 to 360 degrees.

The angle variable should always move towards the targetAngle variable.

The problem is, it doesn’t happen in the shortest way. When the targetAngle is 350 degrees, and the current angle is only 10 degrees, the variable should only move by 20 degrees instead of incrementing by 340 degrees, as I have now.

How can I make the angle variable move towards the targetAngle variable in the shortest way?

Live demo: http://jsfiddle.net/zNsbc/

var angle = 10;
var targetAngle = 350;

setInterval(function() {

    if ( angle > targetAngle ) {
        angle--;
    }
    else if ( angle < targetAngle ) {
        angle++;
    }    

    $("#angle").text(angle);

}, 10);
  • 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-06T02:27:08+00:00Added an answer on June 6, 2026 at 2:27 am

    You should realize that 360 is the same as 0, so you can compare angle with targetAngle and also with targetAngle+360 and move it to the direction to which it is closest (consider -360 as well for angles close to 0 and targetAngle close to 360)

    So your example:

    angle:            10
    targetAngle:      350 (diff 340)
    targetAngle+360:  710 (diff 700)
    targetAngle-360: - 10 (diff  20)
    

    angle is closest to targetAngle-360, so you would decrement it, instead of incrementing it

    Note: make sure to take absolute value for the difference

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

Sidebar

Related Questions

I have a more or less completed Java game. Enemies spawn from the top
I'm making a 3D game, where the player's back should always be facing the
I am making a turn based hex-grid game. The player selects units and moves
So I have this game database, where I have several users with fields, id,
I am currently designing an RTS Game in XNA. So far I have a
I'm making a game. The game consists of an infinite plane. Units must be
I am currently trying to make a game in Android. I have this piece
I'm creating a game in which I have a somewhat complex method for creating
I have some amount of older game code that I am porting over to
I have received a PSD layout from my client, and I it contains some

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.