Why do collisions on player bullets behave weird and don't work half the time(seemingly at random)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lotos

So basically the collisions are kinda f*cked for some reason and don’t work half the time and as far as I could see the only pattern is that the first couple bullets always worked and after that it was(as far as i could see more up to chance) random but it did work more often when you moved up and down. I have tried removing, code double checked everything asked on discord and nothing worked. I think its not associated with the script but something else(maybe even an engine bug) I uploaded the project to MegaUp. THE LINK: https://megaup.net/Vxip/Space_shooter.rar. But in case its just a matter with the code and I am stupid here it is:
THE BULLET:

extends KinematicBody2D

export var speed = 0.0

func _physics_process(delta):
	move_and_slide(Vector2(speed, 0))
	for i in get_slide_count():
		var collision = get_slide_collision(i)
		if collision.collider.get_collision_layer_bit(2):
			collision.collider.queue_free()
			queue_free()
			


func _on_bomb_timeout():
	queue_free()

THE ENEMY:

extends KinematicBody2D

export var HP = 0


func _process(delta):
	move_and_slide(Vector2.ZERO)
	for i in get_slide_count():
		var collision = get_slide_collision(i)
		HP -= 1
		collision.collider.queue_free()
		if HP <= 0:
			queue_free()

Maybe it has to do with the callback functions that are being used? The bullet script uses _physics_process() while the enemy script uses _process(). Since collision detection is calculated in the _physics_process() function, change the callback function in the enemy script to _physics_process().

Ertain | 2021-02-13 19:59

Sadly it didnt work. But I doubt this would do anything since they are not associated.

lotos | 2021-02-13 20:04

I’m not sure I’m understanding your question, but it seems like you need to enable continuous collision detection. This enabled more precise collisions at the cost of a bit of performance.

andersmmg | 2021-02-14 00:32

:bust_in_silhouette: Reply From: Lopy

When two things move to touch one another, you do not know which one will detect the collision.

For instance, say you have two moving entities aligned and going in the same direction, but the front one is slower. Every time the back one moves, it collides with the front one, but when the front one moves, it stops colliding:

Initial:

A-->       B->

A Moves and collides with B:

       A-->B->

B moves without colliding with A:

       A-->   B->

To avoid that, you can make each moving Nodes implement a function, say on_collision(collision, with), and whenever one collides, they call their own function, and then the one of whoever they collided with:

for i in get_slide_count():
    var collision = get_slide_collision(i)
    on_collision(collision, collision.collider)
    if collision.collider.has_method(on_collision):
         collision.collider.on_collision(collision, self)

Physics related functions like move_and_… should always be called inside a _physics_process() or a function called inside one.