0 votes

Hi all,

First of all, I cannot explain myself well in English so please pardon me if I have difficulty delivering my inquiries properly and second, I just started with Godot engine.

here's the script

extends RigidBody2D

var bump

var WALK_ACCEL = 300

var airborne_time = 1e20
var floor_h_velocity = 0.0

func _integrate_forces(s):

    var lv = s.get_linear_velocity()
    var step = s.get_step()

    var wall_found = false

    lv.x -= floor_h_velocity
    floor_h_velocity = 0.0

    var found_floor = false
    var floor_index = -1

    var coliBodies = get_colliding_bodies()
    if coliBodies.size() > 1:
    #   print(coliBodies[0].get_name())
    #   print(coliBodies.size())
        if (coliBodies[1].get_name() == "wall" or coliBodies[1].get_name() == "wall1" ):
            bump = "bump"
            wall_found = true

    for x in range(s.get_contact_count()):
        var ci = s.get_contact_local_normal(x)
        if (ci.dot(Vector2(0, -1)) > 0.6):
            found_floor = true
            floor_index = x

    if (bump == "bump"):
        if (get_node("charsprite").is_flipped_h()):
            bump = ""
            bump = ""

    if (found_floor):
        airborne_time = 0.0
        airborne_time += step

    var on_floor = airborne_time < MAX_FLOOR_AIRBORNE_TIME
    print ("floor: "+str(on_floor))

    if (on_floor):
        if (get_node("charsprite").is_flipped_h()):
            lv.x -= WALK_ACCEL*step
            lv.x += WALK_ACCEL*step

    lv += s.get_total_gravity()*step

func _ready():
    # Called every time the node is added to the scene.
    # Initialization here

Now, my problem is every time the object collides against a wall, the object sprite gets shaky before it moves to an opposite direction. Is there anyway to fix this? I would appreciate it if solutions would be made. Thank you in advance.

in Engine by (44 points)

Try to isolate problems, null all the air checks and start clearing all the velocities, maybe there is a remanent somewhere you forgot to null when turning around.

Enable collision debug to see the contacts too.

1 Answer

+1 vote

OK, I think I have a solution. In your _integrate_forces function, remove the wall colllision test and add the following code in the loop below:

    for x in range(s.get_contact_count()):
        var ci = s.get_contact_local_normal(x)
        if (ci.dot(Vector2(0, -1)) > 0.6):
            found_floor = true
            floor_index = x
        elif (ci.dot(Vector2(1, 0)) < 0) != get_node("charsprite").is_flipped_h():
            bump = "bump"
            wall_found = true

This compares the sprite's direction to the non-ground normals, and if they are opposite, the "bump" occurs. Make sure you have enough "Contacts Reported" in your RigidBody or the wall could be ignored.

by (16 points)

Works like a charm! Thank you very much Rod :) Appreciate it.

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 webmaster@godotengine.org with your username.