Skip to content

A collection of some of the neat math and physics tricks that I've collected over the last few years.

License

Notifications You must be signed in to change notification settings

hulsmeier/MathUtilities

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathUtilities

A grab bag of some of the neat math and physics tricks that I've amassed over the last few years, implemented in Unity's C#. You're free to use the code here however you like.

An extremely fast, general mesh-deformation algorithm with arbitrary point-placement and configurable rigidity. Has the desirable property that it acts like a rigid Kabsch when the "weight" (ductility) is set to 0, but smoothly blends in deformation as the weight is increased.

The implementation is similar to Linear Blend Skinning, but the skinning weights are automatically calculated with Inverse Distance Weighting (in cartesian or surface space), and the "bone rotations" are calculated to optimally preserve the angular relationships between the control points.

A simple example for raymarching and painting/blitting-to volumetric distance field textures. Distance Fields are regions of space that store the distance to the nearest surface at each point in space. This property allows them to represent and render solid shapes of arbitrary geometry and topology.

This particular implementation also stores the normalized vector toward the nearest point (the normal or gradient of the field). This is useful for physics queries and lighting (without taking the numerical derivative).

Also known as Procrustes Analysis, this algorithm can take in an arbitrary set of point-pairs and find the globally optimal rigid translation and rotation to minimize the distance between those point pairs. Incredibly useful, and very cheap. Uses Matthias Muller's polar decomposition solver in place of SVD, as outlined here: https://animation.rwth-aachen.de/media/papers/2016-MIG-StableRotation.pdf

Update: Added an example for averaging arbitrary numbers of quaternions; possibly more accurate than a normalized lerp (averaging the quaternion components in linear space and then normalizing).

A generic helper utility for fitting lines and planes to point-sets in 3D. Uses a novel* matrix-less formulation to solve for the orthogonal lines and planes of best fit; algorithm is without singularities and is extensible to arbitrary dimensionality.

*(as far as I know; I have not seen anyone attempt orthogonal regression without using an SVD (which may be unnecessarily expensive for just the line/plane of best fit))

A prefab that concatenates and warps the images from four cameras into one 180 degree fisheye view, projected stereographically.

Numerous examples of using verlet integration (a subset of Position Based Dynamics) to simulate soft/rigid bodies, cloth, and particle linkage chains.

A textbook implementation of a Kalman filter (transcribed from wikipedia) Kalman filters are a form of bayesian filtering; they are capable of taking in information from multiple sources and "fusing" them into a signal that is cleaner/more accurate than any of the constituent signals. A properly tuned Kalman filter is the mathematically "optimal" technique for turning noisy data into clean data in real time (as long as the noise follows a gaussian distribution and the data varies linearly). In practice one must deal with biases and non-linearly varying quantities.

A set of constraint functions that can be used to build an iterative inverse kinematics solver.

Nice introductory Tutorial to the concept behind CCDIK and the Kinematic Jacobian

The "Configuration Space" can be visualized by graphing the penetration of a robot with it's environment (and itself) as a distance field, where each axis is the angle/configuration of an individual joint. By path finding through valid regions in this space, one is actually planning the motion of the robot from one configuration to another. The gradient of the configuration space can also be used for light depenetration of the robot from invalid configurations. However, because precomputing the configuration space is slow (and must be redone for objects in the environment), I developed a variant of CCDIK (CCCDIK :) ) which iteratively depenetrates itself from the environment by temporarily treating the contact point as a new end-effector.

Other experiments:

A general, n-dimensional implementation of Nelder and Mead's gradient-less numerical optimization method for minimizing cost functions. This is a popular optimization technique for problems with high-dimensionality and no gradient information. Included is an example of optimizing a 5-DoF IK system (far less efficient than CCDIK, but more flexible overall). Also contains a numerical gradient descent optimizer for comparison.

A port of Roy Jonker's famous solution to the Linear Assignment Problem. Allows you to take two arbitrary lists of objects (with a cost to pair objects in each of them to each other), and to find the globally optimal pairing betweeing objects in these lists. Extremely handy.

