如何计算钟针的位置?

时间:2017-05-11 09:30:40

标签: c# xna

我有两个精灵,一个矩形和一个三角形,它们一起形成一个钟表。 当我旋转矩形和三角形时,例如顺时针旋转大约30度(时钟上是5分钟),那么时钟指针看起来很奇怪,因为三角形的位置不正确。

如何计算三角形的新位置?

我的时钟指针(开始前): clock hand at the beginning

顺时针旋转约30度后,我的时钟指针: clock hand after rotating

float CurrentRotation;

Vector2 RectanglePosition, TrianglePosition;

Drawing the sprites:
spriteBatch.Draw(RectangleSprite, RectanglePosition, null, Color.White, CurrentRotation, new Vector2(RectangleSprite.Width / 2.0f, RectangleSprite.Height / 2.0f), 1.0f, SpriteEffects.None, 1.0f);

spriteBatch.Draw(TriangleSprite, TrianglePosition, null, Color.White, CurrentRotation, new Vector2(TriangleSprite.Width / 2.0f, TriangleSprite.Height / 2.0f), 1.0f, SpriteEffects.None, 1.0f);

更新:

namespace Clockhand
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D RectangleSprite, TriangleSprite;
    float Rectanglerotation, Trianglerotation, Angle;
    Vector2 Rectangleposition, Triangleposition;
    SpriteFont Font;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.IsFullScreen = true;
    }

    protected override void Initialize()
    {

        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Font = Content.Load<SpriteFont>("FontArial");
        RectangleSprite = Content.Load<Texture2D>("Rectangle");
        TriangleSprite = Content.Load<Texture2D>("Triangle");

        //Rectangleposition is the centre of the cercle:
        Rectangleposition = new Vector2(500, 500);

        Angle = 0;
        double radians = Angle * Math.PI / 180;
        Rectanglerotation = (float)radians;
        Trianglerotation = Rectanglerotation;

        int newX = (int)(Rectangleposition.X + RectangleSprite.Width / 2.0f * Math.Cos(radians));
        int newY = (int)(Rectangleposition.Y + RectangleSprite.Width / 2.0f * Math.Sin(radians));
        Triangleposition = new Vector2(newX, newY);

        TouchPanel.EnabledGestures = GestureType.Tap;
    }


    protected override void Update(GameTime gameTime)
    {
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gs = TouchPanel.ReadGesture();
            switch (gs.GestureType)
            {
                case GestureType.Tap:
                    Angle += 30;
                    double radians = Angle * Math.PI / 180;
                    Rectanglerotation = (float)radians;
                    Trianglerotation = Rectanglerotation;

                    int newX = (int)(Rectangleposition.X + RectangleSprite.Width / 2.0f * Math.Cos(radians));
                    int newY = (int)(Rectangleposition.Y + RectangleSprite.Width / 2.0f * Math.Sin(radians));
                    Triangleposition = new Vector2(newX, newY);
                    break;
            }
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
          spriteBatch.DrawString(Font, "Current angle: " + Angle.ToString(), new Vector2(400, 300), Color.White);
          spriteBatch.Draw(RectangleSprite, Rectangleposition, null, Color.White, Rectanglerotation, new Vector2(RectangleSprite.Width / 2.0f, RectangleSprite.Height / 2.0f), 1.0f, SpriteEffects.None, 1.0f);
          spriteBatch.Draw(TriangleSprite, Triangleposition, null, Color.White, Trianglerotation, new Vector2(TriangleSprite.Width / 2.0f, TriangleSprite.Height / 2.0f), 1.0f, SpriteEffects.None, 1.0f);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

不行。三角形位置不正确。我的计算错了,我不知道我做错了什么。

有人可以帮我计算一下吗?

我拍了一些钟表的照片。

Current angle 0 Current angle 30

0 个答案:

没有答案