Honestly I have never taken a trigonometry class, but I am trying to create a dual stick shooter in XNA, can someone please look at my code and look to see if there are things that can be fixed, The image facing is off? Why?
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 DualStickPC
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
//set window size
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
}
protected override void Initialize()
{
base.Initialize();
}
Texture2D mytexture;
Texture2D curTexture;
Vector2 sPos = Vector2.Zero;
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mytexture = Content.Load<Texture2D>("Arrow");
curTexture = Content.Load<Texture2D>("Cursor");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
sPos.X = GraphicsDevice.Viewport.Width/2;
sPos.Y = GraphicsDevice.Viewport.Height/2;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
int x2 = Mouse.GetState().X;
int x1 = (int) sPos.X;
int y2 = Mouse.GetState().Y;
int y1 = (int) sPos.Y;
float rotation = (float)Math.Atan2(Mouse.GetState().Y - sPos.Y + (int)mytexture.Height / 2, Mouse.GetState().X - sPos.X + (int)mytexture.Width / 2);
float slope = 0;
try
{
slope = (y2 - y1) / (x2 - x1);
}
catch
{
slope = 180;
}
double angle = 100 * Math.Tan(slope);
float radians = MathHelper.ToRadians((float)angle);
int msX = (int)(Mouse.GetState().X - sPos.X - mytexture.Width/2);
int msY = (int)(Mouse.GetState().Y - sPos.Y - mytexture.Height / 2);
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
Vector2 mousePos = Vector2.Zero;
mousePos.X = Mouse.GetState().X;
mousePos.Y =Mouse.GetState().Y;
spriteBatch.Draw(curTexture, mousePos,Color.White);
spriteBatch.Draw(mytexture, new Rectangle((int)sPos.X, (int)sPos.Y, mytexture.Height, mytexture.Width), null, Color.White, (float) (rotation), new Vector2(mytexture.Width/2, mytexture.Height/2), SpriteEffects.None, 0);
//Testing projectile
spriteBatch.Draw(mytexture, new Rectangle((int)sPos.X + msX/2, (int)sPos.Y + msY/2, mytexture.Height, mytexture.Width), null, Color.White, 0, new Vector2(mytexture.Width / 2, mytexture.Height / 2), SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
