How to match a drawing to location of parent node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tonkincode

I’ve been experimenting with using the _draw() function. The plan was to use said function instead of a sprite. However problems have arisen when it comes to matching the location of a rectangle I’ve drawn to the location of its parent node (and thus the CollisionShape2D). Even when I set them to the exact same vector2 the parent node seems to lag behind the shape. I have used a normal sprite in the below gif to represent the parent node’s location (please ignore the second sprite which pops in briefly).

enter image description here

extends Area2D


var p1y = 0 #controls the y axis for this paddle
var p1x = 100
onready var main = get_node("../../Main") #main stores global variables such as gamespeed

func _ready():
	pass

func _draw():
	draw_rect(Rect2 ( self.position, Vector2(20,100) ), Color( 0, 1, 1, 1 ), true)

func _process(delta):
	
	if (Input.is_action_pressed("p1_up") and (p1y >= 0)): #move up
		p1y -= (100*main.getGameSpeed())*delta #move multiplied by gamespeed. We use delta to keep it consistent regardless of framerate.
		update()
		
	elif (Input.is_action_pressed("p1_down") and (p1y <= 500)):
		p1y += (100*main.getGameSpeed())*delta
		update()
		
	self.position = (Vector2(p1x, p1y))

I’m at a bit of a loss in trying to understand what’s going on here. Perhaps _process and _draw work on two different systems? Any help would be greatly appreciated. Thanks in advance.

Note: I edited your post to fix the code formatting. In the future, please use 4 spaces in front of code lines, or press the “Code Sample” formatting button on top of the posting window.

kidscancode | 2019-08-24 14:28

:bust_in_silhouette: Reply From: kidscancode

_draw() positions are in the local space. To draw it at the sprite’s origin:

draw_rect(Rect2 (Vector2.ZERO, Vector2(20,100) ), Color( 0, 1, 1, 1 ), true)