Coder Social home page Coder Social logo

mathfs's Introduction

Mathfs

Freya's expanded math functionality for Unity!

  • This is primarily a way for me to share the math functionality I write and use in my own personal projects
  • I will recklessly edit and adapt things without too much thought into backwards compatibility
  • Minimum Unity version is currently 2021.2 due to using newer C# version features. It may be possible to auto-downgrade through your IDE if necessary
  • Commits with version tags should be relatively stable. Other commits may not be

Installation instructions

There are several ways to install this library into your project:

  • Plain install
    • Clone or download this repository and put it somewhere in the Assets folder of your Unity project
  • Unity Package Manager (UPM):
    • Add either of the the following lines to Packages/manifest.json:
    • "com.acegikmo.mathfs": "https://github.com/FreyaHolmer/Mathfs.git#0.1.0", if you want to target a specific version (recommended)
    • "com.acegikmo.mathfs": "https://github.com/FreyaHolmer/Mathfs.git", if you want to pull the latest commit (potentially unstable)
    • More information about UPM and git here
  • OpenUPM
    • After installing openupm-cli, run the following command:
    • openupm add com.acegikmo.mathfs

After installation you will be able to access the library in scripts by including the namespace using Freya

Features

  • 2D Intersection tests between all combinations of:
    • Ray
    • LineSegment
    • Line
    • Circle
  • Curves & Splines
    • Bézier (Quadratic, Cubic & Generalized)
    • Hermite
    • Catmull-Rom
    • B-Spline (Uniform Cubic & Generalized Non-Uniform)
    • NURBS (Non-Unifrom Rational B-Spline)
    • Trajectory (Cubic & Generalized)
  • Trajectory math
    • GetDisplacement (point in trajectory), given gravity, angle, speed & time
    • GetLaunchSpeed, given gravity, angle & lateral distance
    • GetLaunchAngles, given gravity, speed & lateral distance
    • GetMaxRange, given gravity & speed
    • GetHeightPotential, given gravity, current height and speed
    • GetSpeedFromHeightPotential, given gravity, current height and height potential
  • Triangle math
    • Area / SignedArea, given three points or base and height
    • Contains check, given three triangle vertices and a point to test by
    • Right-angle trig functions to calculate Opposite/Adjacent/Hypotenuse/Angle
    • Incenter / Centroid
    • Incircle / Circumcircle
    • SmallestAngle
  • Polygon math
    • Area / SignedArea
    • IsClockwise
    • WindingNumber
    • Contains
  • Circle math
    • FromTwoPoints (get smallest circle passing through both points)
    • FromThreePoints (get unique circle passing through three points)
    • RadiusToArea / AreaToRadius
    • AreaToCircumference / CircumferenceToArea
    • RadiusToCircumference / CircumferenceToRadius
  • 2D Angle helpers (AngToDir, DirToAng...)
  • 2D Vector extension methods (Rotate90CCW/CW, Rotate, RotateAround...)
  • Quadratic & Linear Root finders
  • Remap functions
  • Constants (Tau, Pi, Golden Ratio, e, sqrt2)
  • Vector extension methods (WithMagnitude, ClampMagnitude(min,max)...)
  • Expanded basic math operations to vectors (Clamp, Round, Abs...)
  • Color extensions (WithAlpha, MultiplyRGB...)
  • Smoothing functions (Smooth01, SmoothCos01...)
  • Triangle Math helpers (SignedArea, Circumcenter, Incircle...)
  • Circle Math helpers (Area, Circumference...)
  • All functions use radians
  • And more!

Changes

Mathfs.cs does not fully match Unity's Mathf.cs, I've made a few changes:

  • All angles are in radians, no methods use degrees
  • Lerp and InverseLerp:
    • Unclamped by default
    • LerpClamped/InverseLerpClamped are now the special case functions/exceptions
    • Uses the more numerically stable evaluation
  • Smoothstep is removed in favor of the more explicit:
    • LerpSmooth (which is how it was implemented) and
    • InverseLerpSmooth (which is how it is implemented everywhere but Unity's Mathf.cs)
  • Min/Max functions with arbitrary inputs/array input will throw on empty instead of returning 0

mathfs's People

Contributors

cabauman avatar deepchallengervr avatar freyaholmer avatar razenpok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mathfs's Issues

Catenary3D space transformation not working

Hey, Freya!

In the Catenary3D constructor, the space is being initialized to default after p0 and axisY are set, which is causing the transformation not to work when only the constructor is used to create the struct. I've fixed it on my local copy of the lib but thought I should let you know :)

And thanks for the awesome library!

Support for Unity Mathematics

Just came across this repo, and it looks fantastic!
I've been in the process of more heavily investing in Burst optimizing static functions, and this package looks largely static to begin with.

Any chance it could leverage mathematics utilities and structs instead of the ones in the old math library, so that they could automatically be optimized using the compiler optimizations supported by the new math library?

Library is incompatible with Unity 2020

The code uses modern C# 9.0 features which are not available on Unity 2020 (which is the current LTS). Do you want:

  • it to be Unity 2021+ compatible, or
  • port code back into C# 8.0

I'm getting kinda involved with all this at this point, so I can help with the second one.

Box2D.Contains doesn't work properly

Hi Freya,

Thank you for this incredible library! I noticed that Box2D.Contains method doesn't account for a world position, checking just extents.

Format as unity package

Hey @FreyaHolmer ! An awesome contribution! Perhaps You are open making this as a unity package ? If You do not know how - i can fork it and open as pull request :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.