0 votes

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.

in Engine by (93 points)
edited by

3 Answers

+3 votes
Best answer

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


by (5,242 points)
edited by

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

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

@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. ^_^

@Zero You're welcome, hope it helps.

+1 vote

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:

For more details on the dot product:

by (7,894 points)
0 votes

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

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.

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

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

by (124 points)
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 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 webmaster@godotengine.org with your username.