Question about gravity and bottom collisions.

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

I have a basic project with a player controlled KinematicBody2D and a Staticbody2D. The player is influenced by the simple gravity procedure given in the Kinematic Character tut:

func handleGravity(delta):

velocity.y += GRAVITY * delta
var motion = velocity * delta
move(motion)

The projects starts with the player falling and landing on the static body. If the player then walks off of the static body they go shooting downward very fast. This is obviously because velocity.y continues to increment even after the player lands. So I need to know when the player lands on something so that I can reset velocity.y and suspend the gravity procedure until the player is falling again. How can I tell which collisions occur on just the BOTTOM of the kinematic body so that I can reset and suspend gravity accordingly. I’m also restricted from adding jumping until I can figure this out (which I’ve been trying to all day).

:bust_in_silhouette: Reply From: volzhs

You can check it by using RayCast2D.
See this tutorial for detail.

I would prefer to use a member function to do this if possible, but I think raycasting is much simpler. Thanks!

Nichefect | 2016-05-16 18:26

:bust_in_silhouette: Reply From: Bojidar Marinov

You can use get_collision_normal() to check if you are currently colliding with the floor, and in this case, just reset the velocity’s y. E.g.

if get_collision_normal().dot(Vector2(0, 1)) > 0.9:
    # the dot product is the amount a vector goes in another vector's direction.
    # 1 means that they both point in the same direction, -1 -> opposite directions.
    # Note that the floor normal is opposite the direction of gravity.
    velocity.y = 0

Could you elaborate because this looks like the way to do it, but I’m a bit naive to linear algebra so I don’t understand exactly how this works. How does the static body have a vector direction at all and why is it opposite of gravity? What exactly is the “normal”?

Nichefect | 2016-05-16 18:35

If the floor normal is the opposite of gravity, wouldn’t the dot product then be -1? So that the conditional would be more like:

if(get_collision_normal().dot(Vector2(0, 1)) < -.9

Nichefect | 2016-05-16 21:26

Because (0, 1) points up.

Bojidar Marinov | 2016-05-17 07:20