Calculating Mouse Pointer Angle

How can I get the mouse pointer's angle relative to a position on the screen?

Here's a simple example that shows how querying mouse state can be useful. We'll create an object that reports the direction from the center of the window to the mouse cursor when the space bar is pressed.

First, let's create a constructor that receives an SDL_Window*, and stores where the center of the window is:

#include <SDL.h>
#include <iostream>
#include "Window.h"

class DirectionTracker {
public:
  DirectionTracker(SDL_Window* Window) {
    int windowWidth, windowHeight;
    SDL_GetWindowSize(Window, &windowWidth,
                      &windowHeight);

    centerX = windowWidth / 2;
    centerY = windowHeight / 2;

    std::cout <<
      "Window center initialized at: ("
      << centerX << ", " << centerY << ")\n";
  }

private:
  int centerX, centerY;
};

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  DirectionTracker Tracker{
    GameWindow.SDLWindow};
  SDL_Event Event;

  while (true) {
    while (SDL_PollEvent(&Event)) {
      // ...
    }
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Window center initialized at: (350, 150)

Next, when our event loop detects the user pressing the space bar, we'll notify the DirectionTracker:

#include <SDL.h>
#include <iostream>
#include "Window.h"

class DirectionTracker {
public:
DirectionTracker(SDL_Window*){/*...*/} void KeyboardEvent(SDL_KeyboardEvent& e) { if (e.type == SDL_KEYDOWN && e.keysym.sym == SDLK_SPACE) { ReportAngle(); } } private: void ReportAngle() { std::cout << "TODO: Report Angle"; } int centerX, centerY; }; int main(int argc, char** argv) { SDL_Init(SDL_INIT_VIDEO); Window GameWindow; DirectionTracker Tracker{ GameWindow.SDLWindow}; SDL_Event Event; while (true) { while (SDL_PollEvent(&Event)) { if (Event.type == SDL_KEYDOWN) { Tracker.KeyboardEvent(Event.key); } } GameWindow.Update(); } SDL_Quit(); return 0; }
Window center initialized at: (350, 150)
TODO: Report Angle

The final step is to complete the ReportAngle() function. We query the mouse position using SDL_GetMouseState(), and implement the required calculations to determine its direction from the center of the screen:

#include <SDL.h>
#include <iostream>
#include "Window.h"

class DirectionTracker {
public:
DirectionTracker(SDL_Window*){/*...*/}
void KeyboardEvent(SDL_KeyboardEvent&){/*...*/} private: void ReportAngle() { // Get current mouse position int mouseX, mouseY; SDL_GetMouseState(&mouseX, &mouseY); // Calculate direction from center to mouse int dirX = mouseX - centerX; int dirY = mouseY - centerY; // Calculate angle in degrees double angle{ atan2(dirY, dirX) * 180.0 / M_PI}; std::cout << "\nDirection from center: " << dirX << ", " << dirY << " (angle: " << angle << " degrees)"; } int centerX, centerY; };
int main(int argc, char** argv){/*...*/}
Window center initialized at: (350, 150)
Direction from center: -228, -77 (angle: -161.339 degrees)
Direction from center: 248, 113 (angle: 24.4962 degrees)

Mouse State

Learn how to monitor mouse position and button states in real-time using SDL's state query functions

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Calculating Mouse Speed
Can I get the mouse speed or how fast it's moving?
Click and Drag Detection
How do I detect if the user is clicking and dragging?
Events vs State for Mouse Input
What's the difference between using events and using state checking for mouse position?
Smooth Mouse Following
How can I make an object follow the mouse cursor smoothly?
Click-and-Drag Selection
How do I implement a click-and-drag selection rectangle?
Cursor Trail Effect
How do I implement a cursor trail effect?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant