[英]in xna how do i properly reference the vector2 coordinates in relation to origin?
在xna中,当引用精灵绘制的坐标时,我有一个带有位置vector2的精灵。 (暂时不考虑z,这是根据屏幕位置正确堆叠精灵的一次失败尝试)。 当手动引用vector2坐标时,我似乎总是以原点结束,请明白我的意思。
//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);
通过引用精灵的X和Y向量坐标来绘制精灵时,一切都是笨拙的。 如果我尝试以其他任何方式手动使用这些相同的坐标,则这些坐标似乎总是以原点(左上角)结尾。 除此之外,一切都顺利进行,精灵的运动很好
//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");
}
}
因此,在使用屏幕坐标时,子画面可以很好地移动到目标位置; 但是如果我将子画面自己的向量x和y坐标用作目的地的一部分,那会发生什么,随着时间的流逝,子画面最终会像命运一样出现在左上角。 为什么会这样? 手动参考坐标时也会发生这种情况,也可以通过其他方式(例如碰撞检测)进行参考。 我实际上可以看到2个坐标在从原点开始的循环中递减计数,即使我将向量归零并重新将其坐标重新应用到向量上,也会发生这种情况(如果vector2甚至保留了该信息,则我将不执行任何操作但会删除任何幅度) )。 谢谢您的帮助!
// 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
有趣的是,如果我指定另一个子画面的vector2位置坐标,它似乎可以正常工作:
//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
如果您定期使用这些参数调用moveTo函数,则精灵最终会出现在左上角。 子画面的位置成员是其左上角。 但是,在moveTo函数中,您需要中心的位置。 这就是为什么您减去其大小的一半。 因此,当您将精灵的中心永久移动到其左上角时,当您防止精灵离开时,它将导致屏幕的左上角。
解决方案是使用精灵的实际中心点作为参考:
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
因此,您可以在Sprite的类中添加CenterPosition属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.