회전 반경을 사용하여 회전을 증가 및 감소시켜 스프라이트를 회전 시키려고합니다. 마우스가 왼쪽 상단에 있고 오른쪽 상단으로 움직일 때까지 잘 작동합니다. 각도 차이가 현재 회전 값보다 크면 스프라이트의 회전이 증가하고 그렇지 않으면 감소합니다. 그래서 6.5 라디안에서 0으로 갈 때 시계 방향으로 15도 정도가 아닌 약간의 각도로 350도 반 시계 방향으로 회전합니다. 나와 나와 다른 사람들은 하루 종일이 문제를 해결하는 방법을 모르고 있습니다. 내 코드는 다음과 같습니다 :스프라이트 회전 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);
}
}
}
라디안으로 두 개의 float 각도를 간단히 가졌다면 어떻게하는지 생각해보십시오. – Jack
@ 잭 : 그게 이미 대답입니까? 각도를 방향 벡터로 바꾸려면 그냥 (Cos (Rotation), Sin (Rotation)) –
플로트를 Vector3으로 변형하지 않고이 작업을 수행 할 수 있는지 궁금합니다. 그 변환을 거쳐 다시 부유물로 돌아 가기 위해 조금 외적인 것 같군요? – Jack