帮助了解鼠标算法中的对象
我不知道该怎么称呼这个。我正在构建一个基于图块的游戏,用户可以在其中单击图块。 它是一个 2d c++ 瓷砖矢量。现在我有一个算法可以像这样定位它们:
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
它们根据它们的大小进行定位,我的图块类保存它们的每个 x 和 y 坐标,并且我有 2 个用于高度和宽度的变量。
基本上我想找到一种算法,可以根据鼠标的位置返回鼠标所在的图块。
例如:如果我的鼠标(相对于游戏窗口)的位置是 37,0 并且每个图块的大小为 10 * 10 并且它们每个图块的宽度分开,那么当我的鼠标超过 37,0 时,它应该返回( 4,1) 表示 x 上的第四个元素和 y 上的第一个元素。我考虑过对每个矩形进行边界框检查,但我认为如果我每秒检查 60 次,将会消耗太多的 cpu。
谢谢
I wasn't sure what to call this. I'm building a tile based game where the user can click on a tile.
it is a 2d c++ vector of tiles. right now I have an algorithm that positions them like this:
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
[][][][][][][][][][][]
they are positioned based on their size and my tile class holds each of their x and y coordinates, and I have 2 variables for the height and width.
Basically I'd like to find an algorithm that can return the tile my mouse is on based on the mouse's position.
For example: if my mouse (relative to the game window)'s position is 37,0 and each of my tiles is 10 * 10 in size and they are each tilewidth apart then by having my mouse over 37,0 it should return (4,1) meaning the 4th element on the x and the first element on the y. I thought about doing bounding box checks against each rectangle but I think that will use up too much cpu if i check 60 times / second.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据评论,据称 Allegro 没有鼠标事件。不正确。请参阅鼠标例程。
或者,您可以使用
get_mouse_mickeys()
以加快您的检查速度。因此,如果您每秒检查多次,并且此函数为 X 和 Y 分别返回 0,则无需重新检查鼠标所指向的内容,因为它尚未移动。
话虽这么说,使用鼠标回调。这是最有效的方法。
至于如何处理您指出的图块,我会这样处理:在某个时刻您正在渲染像素。要么您正在使用帧缓冲区,要么您应该能够创建一个帧缓冲区。对于每个像素,如果要渲染图块,请在像素映射中存储指向该图块的指针。这意味着每个像素 4 个字节(假设 32 位指针),但在 1280x1024 下,您使用略多于 1 MB 的 RAM 来存储每个 (x,y) 坐标处的图块,并且您的查找速度将非常快。
According to the comments, it's claimed Allegro doesn't have mouse events. Not true. See Mouse routines.
Alternatively you can use
get_mouse_mickeys()
to speed up your checking.So if you check lots of times a second and this function returns 0 for each of X and Y there's no need to recheck what the mouse is pointing at because it hasn't moved.
That being said, use the mouse callback. It's the most efficient way.
As for how to handle what tile you're pointing out, I would handle it this way: at some point you're rendering the pixels. Either you're using a frame buffer or you should be able to create one. For each pixel, if you're rendering a tile, store a pointer to that tile in a pixel map. That means 4 bytes (assuming 32 bit pointers) per pixel but at, say, 1280x1024, you're using a little over one megabyte of RAM to store what tile is at each (x,y) coordinate and your lookups will be incredibly fast.
如果您不使用基于事件的环境,则确实需要编写自己的对象交叉、鼠标检测等。
以下是您想要获得的伪代码公式:
If you're not using an event based environment, you do need to code your own object intersection, mouse detection, etc.
Here's the formula in pseudo code for what you want to get: