2D精灵旋转 [英] 2D Sprite Rotation

查看:73
本文介绍了2D精灵旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我需要旋转2D精灵的帮助.我想要做的是不断平滑旋转精灵,但是旋转速度会不时变化.它加速和减速.

I need help rotating 2D sprites. What I’m trying to do is smooth rotate a sprite constantly but the speed of the rotation varies from time to time. It accelerates and decelerates.

我使用浮动速度乘以(float)gameTime.ElapsedGameTime.TotalMillisecods来增加spriteBatch.Draw方法中的旋转.我正在使用弧度,它将继续无限旋转,没有问题,但是角度不断增加,我 在变量变得太大并崩溃之前,不知道它将旋转多长时间.

I use a float Speed multiplied by (float)gameTime.ElapsedGameTime.TotalMillisecods to add to the rotation in the spriteBatch.Draw method. I am using radians and it will continue to rotate indefinitely without problems but the angle keeps adding up and I don’t know for how long it will rotate until the variable becomes too big and crashes.

我还知道选项之一是在每次旋转时添加一个IF,然后查看角度是否为特定数值,然后将变量重置为再次从cero开始,但是由于sprite旋转的速度不同,因此重置为cero会导致精灵 跳起来,我需要一直保持平稳旋转.

I also know that one of the options is to add an IF at every rotation and see if the angle is at a specific number then resetting the variable to start from cero again but because the sprite rotates at different speeds, resetting to cero will cause the sprite to "jump" and I need smooth rotation all the time.

谢谢…

推荐答案

这是一种使用变量计算每帧旋转的方法:

This is one way you can calculate your rotation each frame using your variables:

rot =((int)(速度*(float)gameTime.TotalGameTime.TotalMilliseconds)%360);

rot = ((int)(speed * (float)gameTime.TotalGameTime.TotalMilliseconds) % 360);

这将始终为您提供0到359的值,该值可以平稳过渡(如果希望它以其他方式旋转,请在前面加上负号),然后可以将其与以下项一起使用:MathHelper.ToRadians(rot)

This will always give you a 0 to 359 value which transitions smoothly (put a negative sign in front if you want it to rotate the other way), which you can then use with: MathHelper.ToRadians(rot)

此处的键是'%'运算符.它需要一个int值,其功能类似于除法运算,只不过它返回余数.它将允许您使用任何IF语句来解决问题.另外,我以0.03f的速度为例.

The key here is the '%' operator. It requires an int value and functions like a division calculation, except it returns the remainder instead. It will allow you to get around using any IF statements. Also, I used a speed of 0.03f as an example.


这篇关于2D精灵旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