How do I get pixel perfect collision to work?

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

I can get it to work when the character is moving right or up but when he moves left or down it collides with walls when it isn’t supposed to.

Here is some code:



extends KinematicBody2D

var areas_in = []
var velocity = Vector2()
const r_vec = Vector2(1, 0)
export var speed = 32 * 2

onready var prev_pos = position



var track = 1




var origin = Vector2()
var dest = Vector2()
var collision_safe = Vector2()

class Entity_Input:
	var move_h = 0.0
	var move_v = 0.0

var ei = Entity_Input.new()





func _ready():
	
	pass
	
func _physics_process(delta):
	
	get_input()
	
	
	
	velocity = Vector2()
	var run = Vector2()
	var spd = 0
	if(abs(ei.move_h) == 1 and abs(ei.move_v) == 1):
		ei.move_v = 0
		run = r_vec.rotated(Vector2(ei.move_h, ei.move_v).angle())
		spd = Vector2(ei.move_h, ei.move_v).length()  * speed
		pass
	else:
		run = r_vec.rotated(Vector2(ei.move_h, ei.move_v).angle())
		spd = Vector2(ei.move_h, ei.move_v).length() * speed
		pass
	if(delta != 0):
		velocity += run * round(spd)

	
	origin = position
	dest = origin + (velocity)
	
	var space_state = get_world_2d().direct_space_state
	
	var global_origin = global_transform.xform(origin)
	var global_dest = global_transform.xform(dest)
	
	var param = Physics2DShapeQueryParameters.new()
	param.collision_layer = self.collision_layer
	param.set_shape($CollisionShape2D.shape)
	param.motion = (dest - origin)
	param.transform = self.transform
	param.exclude = [$CollisionShape2D, self]
	
	var dir = space_state.cast_motion(param)
	
	var vec = dest - origin
	
	collision_safe = origin
	if not dir.empty():
		if(dir[0] != 1):
			print("collision")
			pass
		vec *= dir[0]
		collision_safe = origin + vec
		position += vec
		#This may cause problems
		
		
	

		
		
		position.x = round(position.x)
		position.y = round(position.y)
		
	else:
		print("collision")
		
	if(ei.move_h != 0 or ei.move_v != 0):
		print(str(position))
		print(str(velocity.length()))
		if not dir.empty():
			print(str(dir[1]))
#
	
	



func get_input():
	pass


And here is the wall's code.

extends KinematicBody2D

# class member variables go here, for example:
# var a = 2
# var b = "textvar"

func _ready():
	add_to_group("Wall")
	pass

func _process(delta):
	$Label.text = str(position)
	# Update game logic here.
	pass

enter image description here

:bust_in_silhouette: Reply From: MysteryGM

If you want a object as big as the maze to move inside it you are better off using a old style PacMan grid lookup.
The problem with Vector collisions like Godot uses is that two vectors can never be in the same space, this means your object must be smaller.

*I mean big as the tunnel in the maze.

MysteryGM | 2018-09-06 12:39