-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmathx.Mathf.translations.smoothdamp.cs
144 lines (103 loc) · 5.73 KB
/
mathx.Mathf.translations.smoothdamp.cs
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
142
143
144
#region Header
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
// ** Github Profile: https://github.com/LTMX
// ** Repository : https://github.com/LTMX/Unity.mathx
#endregion
using UnityEngine;
using MI = System.Runtime.CompilerServices.MethodImplAttribute;
namespace Unity.Mathematics
{
public static partial class mathx
{
// https://github.com/FreyaHolmer/Mathfs/blob/master/Runtime/Mathfs.cs
// SmoothDamp ------------------------------------------------------------------------------------------------
[MI(IL)] public static float4 smoothdamp(float4 current, float4 target, ref float4 currentVelocity, float smoothTime, float maxSpeed = float.PositiveInfinity) {
return smoothdamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
}
[MI(IL)] public static float3 smoothdamp(float3 current, float3 target, ref float3 currentVelocity, float smoothTime, float maxSpeed = float.PositiveInfinity) {
return smoothdamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
}
[MI(IL)] public static float2 smoothdamp(float2 current, float2 target, ref float2 currentVelocity, float smoothTime, float maxSpeed = float.PositiveInfinity) {
return smoothdamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
}
[MI(IL)] public static float smoothdamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed = float.PositiveInfinity) {
return smoothdamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
}
[MI(IL)] public static float smoothdamp(float current, float target, ref float velocity, float smoothTime,
[System.ComponentModel.DefaultValue("float.PositiveInfinity")]float maxSpeed,
[System.ComponentModel.DefaultValue("Time.deltaTime")]float deltaTime)
{
smoothTime = 0.0001f.max(smoothTime);
float omega = 2 / smoothTime;
float x = omega * deltaTime;
float exp = (x * (x * (0.48f + x * 0.235f) + 1 )).rcp();
var change = current - target;
var maxChange = maxSpeed * smoothTime;
change = change.clamp(-maxChange, maxChange);
var temp = (velocity + omega * change) * deltaTime;
velocity = (velocity - omega * temp) * exp;
var result = current - change + (change + temp) * exp;
if ((target - current) * (result - target) <= 0) return result;
result = target;
velocity = 0;
return result;
}
[MI(IL)] public static float2 smoothdamp(float2 current, float2 target, ref float2 velocity, float smoothTime,
[System.ComponentModel.DefaultValue("float.PositiveInfinity")]float maxSpeed,
[System.ComponentModel.DefaultValue("Time.deltaTime")]float deltaTime)
{
smoothTime = 0.0001f.max(smoothTime);
float omega = 2 / smoothTime;
float x = omega * deltaTime;
float exp = (x * (x * (0.48f + x * 0.235f) + 1 )).rcp();
var change = current - target;
var maxChange = maxSpeed * smoothTime;
change = change.clamp(-maxChange, maxChange);
var temp = (velocity + omega * change) * deltaTime;
velocity = (velocity - omega * temp) * exp;
var result = current - change + (change + temp) * exp;
if ((target - current).dot(result - target) <= 0) return result;
result = target;
velocity = 0;
return result;
}
[MI(IL)] public static float3 smoothdamp(float3 current, float3 target, ref float3 velocity, float smoothTime,
[System.ComponentModel.DefaultValue("float.PositiveInfinity")]float maxSpeed,
[System.ComponentModel.DefaultValue("Time.deltaTime")]float deltaTime)
{
smoothTime = 0.0001f.max(smoothTime);
float omega = 2 / smoothTime;
float x = omega * deltaTime;
float exp = (x * (x * (0.48f + x * 0.235f) + 1 )).rcp();
var change = current - target;
var maxChange = maxSpeed * smoothTime;
change = change.clamp(-maxChange, maxChange);
var temp = (velocity + omega * change) * deltaTime;
velocity = (velocity - omega * temp) * exp;
var result = current - change + (change + temp) * exp;
if ((target - current).dot(result - target) <= 0) return result;
result = target;
velocity = 0;
return result;
}
[MI(IL)] public static float4 smoothdamp(float4 current, float4 target, ref float4 velocity, float smoothTime,
[System.ComponentModel.DefaultValue("float.PositiveInfinity")]float maxSpeed,
[System.ComponentModel.DefaultValue("Time.deltaTime")]float deltaTime)
{
smoothTime = 0.0001f.max(smoothTime);
float omega = 2 / smoothTime;
float x = omega * deltaTime;
float exp = (x * (x * (0.48f + x * 0.235f) + 1 )).rcp();
var change = current - target;
var maxChange = maxSpeed * smoothTime;
change = change.clamp(-maxChange, maxChange);
var temp = (velocity + omega * change) * deltaTime;
velocity = (velocity - omega * temp) * exp;
var result = current - change + (change + temp) * exp;
if ((target - current).dot(result - target) <= 0) return result;
result = target;
velocity = 0;
return result;
}
}
}