remaking Pong but the AI cannot locate the ball's Y coordinates correctly
Asked Answered
I

4

0

I'm following a detailed YouTube tutorial on remaking Pong in Godot 3.0 but I have ran into an issue regarding the way the AI opponent reacts to the incoming ball, what's happening is that the opponent can track the balls' location but for some strange reason the opponent always reacts to the incoming ball as if it's more bellow it's actual location, here's a video that shows my issue , as you can see the opponent always moves way more down than it should which makes it impossible for it to hit the ball most of the time, the only time it can hit the ball correctly is when the ball is already heading down to the corner, so it just so happens to meet up with it

I have followed the tutorial closely and downloaded the source code for the Godot project and tried copy and pasting the opponent's (and even the ball's code) to my project and i double checked the variable names and the way scene tree is organized yet the issue is still present

here's the current code for the AI opponent :

extends KinematicBody2D

var speed = 250 #tried changing this but the issue still occures
var ball

func _ready():
    ball = get_parent().find_node("ball")

func _physics_process(delta):
    move_and_slide(Vector2(0,get_opponent_direction()) * speed) #i tried also multiplying the speed by delta but it doesn't fix it

#the issue is in this function probably...
func get_opponent_direction(): 
    if abs(ball.position.y - position.y) > 25: #i tried increasing\decreasing this but the issue's still present
        if ball.position.y > position.y: return 1
        else: return -1
    else: return 0

here's the ball's code also (even though i don't think it has anything to do with it)

extends KinematicBody2D

var speed = 400 #tried increasing\decreasing the speed but the issue is still present
var velocity = Vector2.ZERO

func _ready():
    randomize()
    velocity.x = [-1,1][randi() % 2]
    velocity.y = [-0.8,0.8][randi() % 2]

func _physics_process(delta):
    var collision_object = move_and_collide(velocity * speed * delta)
    if collision_object:
        velocity = velocity.bounce(collision_object.normal)

i even checked if the project window size is correct and it is, i also double checked that me and the tutorial are both using Godot 3.2, it seems to me that the issue is that the opponent gets the coordinates for the ball but for some reason it increases the Y axis for the coordinates, this results in the opponent seeing the ball below it's actual location, so yeah something with the Y coordinates...

Importunate answered 26/2, 2024 at 13:32 Comment(0)
H
1

Importunate To me it sounds as if the ball and the AI opponent might have a different coordinate system, or just a different origin for their coordinates. For example, maybe the AI position origin is the center of it's sprite and the ball's is at the top left or something. Maybe step into the get_opponent_direction function with the debugger (click on the left margin in the script editor to set a breakpoint) and check if the ball y position and the ai y position are as expected.

Hoes answered 26/2, 2024 at 14:7 Comment(0)
P
1

Importunate Use global_position property instead of position for both. Make sure that pivot points for both are at centers of their respective visual representations.

Petulah answered 26/2, 2024 at 14:15 Comment(0)
I
1

Hoes OH MY GOD THANK YOU SO MUCH YOU WERE RIGHT !!!!

i looked at the opponent AI scene on it's own and i realized that the collision shape and the sprite nodes were far away from the area2D node which is at the origin point in the XY axis, i took dragged them both to the XY origin point and now everything works right !!!

Importunate answered 26/2, 2024 at 14:31 Comment(0)
I
0

Petulah thanks for the reply, i figured it out with another user's help !

Importunate answered 26/2, 2024 at 14:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.