Sprite shaky upon collision

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

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 MAX_FLOOR_AIRBORNE_TIME = 0.15

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
			print(bump)
	
	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()):
			get_node("charsprite").set_flip_h(false)
			bump = ""
		else:
			get_node("charsprite").set_flip_h(true)
			bump = ""
	
	if (found_floor):
		airborne_time = 0.0
	else:
		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
		else:
			lv.x += WALK_ACCEL*step
	
	lv += s.get_total_gravity()*step
	s.set_linear_velocity(lv)

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

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.

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.

eons | 2017-02-24 15:30

:bust_in_silhouette: Reply From: Rodolphe Suescun

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
		    print("bump")

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.

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

ddarkmoto | 2017-03-02 15:01