Player12
An open source football management simulator
Loading...
Searching...
No Matches
match_player_rig.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 <algorithm>
12#include <cmath>
13#include <cstdint>
14#include <numbers>
15
17
25namespace PlayerRig
26{
28
29inline constexpr Vec3 UP{0.0f, 0.0f, 1.0f};
30inline constexpr float PI = std::numbers::pi_v<float>;
31inline constexpr float TWO_PI = 2.0f * PI;
32
33inline float smoothStep(float t)
34{
35 t = std::clamp(t, 0.0f, 1.0f);
36 return t * t * (3.0f - 2.0f * t);
37}
38
39inline float flatDistance(Vec3 a, Vec3 b)
40{
41 return std::hypot(a.x - b.x, a.y - b.y);
42}
43
44// --- two-bone IK -------------------------------------------------------------
45
46struct TwoBone
47{
50 bool reached = false;
51};
52
58inline TwoBone solveTwoBone(Vec3 root, Vec3 target, float upper, float lower,
59 Vec3 pole)
60{
61 const Vec3 toTarget = target - root;
62 float distance = RenderMath::length(toTarget);
63 const Vec3 direction =
64 distance > 1e-5f ? toTarget * (1.0f / distance) : Vec3{0.0f, 0.0f, -1.0f};
65 const float minimum = std::abs(upper - lower) + 1e-3f;
66 const float maximum = (upper + lower) * 0.9995f;
67 const bool reached = distance >= minimum && distance <= maximum;
68 distance = std::clamp(distance, minimum, maximum);
69 const float along =
70 (upper * upper - lower * lower + distance * distance) / (2.0f * distance);
71 const float height = std::sqrt(std::max(upper * upper - along * along, 0.0f));
72 Vec3 bend = pole - direction * RenderMath::dot(pole, direction);
73 if (const float bendLength = RenderMath::length(bend); bendLength < 1e-4f)
74 {
75 // Pole along the limb: any perpendicular will do.
76 bend = RenderMath::cross(
77 direction, std::abs(direction.z) < 0.9f ? UP : Vec3{1.0f, 0.0f, 0.0f});
78 bend = RenderMath::normalize(bend);
79 }
80 else
81 {
82 bend = bend * (1.0f / bendLength);
83 }
84 return {root + direction * along + bend * height, root + direction * distance,
85 reached};
86}
87
94inline float rollOntoToes(Vec3& ankle, Vec3 hip, Vec3 footForward,
95 float toeLength, float reach, float maxAngle)
96{
97 if (RenderMath::length(ankle - hip) <= reach) return 0.0f;
98 const Vec3 toe = ankle + footForward * toeLength;
99 const auto at = [&](float angle)
100 {
101 return toe - footForward * (toeLength * std::cos(angle)) +
102 UP * (toeLength * std::sin(angle));
103 };
104 float low = 0.0f;
105 float high = maxAngle;
106 if (RenderMath::length(at(high) - hip) > reach)
107 {
108 ankle = at(high);
109 return high;
110 }
111 for (int step = 0; step < 8; ++step)
112 {
113 const float middle = (low + high) * 0.5f;
114 if (RenderMath::length(at(middle) - hip) > reach)
115 low = middle;
116 else
117 high = middle;
118 }
119 ankle = at(high);
120 return high;
121}
122
123// --- foot planting -----------------------------------------------------------
124
127{
135 float stepSeconds = 0.0f;
137 float yaw = 0.0f;
138 bool inStance = false;
139 bool valid = false;
140};
141
144{
150 float yaw = 0.0f;
152 float legPhase = 0.0f;
154 float duty = 0.5f;
156 float stanceLength = 0.0f;
158 float frontReach = 0.3f;
159 float liftHeight = 0.12f;
162 float maxDrift = 0.5f;
164 bool idle = false;
166 float idleStepMetres = 0.25f;
167 float stepDuration = 0.22f;
169 bool otherStepping = false;
171 float deltaSeconds = 0.0f;
172};
173
175inline Vec3 stanceSpot(const StrideInput& input, float share)
176{
177 const float front = std::min(input.stanceLength * 0.5f, input.frontReach);
178 return input.rest + input.forward * (front - share * input.stanceLength);
179}
180
188inline Vec3 stepFoot(FootState& foot, const StrideInput& input)
189{
190 if (!foot.valid)
191 {
192 // Standing: both feet down under the hips. Moving: the gait below puts
193 // the foot down on its exact spot (or swings it from under the body).
194 foot = FootState{};
195 const Vec3 initialPosition =
196 input.idle ? input.rest : stanceSpot(input, 0.5f);
197 foot.liftOff = initialPosition;
198 foot.planted = initialPosition;
199 foot.position = initialPosition;
200 foot.yaw = input.yaw;
201 foot.inStance = input.idle;
202 foot.valid = true;
203 }
204 if (input.idle)
205 {
206 if (foot.stepSeconds > 0.0f)
207 {
208 foot.stepSeconds = std::max(0.0f, foot.stepSeconds - input.deltaSeconds);
209 const float share = 1.0f - foot.stepSeconds / input.stepDuration;
210 foot.position =
211 RenderMath::lerp(foot.liftOff, foot.stepTarget, smoothStep(share)) +
212 UP * (input.liftHeight * 0.5f * std::sin(PI * share));
213 if (foot.stepSeconds <= 0.0f)
214 {
215 foot.planted = foot.stepTarget;
216 foot.position = foot.stepTarget;
217 foot.yaw = input.yaw;
218 foot.inStance = true;
219 }
220 return foot.position;
221 }
222 if (const bool drifted =
223 flatDistance(foot.planted, input.rest) > input.idleStepMetres;
224 (!foot.inStance || drifted) && !input.otherStepping)
225 {
226 // Step back under the hip (a foot caught mid-swing lands there too).
227 foot.liftOff = foot.position;
228 foot.stepTarget = input.rest;
229 foot.stepSeconds = input.stepDuration;
230 foot.inStance = false;
231 return foot.position;
232 }
233 if (foot.inStance) foot.position = foot.planted;
234 return foot.position;
235 }
236
237 if (foot.stepSeconds > 0.0f)
238 {
239 // Started to move during an idle step: carry on as a swing.
240 foot.stepSeconds = 0.0f;
241 foot.inStance = false;
242 foot.liftOff = foot.position;
243 }
244 const float share = std::clamp(input.legPhase / TWO_PI, 0.0f, 1.0f);
245 const bool stance = share < input.duty;
246 if (stance)
247 {
248 if (const Vec3 ideal =
249 stanceSpot(input, share / std::max(input.duty, 1e-3f));
250 !foot.inStance || flatDistance(foot.planted, ideal) > input.maxDrift)
251 {
252 foot.planted = ideal;
253 foot.yaw = input.yaw;
254 }
255 foot.position = foot.planted;
256 }
257 else
258 {
259 if (foot.inStance) foot.liftOff = foot.planted;
260 const float swing =
261 (share - input.duty) / std::max(1.0f - input.duty, 1e-3f);
262 const Vec3 landing = stanceSpot(input, 0.0f);
263 foot.position = RenderMath::lerp(foot.liftOff, landing, smoothStep(swing)) +
264 UP * (input.liftHeight * std::sin(PI * swing));
265 foot.yaw = input.yaw;
266 }
267 foot.inStance = stance;
268 return foot.position;
269}
270
271// --- actions -----------------------------------------------------------------
272
274enum class Action : std::uint8_t
275{
276 IDLE,
277 WALK,
278 JOG,
279 RUN,
280 SPRINT,
281 TURN,
282 PASS,
283 SHOT,
284 CROSS,
285 HEADER,
286 TACKLE,
287 SLIDE,
291 THROW_IN,
292 CELEBRATE,
293 DEJECTED,
294};
295
297enum class Event : std::uint8_t
298{
299 NONE,
300 PASS,
301 SHOT,
302 CROSS,
303 HEADER,
304 TACKLE,
305 SLIDE,
306 THROW,
307};
308
310enum class Mood : std::uint8_t
311{
312 NONE,
313 CELEBRATE,
314 DEJECTED,
315};
316
319{
320 float idle = 0.35f;
321 float walk = 2.2f;
322 float jog = 4.6f;
323 float run = 6.6f;
325 float turnRate = 3.0f;
326 float turnSpeed = 3.5f;
327};
328
331{
332 float speed = 0.0f;
333 float turnRate = 0.0f;
335 bool goalkeeper = false;
336 bool diving = false;
338 bool holding = false;
340 bool keeperSet = false;
342 bool throwIn = false;
344};
345
346inline Action locomotionFor(float speed, const GaitBands& bands = {})
347{
348 using enum Action;
349 if (speed < bands.idle) return IDLE;
350 if (speed < bands.walk) return WALK;
351 if (speed < bands.jog) return JOG;
352 if (speed < bands.run) return RUN;
353 return SPRINT;
354}
355
361inline Action selectAction(const ActionInput& input,
362 const GaitBands& bands = {})
363{
364 if (input.goalkeeper && input.diving) return Action::KEEPER_DIVE;
365 switch (input.event)
366 {
367 case Event::SLIDE:
368 return Action::SLIDE;
369 case Event::TACKLE:
370 return Action::TACKLE;
371 case Event::HEADER:
372 return Action::HEADER;
373 case Event::SHOT:
374 return Action::SHOT;
375 case Event::CROSS:
376 return Action::CROSS;
377 case Event::PASS:
378 return Action::PASS;
379 case Event::THROW:
380 return Action::THROW_IN;
381 case Event::NONE:
382 break;
383 }
384 if (input.throwIn) return Action::THROW_IN;
385 if (input.goalkeeper && input.holding) return Action::KEEPER_HOLD;
386 if (input.mood == Mood::CELEBRATE) return Action::CELEBRATE;
387 if (input.mood == Mood::DEJECTED) return Action::DEJECTED;
388 if (input.goalkeeper && input.keeperSet && input.speed < bands.jog)
389 return Action::KEEPER_SET;
390 if (std::abs(input.turnRate) >= bands.turnRate && input.speed >= bands.idle &&
391 input.speed < bands.turnSpeed)
392 return Action::TURN;
393 return locomotionFor(input.speed, bands);
394}
395
397inline float dutyFactor(float speed)
398{
399 // Walking keeps a foot down most of the time; running has flight phases.
400 constexpr float WALK_DUTY = 0.62f;
401 constexpr float JOG_DUTY = 0.4f;
402 constexpr float SPRINT_DUTY = 0.28f;
403 if (speed <= 1.5f) return WALK_DUTY;
404 if (speed <= 4.0f)
405 return WALK_DUTY + (JOG_DUTY - WALK_DUTY) * (speed - 1.5f) / 2.5f;
406 return std::max(SPRINT_DUTY,
407 JOG_DUTY + (SPRINT_DUTY - JOG_DUTY) * (speed - 4.0f) / 3.5f);
408}
409
410// --- timed ball actions ------------------------------------------------------
411
413struct Swing
414{
415 float forward = 0.0f;
416 float across = 0.0f;
417 float up = 0.0f;
418};
419
421{
422 using enum Event;
423 switch (event)
424 {
425 case SHOT:
426 return {0.55f, 0.05f, 0.7f};
427 case CROSS:
428 return {0.4f, 0.32f, 0.6f};
429 case PASS:
430 default:
431 return {0.38f, 0.08f, 0.22f};
432 }
433}
434
437{
438 float contactHold = 0.05f;
439 float followThrough = 0.26f;
440 float recover = 0.26f;
441 float total() const { return contactHold + followThrough + recover; }
442};
443
450inline Vec3 kickAnkle(Event event, float seconds, Vec3 contact, Vec3 direction,
451 Vec3 across, const KickTiming& timing, float& weight)
452{
453 weight = 0.0f;
454 if (seconds < 0.0f || seconds >= timing.total()) return contact;
455 const Swing swing = followThroughFor(event);
456 const Vec3 finish = contact + direction * swing.forward +
457 across * swing.across + UP * swing.up;
458 if (seconds < timing.contactHold)
459 {
460 weight = 1.0f;
461 return contact;
462 }
463 if (const float through =
464 (seconds - timing.contactHold) / timing.followThrough;
465 through < 1.0f)
466 {
467 weight = 1.0f;
468 // Fast off the ball, slowing towards the top of the follow-through.
469 const float eased = 1.0f - (1.0f - through) * (1.0f - through);
470 return RenderMath::lerp(contact, finish, eased);
471 }
472 const float back =
473 (seconds - timing.contactHold - timing.followThrough) / timing.recover;
474 weight = 1.0f - smoothStep(back);
475 return finish;
476}
477} // namespace PlayerRig
Definition match_player_rig.h:26
Action
Definition match_player_rig.h:275
TwoBone solveTwoBone(Vec3 root, Vec3 target, float upper, float lower, Vec3 pole)
Definition match_player_rig.h:58
Vec3 kickAnkle(Event event, float seconds, Vec3 contact, Vec3 direction, Vec3 across, const KickTiming &timing, float &weight)
Definition match_player_rig.h:450
Action locomotionFor(float speed, const GaitBands &bands={})
Definition match_player_rig.h:346
Vec3 stanceSpot(const StrideInput &input, float share)
Definition match_player_rig.h:175
Action selectAction(const ActionInput &input, const GaitBands &bands={})
Definition match_player_rig.h:361
constexpr float TWO_PI
Definition match_player_rig.h:31
constexpr float PI
Definition match_player_rig.h:30
Event
Definition match_player_rig.h:298
float smoothStep(float t)
Definition match_player_rig.h:33
float dutyFactor(float speed)
Definition match_player_rig.h:397
float rollOntoToes(Vec3 &ankle, Vec3 hip, Vec3 footForward, float toeLength, float reach, float maxAngle)
Definition match_player_rig.h:94
Swing followThroughFor(Event event)
Definition match_player_rig.h:420
Mood
Definition match_player_rig.h:311
constexpr Vec3 UP
Definition match_player_rig.h:29
float flatDistance(Vec3 a, Vec3 b)
Definition match_player_rig.h:39
Vec3 stepFoot(FootState &foot, const StrideInput &input)
Definition match_player_rig.h:188
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
Vec3 normalize(Vec3 a)
Definition match_render_math.h:68
float length(Vec3 a)
Definition match_render_math.h:67
constexpr Vec3 cross(Vec3 a, Vec3 b)
Definition match_render_math.h:63
std::int64_t minimum
Definition runtime_editor_validation.cpp:46
std::int64_t maximum
Definition runtime_editor_validation.cpp:47
Definition match_player_rig.h:331
bool goalkeeper
Definition match_player_rig.h:335
bool diving
Definition match_player_rig.h:336
Mood mood
Definition match_player_rig.h:343
Event event
Definition match_player_rig.h:334
bool keeperSet
Definition match_player_rig.h:340
float turnRate
Definition match_player_rig.h:333
float speed
Definition match_player_rig.h:332
bool throwIn
Definition match_player_rig.h:342
bool holding
Definition match_player_rig.h:338
Definition match_player_rig.h:127
Vec3 planted
Definition match_player_rig.h:130
Vec3 position
Definition match_player_rig.h:128
Vec3 stepTarget
Definition match_player_rig.h:134
bool inStance
Definition match_player_rig.h:138
float stepSeconds
Definition match_player_rig.h:135
Vec3 liftOff
Definition match_player_rig.h:132
bool valid
Definition match_player_rig.h:139
float yaw
Definition match_player_rig.h:137
Definition match_player_rig.h:319
float run
Definition match_player_rig.h:323
float turnSpeed
Definition match_player_rig.h:326
float idle
Definition match_player_rig.h:320
float turnRate
Definition match_player_rig.h:325
float walk
Definition match_player_rig.h:321
float jog
Definition match_player_rig.h:322
Definition match_player_rig.h:437
float contactHold
Definition match_player_rig.h:438
float total() const
Definition match_player_rig.h:441
float followThrough
Definition match_player_rig.h:439
float recover
Definition match_player_rig.h:440
Definition match_player_rig.h:144
bool idle
Definition match_player_rig.h:164
bool otherStepping
Definition match_player_rig.h:169
float yaw
Definition match_player_rig.h:150
float stanceLength
Definition match_player_rig.h:156
float legPhase
Definition match_player_rig.h:152
float frontReach
Definition match_player_rig.h:158
float deltaSeconds
Definition match_player_rig.h:171
Vec3 rest
Definition match_player_rig.h:146
float liftHeight
Definition match_player_rig.h:159
float duty
Definition match_player_rig.h:154
float stepDuration
Definition match_player_rig.h:167
float maxDrift
Definition match_player_rig.h:162
Vec3 forward
Definition match_player_rig.h:148
float idleStepMetres
Definition match_player_rig.h:166
Definition match_player_rig.h:414
float up
Definition match_player_rig.h:417
float across
Definition match_player_rig.h:416
float forward
Definition match_player_rig.h:415
Definition match_player_rig.h:47
Vec3 end
Definition match_player_rig.h:49
Vec3 middle
Definition match_player_rig.h:48
bool reached
Definition match_player_rig.h:50
Definition match_render_math.h:32
float x
Definition match_render_math.h:33
float z
Definition match_render_math.h:35
float y
Definition match_render_math.h:34