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 7996705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:25+00:00 2026-06-04T14:46:25+00:00

in xna i have a sprite with a position vector2, when referencing the coordinates

  • 0

in xna i have a sprite with a position vector2, when referencing the coordinates for sprite drawing. (disregard z for now, it’s a failed attempt at properly stacking the sprites based on screen position). when manually referencing the vector2 coordinates i always seem to end up at origin, please see what i mean.

//draw sprite, this is called repeatedly during gameplay, it's inside a draw function
theGame.spriteBatch.Draw(headings[heading][frame], new Rectangle((int)theSprite.position.X, (int)theSprite.position.Y, theSprite.width, theSprite.height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, theSprite.z);

everything is hunky dory while drawing the sprite by referencing it’s X and Y vector coordinates. if I try to utilize those same coordinates manually any other way I can think of, the coordinates always seem to end up as the origin (top-left). other than that, everything works without a hitch, and the movement of the sprite is fine

//the movement code for the particular sprite:
// this is a function i call repeatedly during gameplay, it moves the sprite
        public void Move(GameTime Time)
        {
            if (!move) { return; } //not even moving? exit function

            direction = destination - position;//these are all vector2 orbjects
            //speed is a float
            position += direction * (speed * (float)Time.ElapsedGameTime.TotalSeconds);

            syncBounds();

            if (Vector2.Distance(position,destination)<bounds_sphere.Radius) //close enough? stop moving
            { 
                move = false;
            }
         }

//here's the moveTo function which triggers the move-event, i typically call it with screen coordinates
        public void moveTo(float _x, float _y, string _anim)

            destination = new Vector2(_x - (width / 2), _y - (height / 2));

            curr_anim = _anim;
            move = true; //kicks off trigger 
        }

 //here's an example of moveTo working with basic coordinates
 // Process touch events
        TouchCollection touchCollection = TouchPanel.GetState();
        foreach (TouchLocation tl in touchCollection)
        {
            if ((tl.State == TouchLocationState.Pressed))//|| (tl.State == TouchLocationState.Moved))
            {
                float px = tl.Position.X; //touch x
                float py = tl.Position.Y; //touch y

                gameObjects.Sprites[player].moveTo(px, py, "running");

            }
        }

so while using screen coordinates the sprite moves fine to the destination; but what happens if i use the sprite’s own vector-x and y coordinates as part of its destination is that over time the sprite ends up in the top left corner, like destiny.
why does this happen? it happens when referencing the coordinates by hand, in other ways too, such as collision detection. I can actually see the 2 coordinates count down over the loop down towards origin, this happens even if i zero out the vector and reapply it’s coordinates back to the vector (i assume that does nothing but remove any magnitude, if vector2 even holds that information). thanks for the help!

// i don't think this actually does anything
vector2 savepos = position;
position = Vector2.Zero;
position.x = savepos.x;
position.y = savepos.y;


//example of my code failing when referencing the vector2 coordinates
//sprite (me) auto-flee function code
int x = (int) me.position.X;
int y = (int) me.position.Y;
int dist = 2;
me.moveTo(rand.Next(x-dist,x+dist), rand.Next(y-dist, y+dist), running); //kick off moveTo function, begin the animation sequence

what’s interesting is that if i specify another sprite’s vector2 position coords it seems to work fine:

//this works and the sprites move to the other object
me.moveTo(gameObjects.Sprites["anotherspriteobject"].position.X, gameObjects.Sprites["anotherspriteobject"].position.Y, running); //kick off moveTo function, begin the animation sequence
  • 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-04T14:46:26+00:00Added an answer on June 4, 2026 at 2:46 pm

    If you regularly call the moveTo function with those parameters, it is natural that the sprite ends up in the top left corner. The position member of the sprite is its top left corner. However, in the moveTo function you expect the position of the center. That’s why you subtract half its size. So when you permanently move the center of the sprite to its top left corner, it will result in the screen’s top left corner when you prevent the sprite from leaving it.

    The solution is to use the sprites actual center point as a reference:

    int x = (int) me.position.X + me.width/2; 
    int y = (int) me.position.Y + me.height/2; 
    int dist = 2; 
    me.moveTo(rand.Next(x-dist,x+dist), rand.Next(y-dist, y+dist), running); //kick off moveTo function
    

    Therefore, you could add a CenterPosition property to the sprite’s class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

XNA doesn't have any methods which support circle drawing. Normally when I had to
XNA games have an Unload() method, where content is supposed to be unloaded. But
I have XNA game and it contains these classes public partial class Bird :
I have an XNA arcade game which runs over Silverlight environment. The game has
So I have a XNA application set up. The camera is in first person
Lets say I have an XNA Rectangle with X = 0, Y = 0,
I have a WP7 XNA game that needs to save the state whenever the
i have a problem with the stopping of a windows phone xna game. i
I have a problem with the logic of this code, XNA's Update Method is
I'm working with xna in C# and in my game I will have a

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.