What is the actual formula behind slide function in Vector2 class?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pospathos
:warning: Old Version Published before Godot 3 was released.
:bust_in_silhouette: Reply From: avencherus

In pseudocode it’s a method/function inside a vector that looks something like this:

.slide(vector) {
  return vector - this * dot(this, vector);

So it’s also important that in most cases you want the vector that is calling the method to be normalized (unit length), otherwise that dot product is going to be pretty huge.

Something like: norm.slide(velocity)

And for the curious, here it is in non-pseudo code: https://github.com/godotengine/godot/blob/4a4f247/core/math/math_2d.cpp#L279-L282 (but it’s indeed what avencherus described).

Akien | 2016-08-16 11:31

Thank you very much!

pospathos | 2017-02-04 19:33

The vector calling the method doesn’t need to be normalized. The parameter is the normal and the vector will slide on its plane.

horizontal_velocity = velocity.slide(-gravity.normalized())

The normal is the up direction of the world relative to gravity which creates a horizontal plane. Velocity will be restrained by this plane so the code returns only the horizontal factor of the vector.

hammeron | 2019-03-24 22:02