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