File: AxisPhysicsModel.cpp

package info (click to toggle)
firefox-esr 115.15.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,659,216 kB
  • sloc: cpp: 6,676,686; javascript: 5,690,965; ansic: 3,328,546; python: 1,120,594; asm: 397,163; xml: 180,531; java: 178,838; sh: 68,930; makefile: 20,999; perl: 12,595; objc: 12,561; yacc: 4,583; cs: 3,846; pascal: 2,840; lex: 1,720; ruby: 1,079; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (101 lines) | stat: -rw-r--r-- 3,636 bytes parent folder | download | duplicates (29)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "AxisPhysicsModel.h"

namespace mozilla {
namespace layers {

/**
 * The simulation is advanced forward in time with a fixed time step to ensure
 * that it remains deterministic given variable framerates.  To determine the
 * position at any variable time, two samples are interpolated.
 *
 * kFixedtimestep is set to 120hz in order to ensure that every frame in a
 * common 60hz refresh rate display will have at least one physics simulation
 * sample.  More accuracy can be obtained by reducing kFixedTimestep to smaller
 * intervals, such as 240hz or 1000hz, at the cost of more CPU cycles.  If
 * kFixedTimestep is increased to much longer intervals, interpolation will
 * become less effective at reducing temporal jitter and the simulation will
 * lose accuracy.
 */
const double AxisPhysicsModel::kFixedTimestep = 1.0 / 120.0;  // 120hz

/**
 * Constructs an AxisPhysicsModel with initial values for state.
 *
 * @param aInitialPosition sets the initial position of the simulation,
 *        in AppUnits.
 * @param aInitialVelocity sets the initial velocity of the simulation,
 *        in AppUnits / second.
 */
AxisPhysicsModel::AxisPhysicsModel(double aInitialPosition,
                                   double aInitialVelocity)
    : mProgress(1.0),
      mPrevState(aInitialPosition, aInitialVelocity),
      mNextState(aInitialPosition, aInitialVelocity) {}

AxisPhysicsModel::~AxisPhysicsModel() = default;

double AxisPhysicsModel::GetVelocity() const {
  return LinearInterpolate(mPrevState.v, mNextState.v, mProgress);
}

double AxisPhysicsModel::GetPosition() const {
  return LinearInterpolate(mPrevState.p, mNextState.p, mProgress);
}

void AxisPhysicsModel::SetVelocity(double aVelocity) {
  mNextState.v = aVelocity;
  mNextState.p = GetPosition();
  mProgress = 1.0;
}

void AxisPhysicsModel::SetPosition(double aPosition) {
  mNextState.v = GetVelocity();
  mNextState.p = aPosition;
  mProgress = 1.0;
}

void AxisPhysicsModel::Simulate(const TimeDuration& aDeltaTime) {
  for (mProgress += aDeltaTime.ToSeconds() / kFixedTimestep; mProgress > 1.0;
       mProgress -= 1.0) {
    Integrate(kFixedTimestep);
  }
}

void AxisPhysicsModel::Integrate(double aDeltaTime) {
  mPrevState = mNextState;

  // RK4 (Runge-Kutta method) Integration
  // http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
  Derivative a = Evaluate(mNextState, 0.0, Derivative());
  Derivative b = Evaluate(mNextState, aDeltaTime * 0.5, a);
  Derivative c = Evaluate(mNextState, aDeltaTime * 0.5, b);
  Derivative d = Evaluate(mNextState, aDeltaTime, c);

  double dpdt = 1.0 / 6.0 * (a.dp + 2.0 * (b.dp + c.dp) + d.dp);
  double dvdt = 1.0 / 6.0 * (a.dv + 2.0 * (b.dv + c.dv) + d.dv);

  mNextState.p += dpdt * aDeltaTime;
  mNextState.v += dvdt * aDeltaTime;
}

AxisPhysicsModel::Derivative AxisPhysicsModel::Evaluate(
    const State& aInitState, double aDeltaTime, const Derivative& aDerivative) {
  State state(aInitState.p + aDerivative.dp * aDeltaTime,
              aInitState.v + aDerivative.dv * aDeltaTime);

  return Derivative(state.v, Acceleration(state));
}

double AxisPhysicsModel::LinearInterpolate(double aV1, double aV2,
                                           double aBlend) {
  return aV1 * (1.0 - aBlend) + aV2 * aBlend;
}

}  // namespace layers
}  // namespace mozilla