How do I properly align my 2D collisions?

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

So right now, I’m trying to set something up where when the character (KinematicBody2D, CollisionShape2D as child) collides with another KB2D/CS2D, the non-character collision object (the box) physically sticks to the character, like so: https://i.imgur.com/7Bu9Rv6.mp4

But as you can see, at certain approach angles, the box goes to the flip side of the character (and even has the correct spacing).

Along with this, the collisions aren’t always perfect either https://i.imgur.com/wEztHEq.mp4

Sorry that my video isn’t great, but there’s a bit of a gap between the two boxes, when I want it to be flush. There’ll also be times when the box will be further into the character (like a negative gap, the phrase escapes me right now), which I can’t understand.

func reparent(node, poc):
    print(poc - self.position)
    var loc = poc - self.position 
    node.get_parent().remove_child(node)
    self.add_child(node)
    node.set_position(loc.rotated(rotation))

func _physics_process(delta):
    get_input()
    rotation = rotation_speed * delta * rotation_dir
    velocity = move_and_collide(velocity * delta)
    if velocity:
        call_deferred("reparent", velocity.collider.get_child(0), velocity.collider.position)      

This is what I’m currently using for the reparenting the child CS2D from it’s own entity to the character.

I’d appreciate any and all advice! Cheers

:bust_in_silhouette: Reply From: mikemountain

I’m stupid, I changed a ton of things but the major difference was using

get_global_position()

d’oh.