Skip to content

Commit c8de32d

Browse files
committed
update
1 parent 4e58029 commit c8de32d

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

notes/new_code.txt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,50 @@ sides = 6
125125
r = ( R * cos( PI / sides ) ) /
126126
cos( ( 2* asin( sin( ( sides * a ) / 2 ))) / sides )
127127
d = v.length()
128-
return d < r;
128+
return d < r;
129+
130+
131+
function dampSpringValue( value, target, velocity, dt, damp=0.4, tension=1 ){
132+
const accel = ( value - target ) * tension;
133+
const vel = ( velocity * damp ) + accel * dt;
134+
const val = value + vel * dt;
135+
return [ val, vel ];
136+
}
137+
138+
//startAmplitude*sin(omega*t+startPhase)*exp(-t*dampningConstant)
139+
// https://www.statisticshowto.com/calculus-definitions/damped-sine-wave/
140+
// function dampedSineWave( t, decay=0.9, maxAmp=1 ){
141+
// return maxAmp * Math.exp( -decay * t ) * Math.cos( Math.PI * 2 );
142+
// }
143+
144+
145+
https://www.alexisbacot.com/blog/the-art-of-damping ( EXTRA SPRING STUFF )
146+
https://github.com/AlexisBacot/ArtOfDamping/blob/main/Assets/Scripts/ToolDamper.cs
147+
148+
149+
150+
https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Runtime/Export/Math/Mathf.cs#L303
151+
function smoothDamp( cur, tar, vel, dt, smoothTime=0.0001, maxSpeed=Infinity ){
152+
// Based on Game Programming Gems 4 Chapter 1.10
153+
smoothTime = Math.max( 0.0001, smoothTime );
154+
const omega = 2 / smoothTime;
155+
const x = omega * dt;
156+
const exp = 1 / ( 1 + x + 0.48 * x * x + 0.235 * x * x * x);
157+
let change = cur - tar;
158+
159+
// Clamp maximum speed
160+
const maxChange = maxSpeed * smoothTime;
161+
change = Math.min( maxChange, Math.max( change, -maxChange ) );
162+
163+
const temp = ( vel + omega * change ) * dt;
164+
vel = ( vel - omega * temp ) * exp;
165+
let output = ( cur - change ) + ( change + temp ) * exp;
166+
167+
// Prevent overshooting
168+
if( tar - cur > 0.0 && output > tar ){
169+
output = tar;
170+
vel = (output - tar) / dt;
171+
}
172+
173+
return [ output, vel ];
174+
}

0 commit comments

Comments
 (0)