在 xna 中,如何正确引用与原点相关的 vector2 坐标

本文关键字:原点 坐标 vector2 引用 xna 何正确 | 更新日期: 2023-09-27 18:35:27

在xna中,我有一个位置向量为2的精灵,当引用精灵绘制的坐标时。(暂时忽略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

在 xna 中,如何正确引用与原点相关的 vector2 坐标

如果您经常使用这些参数调用 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

因此,您可以向精灵的类添加一个 CenterPosition 属性。