Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
        }
    }
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

If you want to set your ship at the center and face to the mouse you have to use something similar to this.

Keep in mind that maybe you need to add some angle offset (90º, º180 or 270º) to your ship to face in the right direction.

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;
        mPos.X = Mouse.GetState().X;
        mPos.Y =Mouse.GetState().Y;         

        rotation = (float)Math.Atan2(mPos.Y-sPos.Y, mPos.X-sPos.X);
        sOrigin = new Vector2(ship.Width, ship.Height) * 0.5f;
        pOrigin = new Vector2(projectile.Width, projectile.Height) * 0.5f;

        pPos = sPos + Vector2.Normalize(mPos-sPos) * 100;

        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {

        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

        spriteBatch.Draw(curTexture, mPos,Color.White);
        spriteBatch.Draw(ship, sPos, null, Color.White, sOrigin, rotation, 1, SpriteEffects.None, 0) ;
        spriteBatch.Draw(projectile, pPos, null, Color.White, pOrigin, rotation, 1, SpriteEffects.None, 0) ;


        spriteBatch.End();

        base.Draw(gameTime);
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.