我在编写鼠标宏的代码。它需要满足屏幕上的某些点在设定的延迟之间的每一点。例如,它必须在132毫秒内移动(x 14,y 30)。我遇到的问题是mouse_event跳到了那个确切的位置,所以我需要包含某种平滑方法,这样它就能平滑地移动到每个点。(运动越平稳,宏观效果越好)。目前,我正在使用这种方法来平滑每一个移动。
这是很好的工作,但它有它的局限性,例如,如果它需要移动10个像素左,平滑设置为20,它将继续跳转。
有没有人知道一种更精确的方法来平滑鼠标移动?(要求准确、流畅)
void Smoothing(int smoothing, int delay, int x, int y) {
for (int i = 0; i < smoothing; i++) {
mouse_event(1, x / smoothing, y / smoothing, 0, 0);
AccurateSleep(delay / smoothing);
}
mouse_event(1, x % smoothing, y % smoothing, 0, 0);
Sleep(delay % smoothing);
}发布于 2019-12-14 05:54:47
将这些点视为向量,并在它们之间进行插值。这通常被称为线性插值的"lerping“排序。如果搜索线性插值,您可以找到许多可能帮助您的资源。下面是an answer,它可能有助于理解它是什么。
由于我有额外的时间在我的手上,我已经打了一个例子的程序,以及它。
#include <iostream>
#include <chrono>
struct Vec2d {
double x;
double y;
Vec2d(double x, double y) : x(x), y(y) {};
};
Vec2d lerp(Vec2d const& a, Vec2d const& b, double t) {
double x((1.0 - t) * a.x + t * b.x);
double y((1.0 - t) * a.y + t * b.y);
return Vec2d(x, y);
}
int main(int argc, char* argv[]) {
Vec2d p1(10, 10);
Vec2d p2(20, 40);
double maxTime(100); //max time 100 milliseconds
double elapsedTime(0);
std::chrono::time_point<std::chrono::system_clock> start(std::chrono::system_clock::now());
std::chrono::time_point<std::chrono::system_clock> end(start);
while(elapsedTime < maxTime) {
elapsedTime += std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
start = end;
//This is where the lerping happens
double t(elapsedTime / maxTime);
Vec2d p3(lerp(p1, p2, t));
//Show what's happening.
std::cout << "p3: " << p3.x << ", " << p3.y << std::endl;
end = std::chrono::system_clock::now();
}
return 0;
}简短解释: t是从0到1的值。当t == 0.0 lerp将返回p1的“副本”时。当t == 1.0 lerp将返回p2的“副本”时。何时t == 0.5 lerp将返回(p1 + p2) / 2 (它们之间的中间点)。
您还需要添加代码来不断更新鼠标的位置。要做到这一点,您需要跟踪经过了多少时间,并根据从t到p2所需的时间和实际经过的时间来计算p2的值。上面的代码通过使用while循环和std::chrono来跟踪经过的时间来做到这一点。然而,这将取决于您打算如何触发这些“更新”。
希望这能帮上忙。
https://stackoverflow.com/questions/59332017
复制相似问题