我想旋轉一個精靈來面對鼠標通過使用旋轉半徑增加和減少旋轉。它的工作正常,直到鼠標左上角並移動到右上角。我擁有它,所以如果角度差異大於當前的旋轉值,精靈的旋轉會增加,否則它會減少。所以當它從6.5弧度變爲0時,它逆時針旋轉350度 - 某些度數,而不是順時針旋轉15度 - 某些度數。我和其他人一整天都在爲此工作,並不知道如何解決這個問題。我的代碼如下:雪碧旋轉XNA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;
namespace Physics
{
public class Ship
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public double Rotation { get; set; }
MouseState prevState, currState;
Vector2 A, B;
public const double NINETY_DEGREES = 1.57079633;
double turningRadius = 2 * (Math.PI/180);
double targetRotation, rotationDifferential;
public Ship(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
A = new Vector2(Position.X, Position.Y);
B = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
Rotation = 0;
}
public void Update()
{
currState = Mouse.GetState();
A.X = Position.X;
A.Y = Position.Y;
B.X = currState.X;
B.Y = currState.Y;
if (B.Y > A.Y)
if (B.X > A.X) //Bottom-right
targetRotation = Math.Atan((B.Y - A.Y)/(B.X - A.X)) + NINETY_DEGREES;
else //Bottom-left
targetRotation = (Math.Atan((A.X - B.X)/(B.Y - A.Y))) + (NINETY_DEGREES * 2);
else
if (B.X > A.X) //Top-right
targetRotation = Math.Atan((B.X - A.X)/(A.Y - B.Y));
else //Top-Left
targetRotation = Math.Atan((A.Y - B.Y)/(A.X - B.X)) + (NINETY_DEGREES * 3);
if (Rotation > targetRotation)
Rotation -= turningRadius;
else
Rotation += turningRadius;
prevState = currState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, null, Color.White, (float)Rotation, new Vector2(Texture.Width/2, Texture.Height/2), 0.5f,
SpriteEffects.None, 0.0f);
}
}
}
任何想法如果我只有兩個弧度的浮動角度如何做到這一點? – Jack 2015-07-20 08:20:03
@傑克:這已經在答案?爲了將角度轉換爲方向矢量,它只是'(Cos(旋轉),Sin(旋轉))' – 2015-07-20 18:03:00
我想我想知道是否有這樣做的方法,而不將浮點數轉換爲Vector3。似乎有點...無關經歷該轉換並再次回到浮動狀態? – Jack 2015-07-20 18:24:25