You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SFML中sf::Mouse::getPosition传参错误,无法获取窗口相对鼠标坐标

Fixing sf::Mouse::getPosition() Issues in Your Game's HandleInput() Method

Hey there! Let's break down why your mouse position calls aren't working as expected and get that ship rotating correctly.

First, let's clarify how sf::Mouse::getPosition() works in SFML:

  • When called without parameters, it returns the mouse position relative to the entire screen, not your game window. That's probably why your ship isn't rotating right—your rotation logic is using the wrong coordinate system.
  • When called with a parameter, it expects a const sf::Window& (a reference to an SFML Window object). Passing a pointer (&m_window) or a custom Window wrapper class directly will cause type mismatch errors.

Let's Fix This Step by Step

1. If You're Using a Custom Window Class (Your Case, Based on the Provided Files)

Your Game class likely has a member m_window of your custom Window type, not the raw sf::Window. To use getPosition(), you need to expose the underlying SFML Window from your wrapper:

Update Window.h: Add a public method to return a reference to the internal SFML Window:

class Window
{
public:
    // ... existing methods ...
    const sf::Window& getSfWindow() const; // Add this line
private:
    sf::Window m_sfWindow; // Assuming this is your internal SFML Window member
};

Update Window.cpp: Implement the method:

const sf::Window& Window::getSfWindow() const
{
    return m_sfWindow;
}

Update Game.cpp's HandleInput(): Now you can pass the correct reference:

void Game::HandleInput()
{
    // Get mouse position RELATIVE to your game window
    sf::Vector2i mousePixelPos = sf::Mouse::getPosition(m_window.getSfWindow());
    
    // Optional: If you're using a sf::View for your game world, convert to world coordinates
    sf::Vector2f mouseWorldPos = m_window.getSfWindow().mapPixelToCoords(mousePixelPos);
    
    // Use mouseWorldPos (or mousePixelPos if no view) to calculate your ship's rotation
    // Example rotation logic (adjust to your code):
    sf::Vector2f shipPos = m_ship.getPosition();
    float angle = atan2(mouseWorldPos.y - shipPos.y, mouseWorldPos.x - shipPos.x) * 180 / M_PI;
    m_ship.setRotation(angle);
}

2. If You're Using Raw sf::Window in Game

If m_window is directly an sf::Window member in Game, the issue was passing a pointer instead of a reference. Just use:

sf::Vector2i mousePos = sf::Mouse::getPosition(m_window); // No &, pass the object directly (reference is inferred)

Why Your Previous Attempts Failed

  • getPosition(&m_window): Passes a pointer (sf::Window*), but the function expects a reference (const sf::Window&)—type mismatch, hence errors.
  • getPosition(m_window): If m_window is your custom wrapper, SFML can't convert it to sf::Window&, leading to type errors.
  • No parameters: Uses screen coordinates, which don't align with your window's game area, breaking rotation logic.

内容的提问来源于stack exchange,提问作者AliTeo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:08:25