I'm a starter to Godot and was trying to fix the Pong tutorial. My main problems are the offset and haspoint, and probably many others. I already searched in many pages about it, and getposition(), getoffset(), offset, getglobalposition() are not working right. For example, everytime I print them, it always gives me wrong values. If the ball is centered, it gives 0,0 and no way I can get the real offset right, that means that if the ball is in the end of the screen, it thinks that it's only at half of the screen, because of the ball starting in 0,0 for itself besides the real offset, which I can't get. Also, haspoint is having the same problem, and only detection anything that is foward it, so, basically: everything is half-screen forward(half only to the x I think). My code:
extends Node2D
var ball_speed = 80
var ball_direction = Vector2(1.0, 0.0)
var ball
var ball_pos
var left_pad
var right_pad
var pad_size
var screen_size
const START_BALL_POS = Vector2(320, 188)
var left_rect
var right_rect
func _ready():
ball = get_node("ball")
ball_pos = ball.get_offset()
left_pad = get_node("left")
right_pad = get_node("right")
pad_size = left_pad.texture.get_size()
screen_size = get_viewport().get_size()
left_rect = left_pad.get_rect()
right_rect = right_pad.get_rect()
set_process(true)
func _process(delta):
ball_pos += ball_speed * ball_direction * delta
if ball_pos.x < 0 or ball_pos.x > screen_size.x:
ball_pos = START_BALL_POS;
ball_speed = 80
ball_direction = Vector2(1.0, 0.0)
if left_rect.has_point(ball_pos) or right_rect.has_point(ball_pos):
print("has_point")
ball.set_position(ball_pos)