我一直在尝试使用 XNA 让精灵面对我的光标,它几乎完美地工作,但是不是精灵脸部的中心,而是指向光标的角......这对于游戏中精灵中心的某些东西必须面向光标,只是想知道是否有办法让它从某个点指向......这听起来令人困惑,但我希望有人明白:)
是我使用的代码:
这 角度:
direction = mousePos - position;
rotation = (float)Math.Atan2(direction.Y, direction.X);
绘制精灵:
spriteBatch.Begin();
spriteBatch.Draw(sprite, position, null, Color.White, rotation + 90, origin, 1.0f, SpriteEffects.None, 0);
spriteBatch.End();
所以如果这是我的精灵:[]左上角将指向光标,而不是中间:P
i've been trying to get a sprite to face my cursor using XNA, it works almost perfectly, but instead of the centre of the face of the sprite, it's the corner that points to the cursor... which is no good for a game where something in the centre of the sprite must face the cursor, just wondering if there's a way to make it point from a certain point... that sounds confusing but i hope somebody gets it :)
heres the code im using:
to get the angle:
direction = mousePos - position;
rotation = (float)Math.Atan2(direction.Y, direction.X);
to Draw the sprite:
spriteBatch.Begin();
spriteBatch.Draw(sprite, position, null, Color.White, rotation + 90, origin, 1.0f, SpriteEffects.None, 0);
spriteBatch.End();
so if this is my sprite: [ ] the top left corner will point to the cursor, not the middle :P
发布评论
评论(5)
不管你做什么,都不要加90。 Atan2 函数将返回以弧度为单位的角度(一旦您开始使用它们,就会比度数好得多)。所以加上 90 就会立即失效。
如果你想将图像旋转90度,那么你需要添加PI值的一半(360度= 2xPI)。
所以这应该解决它。
dont add 90. whatever you do. the Atan2 function will be returning the angle in Radians (much nicer than degrees once you start using them). so adding 90 is going to throw it right off.
if you want to alter the rotation of the image by 90 degrees, then you need to add half the value of PI (360 degree = 2xPI).
so this should take care of it.
我不确定添加 90 是否会损害最终结果,或者不像我上面的人所说的那样,但是精灵角面向你的鼠标的原因是因为在 XNA 中它使用精灵的左上角作为原点。将其设置为精灵的中心并解决问题。
看起来像这样:
这会将其设置为精灵纹理的相对中间。
I'm not sure if the adding 90 hurts the end result or not like the person above me said, but the reason why the sprites corner is facing ur mouse is because in XNA it uses the top left most point of the sprite as the origin. set it to the center of the sprite and problem solved.
Look something like this:
this will set it to the relative middle of the sprite texture.
它看起来很丑,但避免创建新的 vector2 将有助于节省一些 fps :)
It looks ugly but avoid creating a new vector2 will contribute saving some fps :)
如果7年后你还想得到答案 xD
您需要设置WindowHandle。
该文档位于此处 。
我认为这也是一个答案。
If you still want a answer after 7 years xD
You need to set the WindowHandle.
The documentation is here.
I think this is also a Answer.
嗨,在 xna 中,他们没有度数,他们有其他我不知道名字的东西,这意味着 pi i 180 度,要得到 90 度,你只需取 3,14 / 2 = 1,57
所以:旋转 + 1.57
Hi in xna they don't have degrees they have something else that i dont know the name of and this means that pi i 180 degrees and to get 90 degrees you simply take 3,14 / 2 = 1,57
so: rotation + 1.57