0 votes

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)
in Engine by (34 points)

1 Answer

0 votes
Best answer

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.

by (4,223 points)
selected by

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?

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.

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.

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.