Moustic_Bonjour
Moustic_Bonjour

Reputation: 3

Why doesn't my coordinate line end where the cursor is?

I'm trying to make end a line on the cursor. But it doesn't work.

Windowed:
Windowed

Full screen:
Full screened

As you can see the blue line does not end on the cursor.

Here's my code for it:

extends Line2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    #gets parent's position
    var parent := get_parent()
    
    #sets 2 points for the line
    add_point(parent.position)
    add_point(parent.position)



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
    #deletes last position (cursor position)
    remove_point(1)
    
    #calculates the new point position
    var mousePos := get_global_mouse_position() #gets mouse position
    var adjust   := get_viewport_rect().size    #gets screen
    var deltaPos  = mousePos - (adjust/2)       #Adjust the coordinate (Point's (0,0) is in the middle of the screen while cursor's is on the top left corner)
    add_point(Vector2(deltaPos))
    print(deltaPos, points[1])

The line is a child of an Area 2d Like this(who has a sprite 2d as a child corresponding to the building in a square. Here is all the build

Each time the function '_process' is called, I print the mousse coordinates, and the end-line point coordinates that is supposed to be on the cursor. You can see on the windowed image that these coordinates are the same

At first the difference was way more visible. Before:

Before

It was caused because the coordinates of the mouse and of the point do not have the same origin (top left corner for the first and center of the screen for the second).

Upvotes: 0

Views: 33

Answers (1)

Theraot
Theraot

Reputation: 40075

The coordinates do not only have a different origin, they can have a different scale and rotation.

In this case (having the Line2D add points to itself based on the mouse position), I suggest to use get_local_mouse_position instead.

Upvotes: 0

Related Questions