获取sf :: View的X和Y偏移量 [英] Get X and Y offset of sf::View

查看:154
本文介绍了获取sf :: View的X和Y偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得sf :: View的X和Y偏移,我使用sf :: View作为我的2d相机,所以当我点击鼠标时我得到鼠​​标的X和Y坐标瓷砖点击

How can I get the X and Y offset of sf::View, I'm using sf::View as my 2d camera so when I click the mouse I get the mouse X and Y coords of the tile I'm clicking on

void LMButtonDown(int mX, int mY)
{
    printf("[%d][%d]\n", mX / TILE_SIZE, mY / TILE_SIZE);
}

这很棒,但一旦我移动相机 sf :: View 协调,如预期不考虑sf ::视图偏移。我没有看到在文档 $ b中获得X或Y的任何函数$ b所以我可以考虑到偏移量。

This is great, but once I move the camera sf::View the coords, as expected dont take into account the sf::View offset. I dont see any function to get X or Y in the Documentation so I can take into account for the offset. Any help with this would be appreciated.

推荐答案

查看 sf :: RenderTarget :: mapPixelToCoords和mapCoordsToPixel 。您可以使用此方法将坐标从视图空间转换回世界空间。该文档特别提到了你的需要作为一个例子:

Take a look at sf::RenderTarget::mapPixelToCoords and mapCoordsToPixel. You can use this method to convert coordinates from "view" to "world" space and back. The documentation specifically mentions your needs as an example:


此函数找到与渲染目标的给定像素匹配的2D位置。换句话说,它做的是显卡的反转,找到一个渲染像素的初始位置。

This function finds the 2D position that matches the given pixel of the render-target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel.

还有重载版本的方法,它需要一个 Vector2i 并转换它基于RenderTarget的视图,而不必提供视图

There are also overloaded versions of the methods that take a Vector2i and convert it based on the RenderTarget's view without having to supply the view manually.

如果由于某些原因,您无法使用(或无权访问)RenderTarget,那么您可以手动执行转换。它应该很简单,只要你的 sf :: View 不执行缩放或旋转(即,它只执行翻译)。

If for some reason you can't use (or don't have access to) the RenderTarget, then you can perform the translation manually. It should be quite simple as long as your sf::View does not perform scaling or rotation (that is, it only performs translation).

要获得视图的左上角,您只需取中心,然后减去宽度和高度的一半。然后,使用视图的左上角翻译您的鼠标坐标。

To get the top left corner of the view, you simply take center and then subtract half of the width and height. Then, you translate your mouse coordinates using the top left corner of the view.

类似这样:

// Somewhere else...
sf::View view;

void LMButtonDown(int mX, int mY)
{
    sf::Vector2f viewCenter = view.getCenter();
    sf::Vector2f halfExtents = view.getSize() / 2.0f;
    sf::Vector2f translation = viewCenter - halfExtents;

    mX += static_cast<int>(translation.x);
    mY += static_cast<int>(translation.y);

    printf("[%d][%d]\n", mX / TILE_SIZE, mY / TILE_SIZE);
}

这篇关于获取sf :: View的X和Y偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