使对象跟随鼠标

发布于 2025-01-05 09:35:15 字数 1350 浏览 0 评论 0原文

与这个主题相关的其他问题似乎并不能帮助我理解它。我刚刚开始使用 Visual Studio 和 Direct2D 进行编程,但我无法理解如何使两只“眼睛”(即椭圆内的椭圆)跟随我的鼠标。

在函数 void MainWindow::CalculateLayout() 内部,我用来

    const float radius3=radius/4;
    const float radius3_2=radius/5;
    const float x3=x-100;
    const float y3=y-150;
    ellipse3 = D2D1::Ellipse(D2D1::Point2F(x3, y3), radius3, radius3_2);
        //left eye

    const float radius4=radius/4;
    const float radius4_2=radius/5;
    const float x4=x+100;
    const float y4=y-150;
    ellipse4 = D2D1::Ellipse(D2D1::Point2F(x4, y4), radius4, radius4_2);
        //right eye

    const float radius5=radius/8;
    const float radius5_2=radius5/2;
    const float x5=x-100;
    const float y5=y-150;
    ellipse5 = D2D1::Ellipse(D2D1::Point2F(x5, y5), radius5, radius5_2);    
    // left eyeball

    const float radius6=radius/8;
    const float radius6_2=radius6/2;
    const float x6=x+100;
    const float y6=y-150;
    ellipse6 = D2D1::Ellipse(D2D1::Point2F(x6, y6), radius6, radius6_2);    
    // right eyeball

设置眼睛和眼球的位置。我认为沿着 this 应该用于控制鼠标所在的位置。我正在尝试从空白项目而不是表单中执行此操作。解决方案是简单地将 const float x5=x-100 替换为 MouseMoveX 值吗?

Other questions close to this topic don't seem to help me understand it very much. I'm just starting programming using Visual Studio and Direct2D and I'm having trouble understanding how to make two "eyes," which are ellipses inside of ellipses, follow my mouse.

Inside of the function void MainWindow::CalculateLayout() I'm using

    const float radius3=radius/4;
    const float radius3_2=radius/5;
    const float x3=x-100;
    const float y3=y-150;
    ellipse3 = D2D1::Ellipse(D2D1::Point2F(x3, y3), radius3, radius3_2);
        //left eye

    const float radius4=radius/4;
    const float radius4_2=radius/5;
    const float x4=x+100;
    const float y4=y-150;
    ellipse4 = D2D1::Ellipse(D2D1::Point2F(x4, y4), radius4, radius4_2);
        //right eye

    const float radius5=radius/8;
    const float radius5_2=radius5/2;
    const float x5=x-100;
    const float y5=y-150;
    ellipse5 = D2D1::Ellipse(D2D1::Point2F(x5, y5), radius5, radius5_2);    
    // left eyeball

    const float radius6=radius/8;
    const float radius6_2=radius6/2;
    const float x6=x+100;
    const float y6=y-150;
    ellipse6 = D2D1::Ellipse(D2D1::Point2F(x6, y6), radius6, radius6_2);    
    // right eyeball

to set up where the eyes and eyeballs are. I think that something along the line of this should be used to control where the mouse is. I am trying to do this from a blank project, not from a form. Is the solution to simply replace const float x5=x-100 with the X value of MouseMove?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月下凄凉 2025-01-12 09:35:15

您需要替换 x5 的定义,但您需要使用一个公式来完成此操作,该公式将限制它留在眼球内。

您的公式将如下所示:

// compute the angle from the eyes to the mouse
angle = arctan( (mouseY - y) / (mouseX - x) );
// x-100 and y-150 are assumed to be the origins (center) of the eyeball
// eyeballRadius should be the radius of the eyeball, or slightly smaller (so the eyes do not extend outside of it)
x5 = (x-100) + cos(angle) * eyeballRadius;
y5 = (y-150) + sin(angle) * eyeballRadius;

希望这会有所帮助。

要在光标非常接近时获得斗鸡眼效果,您应该让每个眼球计算自己的角度,例如左侧的角度为 leftAngle = arctan( (mouseY - (y-150)) / (mouseX - (x-100)) )

You need to replace the definition of x5, but you need to do it with a formula which will bound it to stay within the eyeball.

Your formula will look something like this:

// compute the angle from the eyes to the mouse
angle = arctan( (mouseY - y) / (mouseX - x) );
// x-100 and y-150 are assumed to be the origins (center) of the eyeball
// eyeballRadius should be the radius of the eyeball, or slightly smaller (so the eyes do not extend outside of it)
x5 = (x-100) + cos(angle) * eyeballRadius;
y5 = (y-150) + sin(angle) * eyeballRadius;

Hope this helps.

To get the cross-eyed effect when the cursor is very near, you should have each eyeball compute its own angle, for example the left's would be leftAngle = arctan( (mouseY - (y-150)) / (mouseX - (x-100)) )

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