How Do I Constrain All 2D Positional Coordinates To Be A Whole Integer Instead of a Floating Point (Decimal) Value?

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

I would like to make sure that all 2D images positional coordinates in the Godot UI and during animation are constrained to only whole number values.

I am new to GDScript, and I suspect it would need to be done this way. Anyone willing to point me in the right direction to figure this out?

I am suspecting I need to apply a round() fucntion on something?

As always greatly appreciated. If I could group hug all of Godot I would <3

UPDATE 1: The following doesn’t work but this is what I currently have…

# root node for scene
extends Control

# learned somewhere that using set_fixed_process will execute things every frame
func _ready():

# set the current position to a whole number instead of a float. not sure if I need delta.
func _fixed_process(delta):
	set_pos(int(round(get_pos())) * delta)

davidpgil | 2017-10-12 05:30

Have you tried enabling 2d pixel snap in project settings?

aozasori | 2017-10-12 06:23

Thank you for your suggestion. I have already set the 2D pixel snap, and it works fine.

My goal is to make it so animation and positional data in Godot don’t use floating point values. For example, normally when making an object move from left to right starting from position 1, the motion would proceed to increment to 1.1, 1.2, 1.3, etc. I want to change the behavior to so the object will move from left to right such that the values will increment from 1 to 2, to 3, and so on. No floating point numbers at all.

I know I probably need to use round() in GDScript, but I am unfamilar with how to get the position of a sprite and change it in such a way that all animation interpolates in whole number integer values.

davidpgil | 2017-10-12 10:34

Regarding in-GUI 2D snapping while mouse dragging a sprite, it doesn’t seem to work. I definitely have it enabled. Is the expected pixel snapping supposed to disable the possibility of floating point placement? Do I need to hold down a key or something to use it?

Regarding rounding off the floating point values during every frame of animation, I whipped up something that seems to work. I need to test in more deeply though…

extends AnimatedSprite

# get position of the sprite 
onready var sprite = get_node(".")
onready var xy = sprite.get_pos()

# enable every frame processing
func _ready():

# round off and typecast any floating point values into integers
func _fixed_process(delta):
	sprite.set_pos(Vector2(int(round(xy[0])), int(round(xy[1]))))

davidpgil | 2017-10-13 01:52

Sorry, the option in game settings is for the renderer. There is also a use pixel snap option for the scene editor. It completely slipped my mind!

aozasori | 2017-10-13 04:23

Nailed it! Thanks very much!

davidpgil | 2017-10-13 04:48

In case anyone has the same question, it also solved my problems related to perfect pixel collision using KinematicBody2D / TileMaps:

onready var player = get_node(".")
func _process(delta):
	var xy = player.position
	player.position = (Vector2(int(round(xy[0])), int(round(xy[1]))))

thank you!

Tiel | 2020-09-25 20:28