2D Coordinate Systems & Transforms proplem

Godot Version

4.7

Question

hi, I wanna to make an aim marker for my top down game I made one it is an area2D at first I make it as a control node but the I analyzed that I wanna the aim mark to be active with the game world not just an UI

so I make it as Area2D to mottring with the enemy’s but I have a proplen the aim it moving very well but it can exit fron the viewport I wnna to make a limets for the aim I read some documents and trying with AI but it won’t work, I’m trying to get the viewport position in to the game position, but it is not perfect

dose any one know the way to do it

extends Area2D 
class_name Aim

@export var aim_joustick : _VirtualJoystick

var SCREEN_SIZE 
var inverse_transform 

var min_world : Vector2 
var max_world : Vector2

var half_texture : float  = 16.0

signal aim_find_enemy


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	SCREEN_SIZE = get_viewport().get_visible_rect()
	
	
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	
	if aim_joustick and aim_joustick.is_pressed:
		global_position += aim_joustick.output * 300 * delta
		
		
		inverse_transform = get_canvas_transform().affine_inverse()
		
		min_world = inverse_transform * SCREEN_SIZE.position
		max_world = inverse_transform * SCREEN_SIZE.end
		
		global_position.x = clamp(global_position.x,min_world.x + half_texture,max_world.x - half_texture)
		global_position.y = clamp(global_position.y,min_world.y - half_texture,max_world.y + half_texture)


func _on_body_entered(body: Node2D) -> void:
	print(1)
	if body.is_in_group("enemy"):
		print(2)
		aim_find_enemy.emit(body)

	pass # Replace with function body.

How it is not perfect?

Note that get_canvas_transform() and get_viewport().get_canvas_transform() are different transforms.