0

I have two sprites, a rectangle and a triangle and together they form a clock hand. When I rotate the rectangle and the triangle, for example about 30 degrees clockwise(that would be 5 minutes on a clock), then the clock hand looks weird because the position of the triangle is not correct.

How can I calculate the new position of the triangle?

my clock hand at the beginning(before rotating): clock hand at the beginning

my clock hand after rotating about 30 degrees clockwise: 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);

Update:

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);
    }
}

}

It´s not working. The triangle is not on the correct position. My calculations are wrong and I don´t know what I´m doing wrong.

Could anybody help me with the calculations?

I made some pictures of the clock hand.

Current angle 0 Current angle 30

JohnDark0
  • 35
  • 5
  • Not too familiar with xna but my first thought is you should group the sprites and then apply transforms to the group. Perhaps something like https://gamedev.stackexchange.com/questions/23034/ but not necessarily that complicated - also gamedev might be more suitable for your question :) – Alex Paven May 11 '17 at 09:34
  • Is it possible to calculate the new position of the triangle with the Pythagorean theorem? If yes, how can I calculate it? – JohnDark0 May 11 '17 at 13:19
  • It's not as simple as just the pythagorean theorem because you need to position the triangle on a circle, in addition to rotating it, but sure it can be done. See http://stackoverflow.com/questions/5300938 – Alex Paven May 11 '17 at 14:27
  • What you want is to rotate both of your objects around the same point. See marked duplicate for how to do that. There's not enough context in your question to know for sure what other approaches might work, but you should look at https://stackoverflow.com/questions/10065072/rotating-the-whole-level. It may be that applying rotate e.g. to your projection matrix/view as suggested on the other question could be useful in your scenario. – Peter Duniho May 12 '17 at 04:40
  • Thanx for the links. In addition, I found this: https://gamedev.stackexchange.com/questions/18340/get-position-of-point-on-circumference-of-circle-given-an-angle But my calculations are wrong and I don´t know what I´m doing wrong. – JohnDark0 May 12 '17 at 12:10

0 Answers0