Jump to content
  • Advertisement
Sign in to follow this  
rherm23

Rotating sprite issues

This topic is 2522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to render a sprite on the screen that will not move but only rotate.

I want to make it so when you press the 'A' key it rotates counterclockwise and if you press the 'D' key it rotates clockwise

I am very new to XNA so i know my code isnt the best atm i tend to make 1 small program and add to it piece by piece so when i go through these learning programs they tend to not be as clean as they can be.


class GameObject
{
public Vector2 position;
public Rectangle rect;
public Texture2D image;
public float rotation;
public Vector2 origin;
public GameObject()
{
position = new Vector2(300, 300);
rect = new Rectangle(300, 300, 64, 64);
rotation = 0.0f;
origin.X = position.X + (rect.Width / 2);
origin.Y = position.Y + (rect.Height / 2);
}
public void loadContent(ContentManager manager)
{
image = manager.Load<Texture2D>("up");
}
public void draw(SpriteBatch batch)
{

batch.Draw(image, position, Color.White);
//batch.Draw(image, rect, null, Color.White, rotation, origin, SpriteEffects.None, 0f);


}
public void update(KeyboardState k_state)
{

if (k_state.IsKeyDown(Keys.A))
{
rotation -= 0.1f;
}
if (k_state.IsKeyDown(Keys.D))
{
rotation += 0.1f;
}
}
}


in my draw method when i make the uncommented call it draws my arrow fine ... no rotation but everything is drawn at the correct spots.
when i draw using the commented part the arrow is in a completely different position and it pivots more than it rotates. can anyone help me at all?

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
made another program and ill post code .... my draw code is

// this line draws my arrow in the perfect spot
//spriteBatch.Draw(image, position, Color.White);

// this line draws my arrow half visible in the top left corner of the screen. and it doesnt rotate but pivot.
spriteBatch.Draw(image, position, null, Color.White, rotation, origin, 1.0f,SpriteEffects.None, 0);

can anyone help me out here? thanks in advance


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace ArrowRotation
{

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font1;
KeyboardState k_current_state;
Vector2 position;
Vector2 origin;
float rotation;
Texture2D image;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
// TODO: Add your initialization logic here
position.X = 200;
position.Y = 200;
origin.X = position.X + 32;
origin.Y = position.Y + 32;
rotation = 0.0f;
base.Initialize();
}

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
font1 = Content.Load<SpriteFont>("testFont");
image = Content.Load<Texture2D>("up");

}

protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)
{
k_current_state = Keyboard.GetState();
if (k_current_state.IsKeyDown(Keys.Escape))
{
this.Exit();
}
if (k_current_state.IsKeyDown(Keys.A))
{
rotation -= 0.1f;
}
if (k_current_state.IsKeyDown(Keys.D))
{
rotation += 0.1f;
}

base.Update(gameTime);
}

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

spriteBatch.DrawString(font1, "rotation = " + rotation.ToString(), new Vector2(0, 0), Color.White);
spriteBatch.DrawString(font1, "X position = " + position.X.ToString(), new Vector2(0, 30), Color.White);
spriteBatch.DrawString(font1, "Y position = " + position.Y.ToString(), new Vector2(0, 60), Color.White);
spriteBatch.DrawString(font1, "X Origin = " + origin.X.ToString(), new Vector2(0, 90), Color.White);
spriteBatch.DrawString(font1, "Y Origin = " + origin.Y.ToString(), new Vector2(0, 120), Color.White);
//spriteBatch.Draw(image, position, Color.White);
spriteBatch.Draw(image, position, null, Color.White, rotation, origin, 1.0f,SpriteEffects.None, 0);

spriteBatch.End();
base.Draw(gameTime);
}
}
}

Share this post


Link to post
Share on other sites
The problem is when you are setting the Origin of the sprite. What you will want to do is set the origin after you have loaded your texture:


protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
font1 = Content.Load<SpriteFont>("testFont");
image = Content.Load<Texture2D>("up");

origin = new Vector2(0, 0);
origin.X = image.Width / 2;
origin.Y = image.Height / 2;
}


This will set the origin at the center of the sprite and when the sprite is rotated, it will rotate at the center. You need to keep in mind that the origin X and Y should not be greater than the Width and Height of the sprite you are applying it to. For example:


// the origin would be at the top left corner of the texture
origin.X = 0;
origin.Y = 0;

// the origin would be at the top right corner of the texture
origin.X = image.Width;
origin.Y = 0;

// the origin would be at the bottom right corner of the texture
origin.X = image.Width;
origin.Y = image.Height;

// and of course, the origin at the center of the texture
origin.X = image.Width / 2;
origin.Y = image.Height / 2;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!