如何在 DirectX 中轻松地将对象从 A 点拖动到 B 点

发布于 2024-10-08 01:46:29 字数 1233 浏览 0 评论 0原文

我必须像 Windows 中的滚动条一样将一个对象从 A 点拖动到 B 点,这里的问题是 A 和 B 的 X 或 Y 不相同,因此移动同时是水平和垂直的- 对角线。

我确实想出了一个简单的算法,该算法基于帧中的光标位置,更新要拖动的对象的位置。但为此我必须使cursorX=objectX,然后计算objectY,反之亦然。这使得水平移动或垂直移动“不那么令人沮丧”。因此,如果对角线靠近水平线,则cursorX=objectX的部分比cursorY=objectY是更好的选择,而对于靠近垂直线的对角线“cursorY=objectY”使其成为比其他部分更好的选择一。这里的问题是我必须对两种类型的对角线使用通用算法,因为我有 星形多边形 并且该对象必须能够从任何角落移动到任何角落。

这是我编写的代码,但它似乎并没有“向右”移动,因为视觉上是正确的,并且不会让用户感到沮丧,我希望它能让这个问题更有意义。我最后所做的,使用了垂直移动和水平移动两种方法并除以 2,所以每个人都很高兴,但这并没有达到目的:

//calculating with x priority
D3DXVECTOR2 resultPosX;

resultPosX.x=nextMousePos.x;

//firstCornerPos and secondCornerPos are the edges of a segment 
//on wich the object has to move
resultPosX.y=prevObjPos.y+(nextMousePos.x-prevObjPos.x)*(secondCornerPos.y-firstCornerPos.y)/(secondCornerPos.x-firstCornerPos.x);

//calculating with y priority
D3DXVECTOR2 resultPosY;

resultPosY.y=nextMousePos.y;

resultPosY.x=prevObjPos.x+(nextMousePos.y-prevObjPos.y)*(secondCornerPos.x-firstCornerPos.x)/(secondCornerPos.y-firstCornerPos.y);

//calculating the average result
resultPos.x=(resultPosX.x+resultPosY.x)/2;
resultPos.y=(resultPosX.y+resultPosY.y)/2;

I have to drag an object from point A to B like the scroll bar does it in Windows, the problem here is that the A and B does not have neither X or Y the same, so the movement is horizontal and vertical at the same time - diagonal.

I did came up with an simple algorithm that based on the cursor position in a frame, updates the position of the object to be dragged. But for that I have to make the cursorX=objectX, and than calculate the objectY, or vice-versa. This makes the movement "less frustrating" for horizontal movement or vertical, respectively. So if the diagonal is close to the horizontal line, the part with cursorX=objectX makes it a better choice than with cursorY=objectY, and for the diagonal close to a vertical line " cursorY=objectY" makes it a better choice that the other one. The problem here is that I have to use a an general algorithm for both type of diagonals cause I have star polygon and the object has to be able to move from any corner to any corner.

Here is the code I wrote, but it doesn't seems to move "right" as to be visual right and less frustrating for the user, I hope it will make more sens for the question. What I did at the end, used both methods as for vertical movement and for horizontal and devided by 2, so everybody is happy, bu that didn't do the trick:

//calculating with x priority
D3DXVECTOR2 resultPosX;

resultPosX.x=nextMousePos.x;

//firstCornerPos and secondCornerPos are the edges of a segment 
//on wich the object has to move
resultPosX.y=prevObjPos.y+(nextMousePos.x-prevObjPos.x)*(secondCornerPos.y-firstCornerPos.y)/(secondCornerPos.x-firstCornerPos.x);

//calculating with y priority
D3DXVECTOR2 resultPosY;

resultPosY.y=nextMousePos.y;

resultPosY.x=prevObjPos.x+(nextMousePos.y-prevObjPos.y)*(secondCornerPos.x-firstCornerPos.x)/(secondCornerPos.y-firstCornerPos.y);

//calculating the average result
resultPos.x=(resultPosX.x+resultPosY.x)/2;
resultPos.y=(resultPosX.y+resultPosY.y)/2;

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文