Player12
An open source football management simulator
Loading...
Searching...
No Matches
match_render_math.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <array>
12#include <cmath>
13#include <cstddef>
14#include <cstdint>
15#include <numbers>
16#include <span>
17
18#include "global/types.h"
19#include "model/match_tuning.h"
20
29namespace RenderMath
30{
31struct Vec3
32{
33 float x = 0.0f;
34 float y = 0.0f;
35 float z = 0.0f;
36
37 friend constexpr Vec3 operator+(Vec3 a, Vec3 b)
38 {
39 return {a.x + b.x, a.y + b.y, a.z + b.z};
40 }
41 friend constexpr Vec3 operator-(Vec3 a, Vec3 b)
42 {
43 return {a.x - b.x, a.y - b.y, a.z - b.z};
44 }
45 friend constexpr Vec3 operator*(Vec3 a, float s)
46 {
47 return {a.x * s, a.y * s, a.z * s};
48 }
49};
50
51struct Vec4
52{
53 float x = 0.0f;
54 float y = 0.0f;
55 float z = 0.0f;
56 float w = 0.0f;
57};
58
59constexpr float dot(Vec3 a, Vec3 b)
60{
61 return a.x * b.x + a.y * b.y + a.z * b.z;
62}
63constexpr Vec3 cross(Vec3 a, Vec3 b)
64{
65 return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
66}
67inline float length(Vec3 a) { return std::sqrt(dot(a, a)); }
69{
70 const float magnitude = length(a);
71 return magnitude > 1e-6f ? a * (1.0f / magnitude) : Vec3{};
72}
73constexpr Vec3 lerp(Vec3 a, Vec3 b, float t) { return a + (b - a) * t; }
74
76struct Mat4
77{
78 std::array<float, 16> m{};
79
80 static constexpr Mat4 identity()
81 {
82 Mat4 result;
83 result.m[0] = 1.0f;
84 result.m[5] = 1.0f;
85 result.m[10] = 1.0f;
86 result.m[15] = 1.0f;
87 return result;
88 }
89
90 constexpr Vec4 transform(Vec3 p) const
91 {
92 return {m[0] * p.x + m[1] * p.y + m[2] * p.z + m[3],
93 m[4] * p.x + m[5] * p.y + m[6] * p.z + m[7],
94 m[8] * p.x + m[9] * p.y + m[10] * p.z + m[11],
95 m[12] * p.x + m[13] * p.y + m[14] * p.z + m[15]};
96 }
97
98 friend constexpr Mat4 operator*(const Mat4& a, const Mat4& b)
99 {
100 Mat4 result;
101 for (std::size_t row = 0; row < 4; ++row)
102 for (std::size_t column = 0; column < 4; ++column)
103 {
104 float sum = 0.0f;
105 for (std::size_t k = 0; k < 4; ++k)
106 sum += a.m[row * 4 + k] * b.m[k * 4 + column];
107 result.m[row * 4 + column] = sum;
108 }
109 return result;
110 }
111};
112
114inline Mat4 lookAt(Vec3 eye, Vec3 target, Vec3 up)
115{
116 const Vec3 forward = normalize(target - eye);
117 const Vec3 side = normalize(cross(forward, up));
118 const Vec3 cameraUp = cross(side, forward);
119 Mat4 view = Mat4::identity();
120 view.m = {side.x, side.y, side.z, -dot(side, eye),
121 cameraUp.x, cameraUp.y, cameraUp.z, -dot(cameraUp, eye),
122 -forward.x, -forward.y, -forward.z, dot(forward, eye),
123 0.0f, 0.0f, 0.0f, 1.0f};
124 return view;
125}
126
128inline Mat4 perspective(float verticalFovRadians, float aspect, float nearPlane,
129 float farPlane)
130{
131 const float focal = 1.0f / std::tan(verticalFovRadians * 0.5f);
132 Mat4 projection;
133 projection.m[0] = focal / aspect;
134 projection.m[5] = focal;
135 projection.m[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
136 projection.m[11] = 2.0f * farPlane * nearPlane / (nearPlane - farPlane);
137 projection.m[14] = -1.0f;
138 return projection;
139}
140
143{
144 float x = 0.0f;
145 float y = 0.0f;
146 float width = 1.0f;
147 float height = 1.0f;
148};
149
152{
153 float x = 0.0f;
154 float y = 0.0f;
155 float depth = 0.0f;
156};
157
160{
163 Vec3 forward{0.0f, 1.0f, 0.0f};
165 Vec3 side{1.0f, 0.0f, 0.0f};
166 Vec3 up{0.0f, 0.0f, 1.0f};
168 float nearPlane = 0.5f;
170 float focalPixels = 1.0f;
171
172 static Projection make(Vec3 eye, Vec3 target, float verticalFovRadians,
173 float nearPlane, float farPlane, ScreenRect rect)
174 {
175 Projection projection;
176 projection.eye = eye;
177 projection.forward = normalize(target - eye);
178 projection.side = normalize(cross(projection.forward, {0.0f, 0.0f, 1.0f}));
179 projection.up = cross(projection.side, projection.forward);
180 projection.rect = rect;
181 projection.nearPlane = nearPlane;
182 const float aspect = rect.width / (rect.height > 0.0f ? rect.height : 1.0f);
183 projection.viewProjection =
184 perspective(verticalFovRadians, aspect, nearPlane, farPlane) *
185 lookAt(eye, target, {0.0f, 0.0f, 1.0f});
186 projection.focalPixels =
187 rect.height * 0.5f / std::tan(verticalFovRadians * 0.5f);
188 return projection;
189 }
190
191 Vec4 toClip(Vec3 point) const { return viewProjection.transform(point); }
192
195 {
196 const float inverseW = 1.0f / clip.w;
197 return {rect.x + (clip.x * inverseW * 0.5f + 0.5f) * rect.width,
198 rect.y + (0.5f - clip.y * inverseW * 0.5f) * rect.height, clip.w};
199 }
200
202 bool project(Vec3 point, ScreenPoint& out) const
203 {
204 const Vec4 clip = toClip(point);
205 if (clip.w < nearPlane) return false;
206 out = clipToScreen(clip);
207 return true;
208 }
209
210 float depth(Vec3 point) const { return dot(point - eye, forward); }
211
213 Vec3 rayThrough(float pixelX, float pixelY) const
214 {
215 const float right = (pixelX - (rect.x + rect.width * 0.5f)) / focalPixels;
216 const float upward = ((rect.y + rect.height * 0.5f) - pixelY) / focalPixels;
217 return forward + side * right + up * upward;
218 }
219
224 bool groundPointAt(float pixelX, float pixelY, Vec3& out,
225 float planeHeight = 0.0f) const
226 {
227 const Vec3 direction = rayThrough(pixelX, pixelY);
228 if (direction.z > -1e-4f) return false;
229 const float distance = (planeHeight - eye.z) / direction.z;
230 if (distance <= 0.0f) return false;
231 out = eye + direction * distance;
232 return true;
233 }
234};
235
243template <typename Vertex, typename WOf, typename Interpolate>
244std::size_t clipConvexToNearPlane(std::span<const Vertex> input,
245 std::span<Vertex> output, float nearPlane,
246 WOf wOf, Interpolate interpolate)
247{
248 std::size_t written = 0;
249 const std::size_t count = input.size();
250 for (std::size_t index = 0; index < count; ++index)
251 {
252 const Vertex& current = input[index];
253 const Vertex& next = input[(index + 1) % count];
254 const float currentDistance = wOf(current) - nearPlane;
255 const float nextDistance = wOf(next) - nearPlane;
256 if (currentDistance >= 0.0f && written < output.size())
257 output[written++] = current;
258 if ((currentDistance >= 0.0f) != (nextDistance >= 0.0f) &&
259 written < output.size())
260 {
261 output[written++] = interpolate(
262 current, next, currentDistance / (currentDistance - nextDistance));
263 }
264 }
265 return written;
266}
267
269constexpr Vec4 lerp(Vec4 a, Vec4 b, float t)
270{
271 return {a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t,
272 a.w + (b.w - a.w) * t};
273}
274
276inline std::size_t clipPolygonToNearPlane(std::span<const Vec4> input,
277 std::span<Vec4> output,
278 float nearPlane)
279{
281 input, output, nearPlane, [](const Vec4& vertex) { return vertex.w; },
282 [](const Vec4& a, const Vec4& b, float t) { return lerp(a, b, t); });
283}
284
286inline float wrapAngle(float angle)
287{
288 constexpr float TWO_PI = 2.0f * std::numbers::pi_v<float>;
289 angle = std::fmod(angle + std::numbers::pi_v<float>, TWO_PI);
290 if (angle < 0.0f) angle += TWO_PI;
291 return angle - std::numbers::pi_v<float>;
292}
293
295inline float lerpAngle(float from, float to, float t)
296{
297 return from + wrapAngle(to - from) * t;
298}
299
301inline float dampingFactor(float ratePerSecond, float deltaSeconds)
302{
303 return 1.0f - std::exp(-ratePerSecond * deltaSeconds);
304}
305
314{
315 public:
316 int take(PlayerRole role, bool starter)
317 {
318 if (starter)
319 {
320 for (const int number : preferred(role))
321 if (number > 0 && claim(number)) return number;
322 for (int number = 1; number <= 11; ++number)
323 if (claim(number)) return number;
324 }
325 for (int number = 12; number < MAX_NUMBER; ++number)
326 if (claim(number)) return number;
327 return 0;
328 }
329
330 private:
331 static constexpr int MAX_NUMBER = 64;
332 std::uint64_t used = 0;
333
334 bool claim(int number)
335 {
336 const std::uint64_t bit = std::uint64_t{1} << number;
337 if ((used & bit) != 0U) return false;
338 used |= bit;
339 return true;
340 }
341
342 static std::array<int, 3> preferred(PlayerRole role)
343 {
344 using enum PlayerRole;
345 switch (role)
346 {
347 case GK:
348 return {1, 0, 0};
349 case RB:
350 return {2, 3, 0};
351 case LB:
352 return {3, 2, 0};
353 case CB:
354 return {4, 5, 6};
355 case CDM:
356 return {6, 4, 8};
357 case CM:
358 return {8, 6, 10};
359 case CAM:
360 return {10, 8, 7};
361 case RM:
362 case RW:
363 return {7, 11, 0};
364 case LM:
365 case LW:
366 return {11, 7, 0};
367 case ST:
368 return {9, 10, 11};
369 case UNKNOWN:
370 break;
371 }
372 return {0, 0, 0};
373 }
374};
375
377constexpr Vec3 worldFromPitch(Vector2F normalized, float heightMetres = 0.0f)
378{
379 return {normalized.x * MatchTuning::Pitch::LENGTH_METRES,
380 (1.0f - normalized.y) * MatchTuning::Pitch::WIDTH_METRES,
381 heightMetres};
382}
383
389inline float worldYawFromFacing(float facingAngle)
390{
391 return std::atan2(-std::sin(facingAngle) * MatchTuning::Pitch::WIDTH_METRES,
392 std::cos(facingAngle) * MatchTuning::Pitch::LENGTH_METRES);
393}
394} // namespace RenderMath
Definition match_render_math.h:314
std::uint64_t used
Definition match_render_math.h:332
static std::array< int, 3 > preferred(PlayerRole role)
Definition match_render_math.h:342
static constexpr int MAX_NUMBER
Definition match_render_math.h:331
int take(PlayerRole role, bool starter)
Definition match_render_math.h:316
bool claim(int number)
Definition match_render_math.h:334
Side side
Definition match_stadium_3d.cpp:389
Definition match_render_math.h:30
float worldYawFromFacing(float facingAngle)
Definition match_render_math.h:389
float dampingFactor(float ratePerSecond, float deltaSeconds)
Definition match_render_math.h:301
float lerpAngle(float from, float to, float t)
Definition match_render_math.h:295
constexpr Vec3 lerp(Vec3 a, Vec3 b, float t)
Definition match_render_math.h:73
constexpr float dot(Vec3 a, Vec3 b)
Definition match_render_math.h:59
float wrapAngle(float angle)
Definition match_render_math.h:286
Vec3 normalize(Vec3 a)
Definition match_render_math.h:68
Mat4 lookAt(Vec3 eye, Vec3 target, Vec3 up)
Definition match_render_math.h:114
std::size_t clipConvexToNearPlane(std::span< const Vertex > input, std::span< Vertex > output, float nearPlane, WOf wOf, Interpolate interpolate)
Definition match_render_math.h:244
Mat4 perspective(float verticalFovRadians, float aspect, float nearPlane, float farPlane)
Definition match_render_math.h:128
std::size_t clipPolygonToNearPlane(std::span< const Vec4 > input, std::span< Vec4 > output, float nearPlane)
Definition match_render_math.h:276
constexpr Vec3 cross(Vec3 a, Vec3 b)
Definition match_render_math.h:63
constexpr Vec3 worldFromPitch(Vector2F normalized, float heightMetres=0.0f)
Definition match_render_math.h:377
int index
Definition runtime_editor.cpp:94
std::string_view column
Definition runtime_editor_validation.cpp:45
static constexpr float WIDTH_METRES
Definition match_tuning.h:36
static constexpr float LENGTH_METRES
Definition match_tuning.h:35
Definition match_render_math.h:77
std::array< float, 16 > m
Definition match_render_math.h:78
static constexpr Mat4 identity()
Definition match_render_math.h:80
constexpr Vec4 transform(Vec3 p) const
Definition match_render_math.h:90
friend constexpr Mat4 operator*(const Mat4 &a, const Mat4 &b)
Definition match_render_math.h:98
Definition match_render_math.h:160
static Projection make(Vec3 eye, Vec3 target, float verticalFovRadians, float nearPlane, float farPlane, ScreenRect rect)
Definition match_render_math.h:172
Vec4 toClip(Vec3 point) const
Definition match_render_math.h:191
Mat4 viewProjection
Definition match_render_math.h:161
Vec3 rayThrough(float pixelX, float pixelY) const
Definition match_render_math.h:213
Vec3 up
Definition match_render_math.h:166
float depth(Vec3 point) const
Definition match_render_math.h:210
ScreenRect rect
Definition match_render_math.h:167
bool groundPointAt(float pixelX, float pixelY, Vec3 &out, float planeHeight=0.0f) const
Definition match_render_math.h:224
float nearPlane
Definition match_render_math.h:168
float focalPixels
Definition match_render_math.h:170
Vec3 forward
Definition match_render_math.h:163
bool project(Vec3 point, ScreenPoint &out) const
Definition match_render_math.h:202
Vec3 side
Definition match_render_math.h:165
Vec3 eye
Definition match_render_math.h:162
ScreenPoint clipToScreen(Vec4 clip) const
Definition match_render_math.h:194
Definition match_render_math.h:152
float depth
Definition match_render_math.h:155
float y
Definition match_render_math.h:154
float x
Definition match_render_math.h:153
Definition match_render_math.h:143
float y
Definition match_render_math.h:145
float width
Definition match_render_math.h:146
float x
Definition match_render_math.h:144
float height
Definition match_render_math.h:147
Definition match_render_math.h:32
friend constexpr Vec3 operator-(Vec3 a, Vec3 b)
Definition match_render_math.h:41
float x
Definition match_render_math.h:33
friend constexpr Vec3 operator+(Vec3 a, Vec3 b)
Definition match_render_math.h:37
friend constexpr Vec3 operator*(Vec3 a, float s)
Definition match_render_math.h:45
float z
Definition match_render_math.h:35
float y
Definition match_render_math.h:34
Definition match_render_math.h:52
float w
Definition match_render_math.h:56
float y
Definition match_render_math.h:54
float z
Definition match_render_math.h:55
float x
Definition match_render_math.h:53
A simple 2D float vector for pitch coordinates.
Definition types.h:84
float y
Definition types.h:86
float x
Definition types.h:85
float length
Definition synth.cpp:49
PlayerRole
Represents a specific tactical role on the pitch.
Definition types.h:63