English 中文(简体)
在 xna 中,我如何恰当地参照矢量2 坐标与源值的关系?
原标题:in xna how do i properly reference the vector2 coordinates in relation to origin?

在 xna i 中, 有一种带有位置矢量 2 的图示, 当引用图示绘制的坐标时 。 (目前, 忽略 z, 这是根据屏幕位置正确堆放图示的尝试失败 。) 当手动引用矢量2 坐标时, 我似乎总是在来源处结束, 请看我是什么意思 。

//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);

在绘制图案时,所有的东西都是hunky dory, 并参照其 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");

            }
        }

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

有趣的是,如果我指定另一个图示矢量2位置的coords, 似乎效果不错:

//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
最佳回答

如果您定期调用带有这些参数的“ 移动To” 函数, 自然会让“ 移动” 最终出现在左上角。 该“ 图例” 的成员是其左上角。 但是, 在移动中, 您期望着中心的位置 。 这就是为什么您要将其中一半的大小减掉。 所以, 当您永久移动图例的中心到左上角时, 当您阻止图例离开它时, 它将导致屏幕左上角 。

解决方案是使用图示的实际中点作为参考 :

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 类中。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签