Here's the class code from my Game1.cs file. The only thing I left out was the using statements and the namespace.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
float inputDelay;
float maxInputDelay = 0.2f;
Texture2D shipTexture;
Vector2 shipTextureOriginPoint;
Texture2D dotTexture;
Vector2 dotTextureOriginPoint;
Vector2 dotPositionPoint;
Vector2 parentShipPositionPoint;
float parentShipRotationRadians;
Vector2 childShipPositionPoint;
float childShipRotationRadians;
Matrix childShipMatrix;
Matrix childShipRotationMatrix;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
shipTexture = Content.Load<Texture2D>(@"textures\ship_with_outline");
dotTexture = Content.Load<Texture2D>(@"textures\dot");
shipTextureOriginPoint = new Vector2(shipTexture.Width / 2, shipTexture.Height / 2);
dotTextureOriginPoint = new Vector2(dotTexture.Width / 2, dotTexture.Height / 2);
dotPositionPoint = new Vector2(GraphicsDevice.Viewport.Width / 2,
GraphicsDevice.Viewport.Height / 2);
parentShipPositionPoint = new Vector2(GraphicsDevice.Viewport.Width / 2,
GraphicsDevice.Viewport.Height / 2);
parentShipRotationRadians = 0;
childShipPositionPoint = new Vector2(parentShipPositionPoint.X - 47,
parentShipPositionPoint.Y - 47);
childShipRotationRadians = 0;
childShipMatrix = new Matrix();
childShipMatrix.Translation = new Vector3(childShipPositionPoint.X,
childShipPositionPoint.Y,
0);
childShipRotationMatrix = new Matrix();
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
if (inputDelay > 0)
inputDelay -= (float)gameTime.ElapsedGameTime.TotalSeconds;
if (inputDelay < 0)
inputDelay = 0;
if (inputDelay == 0)
{
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
parentShipRotationRadians += MathHelper.ToRadians(15);
childShipRotationRadians += MathHelper.ToRadians(15);
Matrix.CreateRotationZ(parentShipRotationRadians, out childShipRotationMatrix);
childShipPositionPoint = Vector2.Transform(childShipPositionPoint,
childShipRotationMatrix);
inputDelay = maxInputDelay;
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(dotTexture,
dotPositionPoint,
null,
Color.Yellow,
0,
dotTextureOriginPoint,
0.6f,
SpriteEffects.None,
0.001f);
spriteBatch.Draw(shipTexture,
parentShipPositionPoint,
null,
Color.BlueViolet,
parentShipRotationRadians,
shipTextureOriginPoint,
2.0f,
SpriteEffects.None,
0.301f);
spriteBatch.Draw(shipTexture,
childShipPositionPoint,
null,
Color.Orange,
childShipRotationRadians,
shipTextureOriginPoint,
1.0f,
SpriteEffects.None,
0.300f);
spriteBatch.End();
base.Draw(gameTime);
}
}
I basically want to rotate the parent ship by 15 degrees when I press the right key and have the child ship stay in the correct formation to it. I also want to rotate the parent ship by -15 degrees when I press the left key and have the child ship stay in the correct formation to it.
This is just a really simple sandbox I'm using to learn how to implement this.
Roy