18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 09:44:59 +01:00

fix(camera): the cab shake taken at the frame's moment between its fixed steps, it no longer moves in steps (#403)

The shake of the cab camera (ShakeState) is stepped by update_shake at a fixed 1/50 s, while the camera read the
raw state every frame. At any frame rate other than 50 the shake moved in steps: some frames showed the same
state as the one before, others jumped one or two steps at once. Players with 144/165 Hz screens saw the cab
jump around the camera.

The vehicle now keeps the state from before the last step (ShakeStateBefore), and the camera takes the state
between the two at the share of the step the simulation time has reached (shake_between). The offset and the
velocity are blended, the angles worked out from the blended velocity. The shake itself is computed as before,
with the same fixed step and the same springs; only what the camera shows of it changes. It lags the vehicle
by a constant 1/50 s instead of 0 to 1/50 s.

Measured with the same build and the blending switched off by a temporary environment variable (not in this
commit), the EU07 on td.scn driven by the AI, frames over 30 km/h, two runs of each:
- 60 fps: frames with the shake standing still 25% -> 0%, the change in its speed frame to frame 4-5 times
  smaller for the offset, a third smaller for the angles
- ~144 fps: 65% -> 0%, 10-18 times smaller for the offset, 4 times for the angles
- no fps limit (~520 fps): 90% -> 0%

Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
maj00r
2026-11-20 03:39:03 +01:00
committed by GitHub
parent 72c69d9249
commit a1fbdf47c6
4 changed files with 50 additions and 6 deletions

View File

