Skip to content

Instantly share code, notes, and snippets.

@regakakobigman
Last active July 16, 2019 10:35
Show Gist options
  • Save regakakobigman/1043698fe035486af8d924281f7275d2 to your computer and use it in GitHub Desktop.
Save regakakobigman/1043698fe035486af8d924281f7275d2 to your computer and use it in GitHub Desktop.
Tools for dealing with angles in GDScript
# Useful functions for calculating distances between angles
static func lerp_angle(from: float, to: float, weight: float) -> float:
return from + short_angle_dist(from, to) * weight
static func short_angle_dist(from: float, to: float) -> float:
var difference = fmod(to - from, PI * 2)
return fmod(2 * difference, PI * 2) - difference
# A chunk of code I came up with for more snappy angle lerping
# The 'boost' will increase lerp speed when lerping over longer distances,
# e.g. when trying to turn 180 degrees.
# Make sure you define ROTATE_LERP_SPEED and ROTATE_LERP_SPEED_BOOST
# Also, make sure to replace FROM_ANGLE with your base angle, and TO_ANGLE
# with the angle you want to lerp towards.
# For my player, I used a lerp speed of 0.07, and a speed boost of 0.1
func _process(delta: float) -> void:
var angle = TO_ANGLE
var boost = abs(Utils.short_angle_dist(FROM_ANGLE, angle)) * ROTATE_LERP_SPEED_BOOST
rotation = wrapf(Utils.lerp_angle(FROM_ANGLE, angle, ROTATE_LERP_SPEED + boost), -PI, PI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment