Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to gradually rotate an object to face another turning the shortest distance
    text
    copied!<p>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:</p> <p><img src="https://i.stack.imgur.com/yUF9A.png" alt="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."></p> <p>Blue square = target</p> <p>Red square = the object</p> <p>Green line = rotation it wants</p> <p>Black line = current rotation</p> <p>Brown arrow = how it rotates to achieve this</p> <p>Red arrow = how I want it to rotate.</p> <p>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.</p> <p>This is the code I'm using to rotate the object.</p> <pre><code> 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 &lt; wantRot) this.rotation += MathHelper.ToRadians(45) * Time.deltaTime; if (this.rotation &gt; wantRot) this.rotation -= MathHelper.ToRadians(45) * Time.deltaTime; </code></pre> <p>What i want to achieve is have it rotate (in Case 2) according to the red arrow instead of the brown one.</p> <p>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.</p> <p>PS: Suggestions for Title would also be appreciated as I had absolutely no idea what to put there.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload