实施摇晃事件?

发布于 2024-10-01 07:27:27 字数 127 浏览 3 评论 0原文

我正在为游戏制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ヤ经典坏疍 2024-10-08 07:27:27

我可能会考虑这样的事情:

  1. 总是为鼠标最后一次移动的方向创建一个向量
  2. ,当您收集下一个向量时,总是保留前一个向量的记录(这意味着您总是有两个向量要评估)。
  3. 当每个鼠标输入到达时,对两个向量进行点积。如果点积为正,则忽略它。如果为负数,则用它设置一个时间值并将其存储在一个数组中,该数组最多可存储五个时间戳。
  4. 重复上述顺序。如果时间戳数组具有一组连续的时间戳,每个时间戳相差一定的 epsilon,那么您可以假设有人正在摇晃某些东西。 :)

I might consider something like this:

  1. always create a vector for the direction the mouse travelled on its last move
  2. always keep a record of the immediate prior vector, as you gather the next vector (which means you always have two vectors to evaluate).
  3. do a dot-product of the two vectors, as each mouse input arrives. If the dot-product is positive, then ignore it. If it is negative, then set a time value with it and store in an array tha stores perhaps up to five of these time stamps.
  4. repeat the above sequence. If the timestamp array has a consecutive set of timestamps that each differ by some epsilon, then you can presume someone is shaking something. :)
不爱素颜 2024-10-08 07:27:27

您需要计算鼠标移动的变化。如果鼠标在不到 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.

安稳善良 2024-10-08 07:27:27

总体思路如下:

鼠标按下时:

  1. 将当前位置存储为(x0, y0)
  2. 设置斜率= 0
  3. 设置开始时间=当前时间

鼠标移动时:

  1. 将当前位置存储为(x, y)
  2. 设置newSlope = abs((y - y0) / (x - x0))。 (这表示鼠标相对于起始位置的移动方向)
  3. 如果abs(newSlope - 斜率) <某个阈值,那么用户仍然朝同一方向移动:然后检查当前时间 - 开始时间 > 是否为当前时间 - 开始时间 > 。摇动时间,如果是,则触发摇动事件并重置开始时间
  4. 否则,用户已经改变方向。设置slope = newSlope并重置start time = current time

The general idea would be something like this:

On mouse down:

  1. Store the current position as (x0, y0).
  2. Set slope = 0.
  3. Set start time = current time.

On mouse move:

  1. Store the current position as (x, y).
  2. Set newSlope = abs((y - y0) / (x - x0)). (this indicates the direction of mouse movement with respect to the starting position)
  3. If abs(newSlope - slope) < some threshold then the user is still moving in the same direction: then check if current time - start time > shake time, if so, fire the shake event and reset start time.
  4. Otherwise, the user has changed direction. Set slope = newSlope and reset start time = current time.
浮光之海 2024-10-08 07:27:27

我找到了更优雅、更切题的答案。跟踪最后 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文