0 votes

I was following the godot pong tutorial and a few errors came up. I fixed the first few but this I can't fiigure out. Someone please help.

Code:

func process(delta):
var ball
pos = getnode("ball").getpositioninparent()
var leftrect = Rect2(getnode("left").getpositioninparent() - padsize / 2, padsize)
var right
rect = Rect2(getnode("left").getpositioninparent() - padsize / 2, padsize)

ball_pos += direction * ball_speed * delta

if((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
    direction.y = -direction.y 

if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
    direction.x = -direction.x
    ball_speed *= 1.1
    direction.y = randf() * 2.0 - 1
    direction = direction.normalized()

if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
    ball_pos = screen_size * 0.5 #ball goes to screen center
    ball_speed = 80
    direction = Vector2(-1, 0)

get_node("ball").set_pos(ball_pos)

#move left pad
var left_pos = get_node("left").get_pos()

if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
    left_pos.y += -PAD_SPEED * delta
if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
    left_pos.y += PAD_SPEED * delta

get_node("left").set_pos(left_pos)

#move right pad
var right_pos = get_node("right").get_pos()

if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
    right_pos.y += -PAD_SPEED * delta
if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
    right_pos.y += PAD_SPEED * delta

get_node("right").set_pos(right_pos)
Godot version 3.2.3-stable
in Engine by (28 points)

1 Answer

+1 vote

So you're trying to subtract a Vector2 and int values in these lines
getnode("left").getpositioninparent() - padsize / 2

Which is not allowed or proper calculation for that matter

by (4,972 points)

This is the correct answer.

This might be the calculation you were going for:

var vector_pad_size = Vector2.ONE * pad_size
var left_rect = Rect2(get_node("left").get_position_in_parent() - (vector_pad_size / 2), vector_pad_size)
var right_rect = Rect2(get_node("left").get_position_in_parent() - (vector_pad_size / 2), vector_pad_size)

It doesn't make any sense mathematically to do Vector2(5,5) - 2 but it does make sense to do: Vector2(5,5) - (Vector2(1,1) * 2) which equals Vector2(3,3)

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 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 webmaster@godotengine.org with your username.