Rise
The Vieneo Province
Loading...
Searching...
No Matches
PropAudio.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <cstdint>
5
6namespace PropAudio {
7struct Input {
8 bool active=false, advanced=false, contact=false, sleeping=false, impact=false;
10 std::uint64_t frame=0;
11};
12struct Events { bool startRolling=false, stopRolling=false, hit=false, stopAll=false; };
13// Audio presentation thresholds, not physical forces or material properties.
14// Hysteresis bridges brief contact gaps as a faceted prop rolls over its rim.
15class State {
16 bool active=false, rolling=false;
18 std::uint64_t lastFrame=0;
19public:
20 void Reset() { *this=State{}; }
21 Events Update(double dt,const Input& input) {
22 Events result;
23 if(!input.active || !std::isfinite(dt) || dt<=0 || dt>.25 ||
24 !std::isfinite(input.speedMps) || !std::isfinite(input.rotationSpeedMps)) {
25 result.stopAll=active;Reset();return result;
26 }
27 active=true;
28 hitCooldown=std::max(0.0,hitCooldown-dt);
29 if(input.frame==lastFrame) return result;
30 lastFrame=input.frame;
31 if(input.sleeping) {
32 result.stopRolling=rolling;rolling=false;quietSeconds=0;return result;
33 }
34 // Render frames with no physics substep must not end a rolling episode.
35 if(!input.advanced) return result;
36 if(input.impact && hitCooldown<=0) {result.hit=true;hitCooldown=.12;}
37 const double rollingSpeed=std::min(input.speedMps,input.rotationSpeedMps);
38 const bool moving=input.contact && rollingSpeed>(rolling?.012:.03);
39 if(moving) {
41 if(!rolling) {rolling=true;result.startRolling=true;}
42 } else if(rolling) {
43 quietSeconds+=dt;
44 if(quietSeconds>=.12) {rolling=false;quietSeconds=0;result.stopRolling=true;}
45 }
46 return result;
47 }
48};
49}
Events Update(double dt, const Input &input)
Definition PropAudio.h:21
std::uint64_t lastFrame
Definition PropAudio.h:18
double hitCooldown
Definition PropAudio.h:17
double quietSeconds
Definition PropAudio.h:17
double rotationSpeedMps
Definition PropAudio.h:9
double speedMps
Definition PropAudio.h:9
std::uint64_t frame
Definition PropAudio.h:10