1
votes

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
what are _x and _y? are they different from position? do you calculate destination every frame? what does moveTo look like?Dave Cousineau
I made some changes to the original post to hopefully clear up some confusion, as you can see the issue is happening when the sprite references its own vector2 coordinates. thanks for your interest!scape

1 Answers

0
votes

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.