-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Release Automat
committed
May 3, 2024
1 parent
e06d16f
commit cb79b81
Showing
7 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using JetBrains.Annotations; | ||
using UnityEngine; | ||
|
||
namespace TLP.UdonUtils.Runtime.Physics | ||
{ | ||
/// <summary> | ||
/// Utilities to calculate velocities, accelerations etc. | ||
/// </summary> | ||
public static class PhysicsUtils | ||
{ | ||
/// <summary> | ||
/// Based on s(t) = 0.5 at² + v0t + s0 solved to a = 2(s(t) - v0t - s0) / t² with t != 0; v(t) = at + v0 | ||
/// | ||
/// <remarks>The accuracy of the results is HIGHLY dependent | ||
/// on the correctness of the <see cref="initialVelocity"/>! | ||
/// Using this function multiple times with the output velocity as input will eventually produce wrong results | ||
/// due to floating point errors!!! If possible calculate the initial velocity differently.</remarks> | ||
/// </summary> | ||
/// <param name="firstPosition"></param> | ||
/// <param name="secondPosition"></param> | ||
/// <param name="initialVelocity">Velocity at <see cref="firstPosition"/></param> | ||
/// <param name="deltaTime">Time elapsed while the position changed from | ||
/// <see cref="firstPosition"/> to <see cref="secondPosition"/></param> | ||
/// <param name="acceleration">result: calculated acceleration, is zero if t == 0</param> | ||
/// <param name="velocity">calculated velocity based on calculated <see cref="acceleration"/> | ||
/// at point <see cref="secondPosition"/></param> | ||
[PublicAPI] | ||
public static void CalculateAccelerationAndVelocity( | ||
Vector3 firstPosition, | ||
Vector3 secondPosition, | ||
Vector3 initialVelocity, | ||
float deltaTime, | ||
out Vector3 acceleration, | ||
out Vector3 velocity | ||
) { | ||
if (deltaTime == 0) { | ||
acceleration = Vector3.zero; // technically it is infinity, but that is not usable | ||
velocity = initialVelocity; | ||
return; | ||
} | ||
|
||
acceleration = 2f * (secondPosition - initialVelocity * deltaTime - firstPosition) / | ||
(deltaTime * deltaTime); | ||
velocity = acceleration * deltaTime + initialVelocity; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters