0

I am creating an FPS-like game via C# form. It is the first game I'm writing, so take it easy :p Anyway, the main idea is that the program would recognize when user's cursor hovers on a character. Now lets say that the character's coordinates are (X,Y) and I want to check if the cursor is within.. a range of 10. To check this, I can use 2 conditions of for loops, but I believe there is a better way for checking it and I just don't know it. If you misunderstood what my intention is, post here and I will try to clarify it. Thank you

4

1 回答 1

3

您不需要 for 循环。
假设鼠标坐标是(m_x,m_y),而字符坐标是(x,y)。
计算距离为 sqrt((m_x-x)*(m_x-x) + (m_y-y)*(m_y-y)) 如果距离小于 10,则鼠标在范围内。

为了获得更好的运行时间,请避免使用 sqrt,即

if (((m_x-x)*(m_x-x) + (m_y-y)*(m_y-y) <100 )
// 范围内 else
// 范围外

于 2012-12-22T21:10:43.317 回答