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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:18+00:00 2026-05-26T11:16:18+00:00

I’m currently trying to rotate a sprite depending on how many degrees(or rads, I

  • 0

I’m currently trying to rotate a sprite depending on how many degrees(or rads, I prefer degrees) it differs from facing straight towards a target, the problem is when the target reaches a certain position the sprites decides to do rotate a full 360 to other way instead of doing the 10 extra. This picture probably explains the problem better:

Blue square = target, Red square = the object, green line = rotation it wants, black line = current rotation, brown arrow = how it rotates to achieve this, red arrow = how I want it to rotate.

Blue square = target

Red square = the object

Green line = rotation it wants

Black line = current rotation

Brown arrow = how it rotates to achieve this

Red arrow = how I want it to rotate.

Note that Case 1 always work, depending what way it rotates, but Case 2 it always does that rotation, no matter if it is to the “right” or “left” of the current rotation.

This is the code I’m using to rotate the object.

    Vector2 distance = new Vector2(target.worldPos.X - this.worldPos.X, target.worldPos.Y - this.worldPos.Y);
    float wantRot = (float)Math.Atan2(distance.Y, distance.X);
    if (this.rotation < wantRot)
        this.rotation += MathHelper.ToRadians(45) * Time.deltaTime;
    if (this.rotation > wantRot)
        this.rotation -= MathHelper.ToRadians(45) * Time.deltaTime;

What i want to achieve is have it rotate (in Case 2) according to the red arrow instead of the brown one.

NOTE: I’m not an expert at programming, I’ve only done it from time to time for the past year(Mainly simple 2D Shooter/shoot’em up kind of games), so in depth explanation would be appreciated. I’m also a student learning programming.

PS: Suggestions for Title would also be appreciated as I had absolutely no idea what to put there.

  • 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-26T11:16:19+00:00Added an answer on May 26, 2026 at 11:16 am

    Your problem is that the target could be at angle 5, and the object could be facing 355 degrees (for example). According to your test, 5 is less than 355, so go anticlockwise.

    What you should do is test whether the target is within 180 degrees to your left, or within 180 degrees to your right, then move accordingly.

    The tricky part is getting the check to ‘wrap’ around 360 <-> 0. It looks like 0 degrees is left in your case, so the hard test is for when the wantRot is on the side that has 0 degrees within it.

    To visualise draw a circle as below, then place your object on the left of where we’re facing. You’ll see that you have to check the 2 shaded areas separately.

    Visualisation

    Method 1

    Check all cases separately.

    Note: Code below is in my head and untested. You’ll need to change degrees to radians.

    int MoveDir = 0;
    var BehindMe = this.rotation - 180;
    if (BehindMe < 0)
        BehindMe += 360;
    
    if (wantRot != this.rotation)
    {
        if (wantRot == BehindMe)
            MoveDir = 1; // or randomly choose
        else if ((wantRot > BehindMe && wantRot < this.rotation) ||
                 (this.rotation < 180 && (wantRot > BehindMe ||
                                          wantRot < this.rotation)))
            MoveDir = -1;
        else if ((wantRot < BehindMe && wantRot > this.rotation) ||
                 (this.rotation > 180 && (wantRot < BehindMe ||
                                          wantRot > this.rotation))
            MoveDir= 1;
    
        this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime;
    }
    

    Method 2

    From looking at the image, you may realise that you could just check whether the object on the right, then if not, assume it’s on the left (since as long as the current angle is less than 180 degrees checking its on the right is easy). If the current angle is more than 180 degrees, then reverse the concept – check whether it’s on the left and if not assume right. Something like below:

    int MoveDir = 0;
    var BehindMe = this.rotation - 180;
    if (BehindMe < 0)
        BehindMe += 360;
    
    if (wantRot != this.rotation)
    {
        if (this.rotation <= 180)
        {
            if (wantRot > this.rotation && wanrRot < BehindMe)
                MoveDir = 1;
            else
                MoveDir = -1;
        }
        else
        {
            if (wantRot < this.rotation && wanrRot > BehindMe)
                MoveDir = -1;
            else
                MoveDir = 1;
        }
    
        this.rotation += MoveDir * MathHelper.ToRadians(45) * Time.deltaTime;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

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.