【问题标题】:Make Objects Follow Mouse使对象跟随鼠标
【发布时间】:2012-02-14 08:43:07
【问题描述】:

与此主题相关的其他问题似乎并不能帮助我很好地理解它。我刚刚开始使用 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 替换为XMouseMove 值?

【问题讨论】:

  • 您知道,C++ 允许您使用没有数字后缀的变量名。因此,例如 ellipse3 可以是 leftEyeEllipse 等。那么您不需要 //left eye 评论。稍后尝试阅读您的代码的其他人会感谢您。 :)

标签: c++ visual-studio-2010 direct2d


【解决方案1】:

您需要替换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)) )

【讨论】:

  • 如何让mouseYmouseX 包含鼠标指针?
  • 我不知道。我以为你知道这一点,因为你提到了一些关于 MouseMove 的事情。
猜你喜欢
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
相关资源
最近更新 更多