使对象跟随鼠标
问题描述:
接近此主题的其他问题似乎并不能帮助我理解它。我刚刚开始使用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这条线应该用来控制鼠标的位置。我试图从一个空白的项目做到这一点,而不是从一个表单。解决方案是简单地用的值MouseMove
替换const float x5=x-100
?
答
您需要替换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)))
要知道,C++允许您使用的变量名做没有数字后缀。因此,例如ellipse3可以留下EyeEllipse等,那么你不需要'/ /左眼评论。其他人试图在以后阅读你的代码会感谢你。 :) – 2012-02-14 01:22:07