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
_x
and_y
? are they different fromposition
? do you calculate destination every frame? what doesmoveTo
look like? – Dave Cousineau