r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
HitboxUtils.hpp
Go to the documentation of this file.
1///
2/// @file HitboxUtils.hpp
3/// @brief Utility functions for hitbox calculations
4/// @namespace utl
5///
6
7#pragma once
8
9#include <utility>
10
11namespace utl
12{
13 ///
14 /// @brief Calculate the center position of a sprite
15 /// @param x Sprite X position
16 /// @param y Sprite Y position
17 /// @param spriteWidth Sprite width
18 /// @param spriteHeight Sprite height
19 /// @param scale Sprite scale
20 /// @return Pair of (centerX, centerY)
21 ///
22 inline std::pair<float, float> calculateSpriteCenter(const float x, const float y, const float spriteWidth,
23 const float spriteHeight, const float scale)
24 {
25 return {x + (spriteWidth * scale) / 2.0f, y + (spriteHeight * scale) / 2.0f};
26 }
27
28 ///
29 /// @brief Calculate hitbox offsets to center the hitbox on the sprite
30 /// @param spriteWidth Sprite width
31 /// @param spriteHeight Sprite height
32 /// @param scale Sprite scale
33 /// @return Pair of (offsetX, offsetY)
34 ///
35 inline std::pair<float, float> calculateHitboxOffsets(const float spriteWidth, const float spriteHeight,
36 const float scale)
37 {
38 return {(spriteWidth * scale) / 2.0f, (spriteHeight * scale) / 2.0f};
39 }
40
41 ///
42 /// @brief Calculate hitbox offsets relative to sprite position
43 /// @param x Sprite X position
44 /// @param y Sprite Y position
45 /// @param spriteWidth Sprite width
46 /// @param spriteHeight Sprite height
47 /// @param scale Sprite scale
48 /// @return Pair of (offsetX, offsetY) relative to sprite position
49 ///
50 inline std::pair<float, float> calculateHitboxOffsetsRelative(const float x, const float y, const float spriteWidth,
51 const float spriteHeight, const float scale)
52 {
53 auto [centerX, centerY] = calculateSpriteCenter(x, y, spriteWidth, spriteHeight, scale);
54 return {centerX - x, centerY - y};
55 }
56} // namespace utl
std::pair< float, float > calculateHitboxOffsets(const float spriteWidth, const float spriteHeight, const float scale)
Calculate hitbox offsets to center the hitbox on the sprite.
std::pair< float, float > calculateHitboxOffsetsRelative(const float x, const float y, const float spriteWidth, const float spriteHeight, const float scale)
Calculate hitbox offsets relative to sprite position.
std::pair< float, float > calculateSpriteCenter(const float x, const float y, const float spriteWidth, const float spriteHeight, const float scale)
Calculate the center position of a sprite.