Prevent "drag" against StaticBody2D when moving along the surface

XascoriaXascoria Posts: 6Member
in 2D

So I'm not entirely sure what settings this is considered as, allow me to explain.

For the example, I have a StaticBody2D located directly at the North of my KinematicBody2D (The Player), I'm using move_and_collide for the player's movement. If I walk straight up, I will get stopped when I hit the StaticBody2D, which is the expected behavior. If I then stop moving straight up but instead walk left or right. I will move to the left/right with the normal speed, which is also expected. The question I'm asking is that after I get stopped by the wall, if I continue to move_and_collide to both top and left, instead of moving left at a normal speed, the KinematicBody2D will slow down considerably and seems to get "dragged" across the surface of the StaticBody2D, and what I wanted is that it would simply ignore the upward force and just move to the left normally. Putting fiction to 0 doesn't change anything, so anyone knows how can I fix this?

Thanks for your time.

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,655
    Accepted Answer

    While I'm not sure, my guess on what is happening is that your velocity is pointing to the upper left corner, rather than two velocities (one up and one left). In this case, because the velocities are combined, the amount of force hitting the StaticBody2D is lost via the move_and_collide function, leaving less than half of the expected velocity in the direction that does not collide (if that makes sense).

    To fix this, I would call move_and_collide separately for each axis you want to move in. For example (untested):

    # I will assume the velocity is stored in a variable called velocity
    if (velocity.x != 0):
        # Only move on the X-axis
        move_and_collide(velocity * Vector2(1, 0), Vector2.UP)
    if (velocity.y != 0):
        # Only move on the Y-axis
        move_and_collide(velocity * Vector2(0, 1), Vector2.UP
    

    Then because the movement is separated per axis, collision on one axis should not affect the other, giving the desired results.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin
    Accepted Answer

    While I'm not sure, my guess on what is happening is that your velocity is pointing to the upper left corner, rather than two velocities (one up and one left). In this case, because the velocities are combined, the amount of force hitting the StaticBody2D is lost via the move_and_collide function, leaving less than half of the expected velocity in the direction that does not collide (if that makes sense).

    To fix this, I would call move_and_collide separately for each axis you want to move in. For example (untested):

    # I will assume the velocity is stored in a variable called velocity
    if (velocity.x != 0):
        # Only move on the X-axis
        move_and_collide(velocity * Vector2(1, 0), Vector2.UP)
    if (velocity.y != 0):
        # Only move on the Y-axis
        move_and_collide(velocity * Vector2(0, 1), Vector2.UP
    

    Then because the movement is separated per axis, collision on one axis should not affect the other, giving the desired results.

  • XascoriaXascoria Posts: 6Member

    Thanks a lot for the response Twisted, doing that solved the problem!

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin
    edited June 19

    @Xascoria said:
    Thanks a lot for the response Twisted, doing that solved the problem!

    Awesome! I'm glad that was able to fix it. :+1:

    (Edit: Changed discussion to a question and marked the reply as the answer)

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file