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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Cinemachine.Utility
{
internal class PositionPredictor
{
Vector3 m_Position;
const float kSmoothingDefault = 10;
float mSmoothing = kSmoothingDefault;
public float Smoothing
{
get { return mSmoothing; }
set
{
if (value != mSmoothing)
{
mSmoothing = value;
int maxRadius = Mathf.Max(10, Mathf.FloorToInt(value * 1.5f));
m_Velocity = new GaussianWindow1D_Vector3(mSmoothing, maxRadius);
m_Accel = new GaussianWindow1D_Vector3(mSmoothing, maxRadius);
}
}
}
GaussianWindow1D_Vector3 m_Velocity = new GaussianWindow1D_Vector3(kSmoothingDefault);
GaussianWindow1D_Vector3 m_Accel = new GaussianWindow1D_Vector3(kSmoothingDefault);
public bool IsEmpty { get { return m_Velocity.IsEmpty(); } }
public void Reset()
{
m_Velocity.Reset();
m_Accel.Reset();
}
public void AddPosition(Vector3 pos)
{
if (IsEmpty)
m_Velocity.AddValue(Vector3.zero);
else
{
Vector3 vel = m_Velocity.Value();
Vector3 vel2 = (pos - m_Position) / Time.deltaTime;
m_Velocity.AddValue(vel2);
m_Accel.AddValue(vel2 - vel);
}
m_Position = pos;
}
public Vector3 PredictPosition(float lookaheadTime)
{
int numSteps = Mathf.Min(Mathf.RoundToInt(lookaheadTime / Time.deltaTime), 6);
float dt = lookaheadTime / numSteps;
Vector3 pos = m_Position;
Vector3 vel = m_Velocity.IsEmpty() ? Vector3.zero : m_Velocity.Value();
Vector3 accel = m_Accel.IsEmpty() ? Vector3.zero : m_Accel.Value();
for (int i = 0; i < numSteps; ++i)
{
pos += vel * dt;
Vector3 vel2 = vel + (accel * dt);
accel = Quaternion.FromToRotation(vel, vel2) * accel;
vel = vel2;
}
return pos;
}
}
/// <summary>Utility to perform realistic damping of float or Vector3 values.
/// The algorithm is based on exponentially decaying the delta until only
/// a negligible amount remains.</summary>
public static class Damper
{
const float Epsilon = UnityVectorExtensions.Epsilon;
// Get the decay constant that would leave a given residual after a given time
static float DecayConstant(float time, float residual)
{
return Mathf.Log(1f / residual) / time;
}
// Exponential decay: decay a given quantity opver a period of time
static float Decay(float initial, float decayConstant, float deltaTime)
{
return initial / Mathf.Exp(decayConstant * deltaTime);
}
/// <summary>Standard residual</summary>
public const float kNegligibleResidual = 0.01f;
/// <summary>Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.</summary>
/// <param name="initial">The amount that will be damped</param>
/// <param name="dampTime">The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage</param>
/// <param name="deltaTime">The time over which to damp</param>
/// <returns>The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.</returns>
public static float Damp(float initial, float dampTime, float deltaTime)
{
if (dampTime < Epsilon || Mathf.Abs(initial) < Epsilon)
return initial;
if (deltaTime < Epsilon)
return 0;
return initial - Decay(
initial, DecayConstant(dampTime, kNegligibleResidual), deltaTime);
}
/// <summary>Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.</summary>
/// <param name="initial">The amount that will be damped</param>
/// <param name="dampTime">The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage</param>
/// <param name="deltaTime">The time over which to damp</param>
/// <returns>The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.</returns>
public static Vector3 Damp(Vector3 initial, Vector3 dampTime, float deltaTime)
{
for (int i = 0; i < 3; ++i)
initial[i] = Damp(initial[i], dampTime[i], deltaTime);
return initial;
}
/// <summary>Get a damped version of a quantity. This is the portion of the
/// quantity that will take effect over the given time.</summary>
/// <param name="initial">The amount that will be damped</param>
/// <param name="dampTime">The rate of damping. This is the time it would
/// take to reduce the original amount to a negligible percentage</param>
/// <param name="deltaTime">The time over which to damp</param>
/// <returns>The damped amount. This will be the original amount scaled by
/// a value between 0 and 1.</returns>
public static Vector3 Damp(Vector3 initial, float dampTime, float deltaTime)
{
for (int i = 0; i < 3; ++i)
initial[i] = Damp(initial[i], dampTime, deltaTime);
return initial;
}
}
}
|