Can someone explain what this piece of code is doing? (from the kinematic character demo)

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

Can someone please explain what this line of code is doing exactly? This is the line:
if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOOR_ANGLE_TOLERANCE):

It’s from the Kinematic character demo. I don’t really understand the logic behind it, what it is actually doing. Like, what is the dot(Vector2(0, -1) part for? Thank you for reading.

:bust_in_silhouette: Reply From: eons

Is a vector math thing, with the acos part, you get the angle of the collision normal with the versor (0,-1), the tolerance says when an “angled floor” is considered a “wall” or impossible to walk.

The dot product is a vector operation, the docs have a small explanation:
http://docs.godotengine.org/en/stable/tutorials/vector_math.html?highlight=vectors#dot-product

For more details on the dot product:
http://mathinsight.org/dot_product

:bust_in_silhouette: Reply From: Jatz

if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOORANGLETOLERANCE):

n.dot(Vector2(0, -1)
as I understand it, Vector2.dot basically does this:

func dot(a,b):
    return a.x*b.x+a.y+b*y

multiplying all the axis and then adding their values together

acos
This is basically the inverse of cos
since acos and cos return values are always between 0 and 180 we don’t have to worry about them being less than zero.

acos basically takes the ammount the floor changes and turns it into a radian angle.

rad2deg
This turns the radians into degrees. so that we can measure them against FLOORANGLETOLERANCE

< FLOORANGLETOLERANCE
if the degree angle we are climbing or descending at is less than FLOORANGLETOLERANCE then do this…

:bust_in_silhouette: Reply From: avencherus

The dot(Vector2(0, -1)) part: It’s comparing the angle of that normal’s direction between the direction of a vector that is pointing up in a 2D screen coordinate system.

The dot product numbers will tell you if two vectors are:

  1. Going in the same direction. (Positive number)
  2. Perpendicular to each other. (0)
  3. Heading in the opposite direction of each other. (Negative number)

In this example, as others said, they applied an inverse cosine function to convert that number into an angle in radians, then convert it into degrees.

The result is an angle between this normal’s direction and a vector pointing straight up, then comparing if that angle is beyond a threshold.

It’s also very similar to the angle_to() method inside the vector itself, but it keeps track of a the negative sign. You could use it just the same if you applied absolute value to keep it always positive so it can be compared.

Maybe this is a more readable version for the mind’s eye. (I left the units in radians to cut down on the example):

var up_vector = Vector2(0, -1) # If it's screen coodinates, -1 is up

if(abs(normal.angle_to(up_vector)) < FLOOR_RADIAN_TOLERANCE): #do stuff

https://drive.google.com/file/d/0BxM4Z1C-377uYjBUTDJ6NnJVbmM/view?usp=sharing

the vector is pointing straight up…
thanks for telling about the what the dot product actually does.

Jatz | 2016-12-02 10:31

I see! Thank you very much for taking the time to explain this!

Zero | 2016-12-02 12:57

@Jatz Yeah, it is for screen coordinates, and probably is exactly how it’s being used. I didn’t go looking at the source so I was using the general orientation in math graphing. Makes sense to fix it though. :slight_smile:

@Zero You’re welcome, hope it helps.

avencherus | 2016-12-02 14:03