2

This code is to draw the tower. Square locations are the top left of the square. TILE_SIZE is simply the dimensions of the square.

SpriteBatch.Draw(TowerImage, new Rectangle(square.X * TILE_SIZE, square.Y * TILE_SIZE, TILE_SIZE, TILE_SIZE), null, Color.White, myTower.Rotation, 
    new Vector2(TILE_SIZE - 35, TILE_SIZE - 35), SpriteEffects.None, (float)0.0);

This code is how I determine the rotation

public void FaceTarget(Vector2 center, Vector2 enemyCenter)
        {
            Vector2 direction = center - enemyCenter;
            direction.Normalize();

            this.Rotation = (float)Math.Atan2(-direction.X, direction.Y);
        }

I did this based on:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Rotation.php

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Direction_to_Angle.php

The rotation is being really weird, here is how it looks normally:

http://puu.sh/bkd0E.png

But when it rotates it goes like this:

http://puu.sh/bkd6K.png

Finally when it looks down, it goes complete off path, it's not rotating by its center, but the entire image is moving why is it doing that?

https://i.sstatic.net/4PuiZ.jpg

Only the first image is actually the tower in the correct position

It seems like it is rotating of the top left point and I don't know why. Can anyone help?

1
  • did you tried Vector2(sprite.width/2,sprite.height/2) for origin ? Commented Sep 4, 2014 at 6:39

1 Answer 1

5

Apparently, your sprite is taking into consideration as the origin of rotation a Vector2.Zero (or Vector2(0,0)) point. That means the upper left point of the Texture2D file.

I see that you are setting the origin in the Draw method to TILE_SIZE - 35 which makes me wonder, is the tile a square of 70 pixels W/H?

What happens if you replace the substraction with new Vector2(TowerImage.Width / 2, TowerImage.Height / 2)?

I'll leave you an example from this site which explains easily how to rotate an image following the mouse position at all times:

Update method:

MouseState mouse = Mouse.GetState();
mousePosition = new Vector2(mouse.X, mouse.Y);

Vector2 direction = mousePosition - position;
direction.Normalize();

rotation = (float)Math.Atan2(
              (double)direction.Y, 
              (double)direction.X);

Draw method:

spriteBatch.Begin();

spriteBatch.Draw(
     rocket,
     position,
     null,
     Color.White,
     rotation,
     new Vector2(
         rocket.Width / 2, 
         rocket.Height / 2),
     1.0f,
     SpriteEffects.None,
     1.0f);

spriteBatch.End();

Check that in the posted code the rotation angle is calculated slightly different than yours, but the important piece of code is the calculation of the origin point in the Draw method.

Sign up to request clarification or add additional context in comments.

4 Comments

I will definitely try that. Also, the TILE_SIZE is equal to the textures of the path and grass, it's 40 px, for some reason if I do TILE_SIZE - 35 it puts it around the center of the tile, even if I do -TILE_SIZE or something it puts it really far away. But yes, it is rotating around the top left corner and I don't know why. Even after adjustments it is a huge rotation that is becoming difficult to control. I will try that tutorial tomorrow.
Great, let me know if that doesn't help and I'll try it on my home PC :)
Thank you! The ImageWidth/2 worked, how was I so stupid.. I tried moving it's origin, etc... It never worked
Congrats! Happy I was of help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.