Isometric Collision with move_and_slide

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

Hi Godot Experts,

Total beginner at Godot here.

I’m looking to create a 2D isometric game. I was playing around with the existing 2D isometric demo template and noticed the collision mechanics is a little wonky.

When a KinematicsBody2D (the Troll in the case of this demo) collides with the isometric tile, it will slide no matter the situation–even if the KinematicsBody2D is colliding head on to the tile (wall in this case). I would expect when there is a head on collision (i.e. normals are opposite of one another) there should be no sliding at all.

Looking into this issue a little deeper, I followed the tutorial here (https://www.youtube.com/watch?v=KvSjJ-kdGio) for isometric movement and converted the movement of the Troll to move along isometric axis. For instance:

extends KinematicBody2D

# This is a demo showing how KinematicBody2D
# move_and_slide works.

# Member variables
const MOTION_SPEED = 160 # Pixels/second

func cartesian_to_isometric(cartesian):
	return Vector2(cartesian.x - cartesian.y, (cartesian.x + cartesian.y) / 2)

func _physics_process(delta):
	var motion = Vector2()
	
	if Input.is_action_pressed("move_up"):
		motion += Vector2(-1, -1)
	if Input.is_action_pressed("move_bottom"):
		motion += Vector2(1, 1)
	if Input.is_action_pressed("move_left"):
		motion += Vector2(-1, 1)
	if Input.is_action_pressed("move_right"):
		motion += Vector2(1, -1)
		
	motion = motion * MOTION_SPEED
	motion = cartesian_to_isometric(motion)
	move_and_slide(motion)

I’ve also tried changing the Troll’s collider from a circle collider to a polygon collider that mimic the wall’s collider, but the sliding persists.

Also, even when the movement is normalized, moving when pressing left or right is faster than moving when pressing up and down, so the sliding speed is not consistent even when it happens.

Does anyone know the underlying problem? And if so, can you show me how to fix it?

Thanks for the help. I really appreciate it!

:bust_in_silhouette: Reply From: Arkinum

I’m pretty bad at coding but I think I may have Macgyvered a solution for you until someone with more knowledge comes along.

I think the sliding is just part of how move_and_slide works. You could try move_and_collide instead to see if that gives you the desired behavior (just be sure to lower the speed to something like 2 or your character will fly off the screen, or hit a wall at mach3).

As for the normalizing problem, try this instead.

motion = cartesian_to_isometric(motion)
motion = motion.normalized() * MOTION_SPEED
move_and_slide(motion)