See the source file for Commercial Licensing Details.

A reference implementation that demonstrates how to apply bone motions to a model using the data contained within a skinned mesh renderer. As they say, there is more than one way to skin a mesh.

Demonstrates how to set up a shader to project a texture onto scene geometry using a (disabled) camera as the projector frustum. Useful for illusions.

Contains a class for generating a near-uniform quasirandom distribution of points for arbitrary dimensionality and point counts. Based off of the article here: http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/

Shows an interesting technique for displacing particles to flow around distance fields (would work very well in UE4's Niagara).

Uses a compute shader to draw arbitrary 2D Minkowski "Differences" in real time. The Minkowski Sum (and its modification, the "Minkowski Difference") is a core operation in collision detection. This concept allows for a fully generalized way of determining whether any two objects are intersecting, and what the minimum translation is that separates them. The key is determining whether the origin (of the coordinate system used in the operation) is inside of the resulting Minkowski Difference shape. GJK is a collision detection technique that implements this check quickly for convex objects, with only a few samples of the implicit Minkowski Difference.

Operates like a standard raycast, but returns both entry and exit information. Useful for building wires that wrap around the environment and bullet entry/exit effects.

A special heuristic formula to compute the time-optimal movement trajectory for a double-integrating mass with limited thrust.

Formula taken from this excellent course: http://underactuated.csail.mit.edu/underactuated.html?chapter=9

This is a technique for augmenting the end-point of trajectories composed of discrete segments, using a rigid-as-possible/"Blossoming" (Bézier-like) interpolation scheme. Includes an implementation for both 3D and 6D trajectories.

Inspired by this paper: https://april.eecs.umich.edu/media/pdfs/olson2006icra.pdf

What appears to be a unique geometric solution for calculating the pivot point of a rigid transformation in 2D and 3D (where applicable). Is computationally efficient, geometrically intuitive, and can be extended to arbitrary dimensions.

Useful for generating solid meshes that are essentially deformed planes (shapes that are common in free-form optics).

Useful for generating capsule meshes with an arbitrary length and radius.

A little shader that raytraces a pixel-perfect sphere on your object/billboard, for when you want to be pixel-bound rather than vertex bound...

Also contains a small demo for finding tangential points to circles and spheres.

A serialization utility that smartly serializes and deserializes game object hierarchies, engine components, monobehaviours, references, and more at runtime using reflection. Useful for a save/load system or a runtime editor.

A generic matrix class and a set of basic matrix operations (multiplication, addition, subtraction, inversion, cholesky decomposition, and more). Written to support the implementation of the Kalman Filter. Usage of any of these operations will allocate a new array (garbage), so be careful about using this on performance constrained systems.

An example implementing a haptic probe, where the force on the effector of the haptic controller is equal to the linear and angular displacement of the green cube to the gray one.

My attempts at implementing Bundle Adjustment (an algorithm which attempts to solve for the relative motion between two camera images, given the motion of a set of feature-points between the images). The stereo-case now converges to a unique 6-DoF pose (in most situations). This implementation is highly parallelizable.

This adds a function to apply torque "through" HingeJoints and to Rigidbodies in general.

An untested function for encoding an n-dimensional vector to the surface of an n+1 dimensional torus. Originally intended to allow Neural Networks to learn continuous cyclic functions without ballooning the dimensionality to 2n (ie trivial x -> (sin(x), cos(x)) case).

Attempts to simulate soft-spongy contact by damping the accelerations that are be applied to an object. Phenomena like friction cause it to exhibit strange artifacts...

Uses a neat trick where, if the anchor of a spring joint is moved, both connected rigidbodies are physically affected. This allows one to easily simulate "muscles".

Fun for the whole family!

About

A collection of some of the neat math and physics tricks that I've collected over the last few years.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 90.7%
  • ShaderLab 6.6%
  • Python 1.6%
  • GLSL 1.1%