Giter Site home page Giter Site logo

godot-math-extensions's Introduction

Godot Math Extensions

Additional math helper functions and behaviors for manipulating angles and floats.

I'm hopeful that the methods in MathEx will be added to Godot 4, as the interpolation example (lerp and lerp_angle) in the documentation is frame-rate dependent.

This extension also includes FloatSmooth (based on the same smoothing function as Unity's SmoothDamp). Though I find the spring function's timing imprecise, and am still searching for a more accurately controllable solution.

Usage

Helper functions (MathEx)

Helper functions are contained in the MathEx class:

var angle = MathEx.angle(deg_to_rad(0), deg_to_rad(90))

Currently this comprises:

angle(from: float, to: float) -> float
move_toward_angle(from: float, to: float, delta: float) -> float

Float smoothing (FloatSmooth)

FloatSmooth is a small class for smoothly modifying a float value.

Due to the spring model requiring two tracked properties (value and velocity), the options for implementing it in Godot were either to return an Array/Dictionary, or to track both in a dedicated class. This is the latter.

To use it, create a variable โ€” this should be persistent, don't create a new one each time:

var my_value := FloatSmooth.new(1.0)

To smoothly modify the value, use move_toward. This returns the updated value for immediate use:

func _process(delta: float) -> void:
    var result := my_value.move_toward(10, delta)
    print(result)

For angles (in radians), use move_toward_angle. Which will correctly wrap around TAU:

var result := my_value.move_toward_angle(deg_to_rad(90), delta)

Use set_instant to change the value without smoothing. This clears the current velocity:

my_value.set_instant(0.0)

FloatSmooth is developed with thanks to:
Lowe, T. (2004). Critically damped ease-in/ease-out smoothing.
In A, Kirmse (Ed.), Game programming gems 4 (pp. 95-101). Charles River Media, Inc.

godot-math-extensions's People

Contributors

adamscoble avatar

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.