@@ -31,6 +31,9 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Timer.h" #include "utilities/Timer.h"
#include "rendering/renderer.h" #include "rendering/renderer.h"
#include "utilities/Logs.h" #include "utilities/Logs.h"
#include <algorithm>
/* /*
namespace input { namespace input {
@@ -627,6 +630,12 @@ bool driver_mode::is_command_processor() const
return true; return true;
} }
double driver_mode::shake_fraction() const
{
// how far the simulation time has got from the last step of the shake towards the next one
return std::clamp(m_secondaryupdateaccumulator / m_secondaryupdaterate, 0.0, 1.0);
}
void driver_mode::update_camera(double const Deltatime) void driver_mode::update_camera(double const Deltatime)
{ {
@@ -770,7 +779,8 @@ void driver_mode::update_camera(double const Deltatime)
// ale bez odbicia // ale bez odbicia
Camera.LookAt = Camera.Pos - simulation::Train->GetDirection() * (double)simulation::Train->Occupied()->CabOccupied; //-1 albo 1 Camera.LookAt = Camera.Pos - simulation::Train->GetDirection() * (double)simulation::Train->Occupied()->CabOccupied; //-1 albo 1
} }
auto const shakeangles{simulation::Train->Dynamic()->shake_angles()}; auto const *dynamic{simulation::Train->Dynamic()};
auto const shakeangles{dynamic->shake_angles(dynamic->shake_between(shake_fraction()))};
Camera.Angle.x = 0.5 * shakeangles.second; // hustanie kamery przod tyl Camera.Angle.x = 0.5 * shakeangles.second; // hustanie kamery przod tyl
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
/* /*
@@ -791,8 +801,9 @@ void driver_mode::update_camera(double const Deltatime)
} }
float const shakescale{FreeFlyModeFlag ? 5.0f : 1.0f}; float const shakescale{FreeFlyModeFlag ? 5.0f : 1.0f};
auto shakencamerapos{Camera.m_owneroffset + // the shake steps at a fixed rate, the frames come at their own: taken at the frame's moment between the steps, it moves as smoothly as the vehicle
shakescale * glm::vec3(1.5 * Camera.m_owner->ShakeState.offset.x, 2.0 * Camera.m_owner->ShakeState.offset.y, 1.5 * Camera.m_owner->ShakeState.offset.z)}; auto const shake{Camera.m_owner->shake_between(shake_fraction())};
auto shakencamerapos{Camera.m_owneroffset + shakescale * glm::vec3(1.5 * shake.offset.x, 2.0 * shake.offset.y, 1.5 * shake.offset.z)};
Camera.Pos = Camera.m_owner->GetWorldPosition(FreeFlyModeFlag ? glm::dvec3(shakencamerapos) : // TODO: vehicle collision box for the external vehicle camera Camera.Pos = Camera.m_owner->GetWorldPosition(FreeFlyModeFlag ? glm::dvec3(shakencamerapos) : // TODO: vehicle collision box for the external vehicle camera
simulation::Train->clamp_inside(shakencamerapos)); simulation::Train->clamp_inside(shakencamerapos));
@@ -800,7 +811,7 @@ void driver_mode::update_camera(double const Deltatime)
if (!Global.iPause) if (!Global.iPause)
{ {
// podczas pauzy nie przeliczać kątów przypadkowymi wartościami // podczas pauzy nie przeliczać kątów przypadkowymi wartościami
auto const shakeangles{Camera.m_owner->shake_angles()}; auto const shakeangles{Camera.m_owner->shake_angles(shake)};
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
/* /*

View File

@@ -91,6 +91,7 @@ private:
// methods // methods
void update_camera( const double Deltatime ); void update_camera( const double Deltatime );
double shake_fraction() const;
// handles vehicle change flag // handles vehicle change flag
void OnKeyDown( int cKey ); void OnKeyDown( int cKey );
void InOutKey(); void InOutKey();

View File

@@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/.
#include "vehicle/Driver.h" #include "vehicle/Driver.h"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <iterator> #include <iterator>
#include <unordered_set> #include <unordered_set>
#include "utilities/Globals.h" #include "utilities/Globals.h"
@@ -2369,6 +2370,7 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
ShakeSpring.Init( 125.0 ); ShakeSpring.Init( 125.0 );
BaseShake = baseshake_config {}; BaseShake = baseshake_config {};
ShakeState = shake_state {}; ShakeState = shake_state {};
ShakeStateBefore = shake_state {};
// McZapkie-250202 // McZapkie-250202
/* /*
@@ -8236,6 +8238,8 @@ void TDynamicObject::OverheadTrack(float o)
void void
TDynamicObject::update_shake( double const Timedelta ) { TDynamicObject::update_shake( double const Timedelta ) {
ShakeStateBefore = ShakeState;
// Ra: mechanik powinien być telepany niezależnie od pozycji pojazdu // Ra: mechanik powinien być telepany niezależnie od pozycji pojazdu
// Ra: trzeba zrobić model bujania głową i wczepić go do pojazdu // Ra: trzeba zrobić model bujania głową i wczepić go do pojazdu
@@ -8360,9 +8364,32 @@ TDynamicObject::axle_passed_joint( axle_sounds &Axle, double const Move, int con
std::pair<double, double> std::pair<double, double>
TDynamicObject::shake_angles() const { TDynamicObject::shake_angles() const {
return shake_angles( ShakeState );
}
std::pair<double, double>
TDynamicObject::shake_angles( shake_state const &State ) const {
return { return {
std::atan( ShakeState.velocity.x * BaseShake.angle_scale.x ), std::atan( State.velocity.x * BaseShake.angle_scale.x ),
std::atan( ShakeState.velocity.z * BaseShake.angle_scale.z ) }; std::atan( State.velocity.z * BaseShake.angle_scale.z ) };
}
TDynamicObject::shake_state
TDynamicObject::shake_between( double const Fraction ) const {
if( Fraction >= 1.0 ) {
return ShakeState;
}
auto const between { []( glm::dvec3 const &Before, glm::dvec3 const &After, double const Part ) {
return glm::dvec3 {
std::lerp( Before.x, After.x, Part ),
std::lerp( Before.y, After.y, Part ),
std::lerp( Before.z, After.z, Part ) }; } };
auto const part { std::max( Fraction, 0.0 ) };
return {
between( ShakeStateBefore.velocity, ShakeState.velocity, part ),
between( ShakeStateBefore.offset, ShakeState.offset, part ) };
} }
void void

View File

@@ -918,6 +918,11 @@ public:
glm::dvec3 velocity{}; // current shaking vector glm::dvec3 velocity{}; // current shaking vector
glm::dvec3 offset{}; // overall shake-driven offset from base position glm::dvec3 offset{}; // overall shake-driven offset from base position
} ShakeState; } ShakeState;
shake_state ShakeStateBefore; // the state before the last step of update_shake
// the shake between the last two steps of update_shake: 0 the state before the last step, 1 the state after it.
// the steps are fixed and the frames aren't, so the camera takes the state at its own moment between the two
shake_state shake_between( double const Fraction ) const;
std::pair<double, double> shake_angles( shake_state const &State ) const;
glm::dvec3 modelShake; glm::dvec3 modelShake;
}; };