0 votes
in Engine by (244 points)

1 Answer

+2 votes

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)

by (5,274 points)

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).

Thank you very much!

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.