Could someone help me point out why some tiles are dissapearing? Using draw() for this.

Godot Version

Godot 4.5.stable

Question

Im creating my own tile system (because i need greater manipulation over each tile) so i quickly coded this draw function that draws tiles that loop over when they reach the edge of the screen so it creates seamless endless tiles all around. When i hit a specific position in the camera position a row/column of tiles (when the Y is applied) dissapears entirely. Is it a problem with the math? Or is something else happening here.

extends Node2D

const ICON = preload("uid://dlbmoqm541v64") #godot icon

const TILE_SIZE: float = 128

@onready var cam: Camera2D = $"../Cam"

var LevelData = []

func _draw() -> void:
	var camPos = cam.position
	var frameSize = get_viewport().size
	var fillRange = Vector2(1+ceil(frameSize.x/TILE_SIZE),ceil(frameSize.y/TILE_SIZE)) #gets the maximum ammount of tiles that will fill the screen bin both x and y 
	print(fillRange)
	for x in range(fillRange.x):
		for y in range(fillRange.y):
			var alignX = (-TILE_SIZE/2)+(frameSize.x/2)-round(((x*TILE_SIZE)-camPos.x)/TILE_SIZE)*TILE_SIZE
			var alignY = 0 #this is left at 0 because ill copy the X equation above once its refined
			draw_texture(ICON, Vector2(alignX,alignY))

func _ready() -> void:
	for i in range(100):
		LevelData.append(randi_range(0,1)) #right now not used for anything
	print(LevelData)
	
func _process(_delta: float) -> void:
	queue_redraw()

Video showing this: (couldnt upload since im a new account) (and apologies if its low qual, keeping the file size low)

Hi!

Could it be that round() rounds into the wrong direction and one of your columns gets overlayed by another?

I mean in this part:

round(((x*TILE_SIZE)-camPos.x)/TILE_SIZE)*TILE_SIZE

Does this happen for the rows as well?

1 Like

i tested for this by making the tiles transparent, and upon finding a spot where a part dissapears, no overlap in sight. so ive no clue

also yeah ill try using ceiling or floor instead, ill report back to you if it helps

ok changing it to ceiling and removing the offset seems to have completely fixed it
(frameSize.x/2)-ceil(((x*TILE_SIZE)-camPos.x)/TILE_SIZE)*TILE_SIZE

thank you a ton :folded_hands:

1 Like

Maybe try giving them color for debugging, by altering the modulate property for example. That might make things visible which will not appear with transparency.

You are welcome :four_leaf_clover: