实施摇晃事件?
我正在为游戏制作一个 GUI API,其中一个请求的功能是摇动事件。本质上是一个与 Windows 7 的 Aero Shake 非常相似的事件。当鼠标按下时,如果单向快速来回移动,则会触发该事件。我只是不确定这里面会用到什么类型的伪代码?
I'm making a gui API for games and one requested feature was a shake event. Essentially an event very similar to Windows 7's Aero Shake. When the mouse is down, if it is rapidly moved back and forth uni directionally, the event is fired. I'm just not sure what type of psedocode would go into this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能会考虑这样的事情:
I might consider something like this:
您需要计算鼠标移动的变化。如果鼠标在不到 0.7 秒的时间内改变方向超过 3 次,则为晃动。要检测方向的变化,请永远跟踪最后 5 个鼠标坐标。如果点 P0 是最后一个,P5 是倒数第五个,则计算 P0-P3 和 P3-P5 之间的角度。如果角度小于 5 度,则鼠标改变方向。
You need to count the changes in mouse movement. If mouse changes direction more than 3 times in less than, say, 0.7 seconds then it's a shake. To detect the change in direction track forever last 5 mouse coordinates. If point P0 is last, and P5 fifth to last then calculate an angle made between P0-P3 and P3-P5. If the angle is lest that 5 degrees then the mouse changed direction.
总体思路如下:
鼠标按下时:
(x0, y0)
。斜率= 0
。开始时间=当前时间
。鼠标移动时:
(x, y)
。当前时间 - 开始时间 > 是否为当前时间 - 开始时间 > 。摇动时间
,如果是,则触发摇动事件并重置开始时间
。slope = newSlope
并重置start time = current time
。The general idea would be something like this:
On mouse down:
(x0, y0)
.slope = 0
.start time = current time
.On mouse move:
(x, y)
.newSlope = abs((y - y0) / (x - x0))
. (this indicates the direction of mouse movement with respect to the starting position)abs(newSlope - slope) < some threshold
then the user is still moving in the same direction: then check ifcurrent time - start time > shake time
, if so, fire the shake event and resetstart time
.slope = newSlope
and resetstart time = current time
.我找到了更优雅、更切题的答案。跟踪最后 N 个(比如 20 个)鼠标位置。这些位置以矩形为界,其顶部为最小值(P(N).top),底部=最大值(P(N).bottom)等。计算鼠标在最后N次期间移动的总距离。如果行进的总距离比该矩形的周长大 K 倍以上,则发生摇晃。
您可能想根据时间确定 N。仅获取最多 1.5 秒左右的旧鼠标位置。
I found more elegant and more to the point answer. Track last N (say 20) mouse positions. Those positions are bounded by rectangle whose top is minimum(P(N).top), bottom = maximum(P(N).bottom), etc. Calculate the total distance the mouse moved during those last N times. If the total distance traveled is more than K times larger than that rectangle's perimeter then it's a shake.
You may want to determine N based on time. Take only the mouse positions that are old at most 1.5 seconds, or so.