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)
{
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
"choices always were a problem for you......" Maynard James Keenan
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 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;
}
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");
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;