Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:12:33+00:00 2026-06-15T09:12:33+00:00

I am trying to make a simple game in XNA. I have a player

  • 0

I am trying to make a simple game in XNA.

I have a player which has spritesheet next to it.
The spritesheet is a sort of weapon, with a tip.

How can I get this sprite to rotate with the tip facing the mouse position?

        float y2 = m_Mouse.Y;
        float y1 = m_WeaponOrigin.Y;
        float x2 = m_Mouse.X;
        float x1 = m_WeaponOrigin.X;

        // Get angle from mouse position.
        m_Radians = (float) Math.Atan2((y2 - y1), (x2 - x1));

Drawing with: 
activeSpriteBatch.Draw(m_WeaponImage, m_WeaponPos, r, Color.White, m_Radians, m_WeaponOrigin, 1.0f, SpriteEffects.None, 0.100f);

while this makes it rotate, it does not follow the mouse correctly, and it behaves weirdly.

Any hints on how to make this work?

Another issue I have is to define a point and which is the muzzle, and update it based on the angle as well, so that shots will fire correctly from that point towards the mouse.

Thanks


Screenshots:
early, getting the mouse and cursor in place

playing with the super laser

using the superlaser on every enemy type

Thanks again, turned out to be a fun game.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:12:34+00:00Added an answer on June 15, 2026 at 9:12 am

    Basically, use Math.Atan2.

    Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
    Vector2 dPos = _arrow.Position - mousePosition;
    
    _arrow.Rotation = (float)Math.Atan2(dPos.Y, dPos.X);
    

    Proof of concept (I used the plus texture for the cursor – it doesn’t show on the sceenshot unfortunately):

    Pointing to cursor


    “What is _arrow?”

    In that example _arrow is of type Sprite, which might come in handy in some situations, and will sure make your code look a bit cleaner:

    public class Sprite
    {
        public Texture2D Texture { get; private set; }
    
        public Vector2 Position { get; set; }
        public float Rotation { get; set; }
        public float Scale { get; set; }
    
        public Vector2 Origin { get; set; }
        public Color Color { get; set; }
    
        public Sprite(Texture2D texture)
        {
            this.Texture = texture;
        }
    
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(this.Texture, 
                             this.Position, 
                             null, 
                             this.Color, 
                             this.Rotation, 
                             this.Origin, 
                             this.Scale, 
                             SpriteEffects.None, 
                             0f);
        }
    }
    

    Declare:

    Sprite _arrow;
    

    Initiate:

    Texture2D arrowTexture = this.Content.Load<Texture2D>("ArrowUp");
    _arrow = new Sprite(arrowTexture)
            {
                Position = new Vector2(100, 100),
                Color = Color.White,
                Rotation = 0f,
                Scale = 1f,
                Origin = new Vector2(arrowTexture.Bounds.Center.X, arrowTexture.Bounds.Center.Y)
            };
    

    Draw:

    _spriteBatch.Begin();
    _arrow.Draw(_spriteBatch, gameTime);
    _spriteBatch.End();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started trying to make a simple game with XNA 3.1 to
Trying to make simple minesweeper game in python, but have one problem. I have
I am trying to make a simple game in C#/XAML. I have a worker
For quite awhile I have been trying to make a simple game in Java
I am trying to make a simple 3D game for windows with XNA and
i am trying to make a very simple game where i have 7 positions
I am trying to make a simple pong game using opengl-es. I have checked
I'm trying to make a very simple game. In it I have objects (
I am trying to make a fairly simple grid game where users have to
I am playing around with trying to make a simple Roguelike game to learn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.