Can't get offset and has_point right

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

I’m a starter to Godot and was trying to fix the Pong tutorial. My main problems are the offset and has_point, and probably many others. I already searched in many pages about it, and get_position(), get_offset(), offset, get_global_position() 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, has_point 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)
:bust_in_silhouette: Reply From: Dlean Jeans

offset is for, well… offsetting the graphic of the ball, not storing the position.
Instead of using ball_pos which is assigned to its offset, use ball.position.

I think you don’t got it: both the 5: offset, getoffset(), position, getposition() and getglobalposition() give me the exact same results, and all of them are relative to the ball, not the entire screen/viewport. has_point, and I bet all other things related to position are also bugged in the same way. Is this really a bug or not?

BGamers11 | 2019-06-03 00:13

It’s likely a bug on your end, not an the engine bug.
The fact that all those 5 Vector2 return the same thing might be a coincidence or/and has something to do with how you set up the scene.
get_offset() and offset would give you the same value, same with get_position() and position.
As I said, don’t use offset here and you don’t even need a ball_pos variable, just use ball.position directly.

I’d created dozen of Pong games myself, finished and half-finished. Here’s one if you wanna check: Juicy Pong on itch.io.
IIRC, the ball and the paddles are likelyKinematicBody2D not Sprite and the (hidden) goals are Area2D not Rect2D.

Dlean Jeans | 2019-06-03 10:43

I tried your answer, and it still don’t works right. I mean, why do I can’t get globalposition and only can get the sprite being the origin for itself. Also getrect should atleast work right, but for some reason the pad only detected the ball collision when the ball was very forward of it, like a really late collision. I really wanted to just fix this someway, but to do that it’s so annoying to add “divide by 2” in everything I do. Also is using Area2D really needed? I just wanna code based in the position, not necessarily a collisionobject.

BGamers11 | 2019-06-03 17:12

I think I fixed the issue in the worst way possible, but it occupied a lot of space and time in the script. Also looks like the offset back to work, so I could use it and sum with it’s position.

BGamers11 | 2019-06-04 03:02

I find using Area2D really time-saving, it involves no math, just drag-and-drop in the editor. The ball gets inside it, you get a signal. If the ball is detected too late or too soon just drag it around.

Dlean Jeans | 2019-06-04 06:09