精灵旋转XNA
本文关键字:XNA 旋转 精灵 | 更新日期: 2023-09-27 18:26:16
我正试图通过使用旋转半径递增和递减旋转来旋转精灵以面向鼠标。它可以很好地工作,直到鼠标位于左上角并移动到右上角。这样,如果角度差大于当前"旋转"值,则精灵的"旋转"会增加,否则会减少。因此,当它从6.5弧度变为0时,它会逆时针旋转350度,而不是顺时针旋转15度。我和其他人已经为此工作了一整天,不知道如何解决这个问题。我的代码如下:
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);
}
}
}
让我确保我理解。您有一个向量(Cos(Rotation), Sin(Rotation)
,表示对象所面向的方向;另一个向量(B.X-A.X, B.Y-A.Y)
,表示您希望对象面向的方向;并且您想知道是顺时针还是逆时针旋转对象(第一个向量)以在第二个矢量的方向?
很简单,只要将它们都视为3D向量(设置Z
=0)并取它们的叉积。
- 如果生成的矢量具有正
Z
分量,则逆时针旋转 - 如果它具有正
Z
分量,则顺时针旋转 - 如果
Z
分量为0,则两个矢量是平行的,因此只需检查它们是面向相反的方向(并向任意方向旋转)还是面向相同的方向(无需执行任何操作!)
这是因为定义叉积的右手规则。
这应该只是将方向反转到更快的方向。
spin = targetRotation - Rotation;
if (abs(spin) > Math.PI)
spin *= -1;
if (spin > 0
Rotation += turningRadius;
else
Rotation -= turningRadius;
编辑:将180更改为Math.PI
尝试以下操作(这是我制作的坦克游戏中的一些代码,不确定它是否仍然适用于此场景)。我把它精简到了要点:
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Physics
{
public class Ship
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public double Rotation { get; set; }
private MouseState currState;
public Ship(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
Rotation = 0;
}
public void Update()
{
currState = Mouse.GetState();
var mouseloc = new Vector2(currState.X, currState.Y);
Vector2 direction = Position - mouseloc;
Rotation = Math.Atan2(direction.Y, direction.X) + MathHelper.PiOver2;
}
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);
}
}
}
我想你只需要多一点数学来修改旋转速率,如果我能想出一个解决方案,我会回来的。此外,此代码将根据精灵的默认旋转而变化。在我的示例中,它假设精灵面朝下(6点钟)。希望这能为你指明正确的方向!
编辑我的道歉,我把我的头从屁股上取下来,这次真的读了这个问题。这里有一个很好的小方法,我今晚花了大约3个小时写出来。
private float AngleLerp(float nowrap, float wraps, float lerp) {
float c, d;
if (wraps < nowrap) {
c = wraps + MathHelper.TwoPi;
//c > nowrap > wraps
d = c - nowrap > nowrap - wraps
? MathHelper.Lerp(nowrap, wraps, lerp)
: MathHelper.Lerp(nowrap, c, lerp);
} else if (wraps > nowrap) {
c = wraps - MathHelper.TwoPi;
//wraps > nowrap > c
d = wraps - nowrap > nowrap - c
? MathHelper.Lerp(nowrap, c, lerp)
: MathHelper.Lerp(nowrap, wraps, lerp);
} else { return nowrap; } //Same angle already
return MathHelper.WrapAngle(d);
}
它将nowrap
向wraps
值报警。wraps
角度可以是从0跳到TwoPi再跳回来的角度,所以在这种情况下是鼠标的角度。nowrap
应该是你的物体角度。
希望这次我真的能帮上忙。