What Happens When Large Values Are X and Y Coordinates of Points in a Line2D

Godot Version

v4.6.2.stable.steam [71f334935]

Question

I’m working on a feature in a game, and I did my usual copy-paste into ChatGPT to see if the code was understandable. Here’s the code:

extends Node2D

@onready var positive_line_2d: Line2D = $"Positive Line2D"
@onready var negative_line_2d: Line2D = $"Negative Line2D"

var x_pos: float = 0.0
var x_neg: float = 0.0

var expression_string: String
var var_names: Array = ["x", "e"]
var var_values: Array = [0.0, exp(1)]
var evaluator = Expression.new()
var x_idx: int = var_names.find("x")

func evaluate_expression() -> float:
	var result = evaluator.execute(var_values)
	if evaluator.has_execute_failed() or not (typeof(result) in [TYPE_FLOAT, TYPE_INT]):
		push_error("Expression evaluation failed")
		return 0.0
	return float(result)
	


func _ready() -> void:
	expression_string = self.get_meta("expression")
	expression_string = expression_string.replace("^", "**")
	
	var err = evaluator.parse(expression_string, var_names)
	if err != OK:
		push_error("Expression parsing failed")
		self.queue_free()
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	var_values[x_idx] = x_pos
	positive_line_2d.add_point(Vector2(x_pos, -evaluate_expression()))
	
	var_values[x_idx] = x_neg
	negative_line_2d.add_point(Vector2(x_neg, -evaluate_expression()))
	
	x_pos += delta * 10
	x_neg -= delta * 10
	

ChatGPT said that I need to clamp the domain, which I will implement later in the game. It also said I need to clamp the range to ensure rendering stability, which is what I’m confused about. Do large X and Y coordinates in a Line2D cause rendering problems?

I know they probably cause lag, trying to calculate large values, but I suggested I could clamp the value to [-10000, 10000]. My game’s viewport is 115.28x64.8, so this wouldn’t cause any clipping problems, but ChatGPT said that that wouldn’t reflect the real, unclamped graph. My understanding of maths is that there shouldn’t be a noticeable change.

What happens with large coordinates in a Line2D?

Nothing, as long as you’re inside single precision floating point range. You may lose some precision when very far from origin. At one million order of magnitude, your precision drops to approximately 0.0625, and at billion it drops quite a bit more - to about 64. You can try this by yourself by typing the numbers into Line2D’s packed array. If you type anything between 1000000000.0 and 1000000064.0, it will always be “rounded” to one of those two values. Same for the next increment of 64 etc…

How large are the numbers and why do you need large numbers?

1 Like

When graphing “expression“, there might be an asymptote, where the y-coordinate increases to ±infinity. I’d obviously have to clamp it, but I was wondering if values relatively smaller, like 10000, could have any effect.

I don’t have any idea how Line2d works in the backend, and what happens when rendering values not on the viewport.

Line is clipped when rendering, same as any other geometry. There shouldn’t be any problems, precision or otherwise if you keep it under 1.000.000-ish order of magnitude.

2 Likes