#Getting weird offsets when drawing a line and cant figure out why

39 messages · Page 1 of 1 (latest)

severe spindle
#

I'm trying to do a simple thing, draw a vertical line from a "Player" straight up and its kinda working but there are several issues.

  1. The line is not full length
  2. When the character moves the line gets more offset the more you move
  3. It doesnt originate from player position

Attached to Sprite2D2:

extends Sprite2D


var radius := 400.0
var refrence_vector:Vector2 
# Called when the node enters the scene tree for the first time.

func _draw():
    draw_line(%Player.global_position,refrence_vector, Color(0,1,1), 2, true)

func _ready():
    refrence_vector = Vector2(%Player.global_position.x,%Player.global_position.y+radius)
    

func _process(delta):
    refrence_vector = Vector2(%Player.global_position.x,%Player.global_position.y+radius)
    queue_redraw()
    

I should mention that I have exactly one day of experience.

grave gulch
#

Is the Sprite2D correctly centered on its parent (Player) in the Editor? Before you do any drawing? As it seems the Icon on the Sprite2D is also offset?

Take a look at the position of the Sprite2D in the right hand editor plane.

severe spindle
grave gulch
#

This is not the position but the Up vector, telling other parts of the engine what you expect to be "up".

#

The position part should be in the lower part of the panel.

severe spindle
grave gulch
#

👍

severe spindle
#

Everything is 0,0

grave gulch
#

Looks good, is this the Sprite2D or the CharacterBody2D

#

Or both? 😄

severe spindle
#

both, everything is set to 0,0

grave gulch
#

Okay. So, is the small godot icon above the line part of the Sprite2D and is it all centered before you run the game?

#

I would expect, it's on top of your CharacterBody2D before you start the game?

severe spindle
#

Circle is small collision i added

#

I just have a feeling that i messed up something basic and dont know what

grave gulch
#

Instead of using the global_position did you just try using relative coordinates? Meaning, just think of Vector2(0,0) as the above center of your operation and draw from e.g. (0,0) to (0,20)

#

Never actually calculate anything yourself, but rely on the engine to move the child respectively to the parent (which it normally does)

severe spindle
#
    draw_line(Vector2(0,0),Vector2(0,100), Color(0,1,1), 2, true)

Nothing gets drawn if i do that

#

game dev is hard i guess 🙂

#

This code keeps the line in the same place, attached to smaller icon

grave gulch
#

So, the scene tree you showed only had 4 items in it (World>Blocks>Player>Sprite2D) were is the CollisionShape and Player Sprite coming from? 😄

#

Is the offset still changing when you move your Player, or does it stay in the same relative position to the right?

severe spindle
severe spindle
grave gulch
#

Ahh! Player is a subscene. if you open the Player subscene, and switch the editor to 2D (on top) is everything centered on the red/green world origin lines? or is is offset?

#

if not, click on every node and make sure it's at (0,0) position and save the child scene

#

This might move your character in the "World" scene.

severe spindle
grave gulch
#

okay, so everything is at its origin, that's good. Do you by any chance set the Sprite2D's position from another script. because it's on top you player in the editor, but offset once the game runs. and since you have an icon attached to it which normally rest at the center of the Sprite2D i think it might be moved somewhere else. This is especially true, because our line is drown from (0,0) -> (0, 100) and starts in the middle of the smaller icon.

severe spindle
#

Yes, i removed that part of the code and changed draw_line function, now when i move line streches

draw_line(%Player.global_position, position, Color(0,1,1), 8, true)

#

Starting position

#

After moving

grave gulch
#

yeah, so having figured out the offset issue, the question now becomes, what do you actually want to achieve?

I think draw_line is not in world coordinates, but canvas/screen coordinates.

severe spindle
#

I was just messing around, learning and following some tutorials

grave gulch
#

Is your question resolved? Did you learn what you wanted to learn? 😄

severe spindle
#

i should go read up on different scopes of nodes i guess

severe spindle