Diagonal movement causing tilemap painting flaws

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

I was able to make a good assembly of what is happening and killing me for weeks, and I really wanted to know if it was my mistake or just the keys pressed too fast, or the rendering delayed? to deal with the problem, because I’m not finding the culprit for doing so.

This happens when for example I go from diagonal down-rigth to left, and right, then I go back down, I make moves like that, fast, it does not always happen, but enough to disrupt the game.

enter image description here

Movement Script:

 extends KinematicBody2D

var velocity = 150
var direction = Vector2()

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
#-----------MOVIMENT-------------------------------
	direction = Vector2()
	#LEFT
	if Input.is_action_pressed("left"):
		direction += Vector2(-1, 0)
	#RIGHT
	elif Input.is_action_pressed("right"):
		direction += Vector2(1, 0)
	#UṔ
	if Input.is_action_pressed("up"):
		direction += Vector2(0, -1)
	#DOWN
	elif Input.is_action_pressed("down"):
		direction += Vector2(0, 1)

	direction = direction.normalized()*velocity
	move(direction*delta)

Paint Script

enter link description here

Up 1:

I think I found the problem, still not sure if that’s it and also do not know how to solve it:

enter image description here

The postWorld variable is responsible for keeping the position in which the player is in the world (ie just data, not tilemap position or transform position). I start the game by painting the map around the player. In the upper left vertex would be the position (0, 0) in the world, so the player would actually be in the position (32, 32) of the world, and start to instantiate there in front, as you can see in the image below:

Up 2:

I thought the error could be the one said above, but no, because I started posWorld at 0, and to the right and down it decreases by -1, and I still get the fault. But this was clearly wrong, it’s already something that has been fixed, but has more error around here.

Video of what is happening:

http://www.dailymotion.com/video/x6286px

and it’s not too fast, because I tested very slowly and it also happens

Note that direction can be (0,0), and normalizing such a vector is usually undefined behavior. You are lucky, Godot checks this for you^^ https://github.com/godotengine/godot/blob/2.2-legacy/core/math/vector3.h#L352

Other than that, I don’t see anything wrong with your code… and I don’t understand your screenshots at all Oo

Zylann | 2017-09-27 00:17

In the picture, there is a difference in the two, notice here.

enter image description here

this is leaving the map deformed at times. I do not understand why this happens.

PerduGames | 2017-09-27 00:42

I edited the image, showing what is happening.

PerduGames | 2017-09-27 02:54

:bust_in_silhouette: Reply From: PerduGames

I solved the problem so, instead of comparing two rectangles, I just compare vectors from the position of the converted player to the position of the tilemap, this I do by dividing by 32 the size of my cells. And I’ve created a variable to hold the tilemap position (which is pos_tileMap).

    #LEFT
	if int(player.get_position().x/32) < oldPosWorld.x:
			pos_tileMap.x -= 1
			posWorld.x -= 1
			gen_data(Vector2(posWorld.x, posWorld.y), Vector2(pos_tileMap.x, pos_tileMap.y), Vector2(1, size_rectMap))
			oldPosWorld.x = int(player.get_position().x/32)
	#RIGHT
	elif int(player.get_position().x/32) > oldPosWorld.x:
			pos_tileMap.x += 1
			posWorld.x += 1
			gen_data(Vector2(posWorld.x - 1, posWorld.y), Vector2(pos_tileMap.x + 63, pos_tileMap.y), Vector2(1, size_rectMap))
			oldPosWorld.x = int(player.get_position().x/32)
	#UP
	if int(player.get_position().y/32) < oldPosWorld.y:
			pos_tileMap.y -= 1
			posWorld.y -= 1
			gen_data(Vector2(posWorld.x, posWorld.y), Vector2(pos_tileMap.x, pos_tileMap.y), Vector2(size_rectMap, 1))
			oldPosWorld.y = int(player.get_position().y/32)
	#DOWN
	elif int(player.get_position().y/32) > oldPosWorld.y:
			pos_tileMap.y += 1
			posWorld.y += 1
			gen_data(Vector2(posWorld.x, posWorld.y - 1), Vector2(pos_tileMap.x, pos_tileMap.y + 63), Vector2(size_rectMap, 1))
			oldPosWorld.y = int(player.get_position().y/32)