Mouse State

Calculating Mouse Pointer Angle

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

Abstract art representing computer programming

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)

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved