How do I define the game area for 4 players in a script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Patryk
:warning: Old Version Published before Godot 3 was released.

For my school project I make a round pong with four players. The game area is defined with a collider and script. In the script, I managed to split the game area to two players. If the ball flies up, the lower player gets the point and when the ball flies down, the top player gets the point. How can I make that for 4 players? How can I split the game area for 4 players?

Can someone help me?

Here is the complete Script:

extends Node2D

onready var ball = get_node(“Ball”)
var screen_size
var var1 = 0
var var2 = 0
var var3 = 0

func _ready():
set_process(true)
screen_size = get_viewport_rect().size
set_process_input(true)

func _process(delta):
if ((ball.get_global_pos().y < 0) or (ball.get_global_pos().y > screen_size.y) or (ball.get_global_pos().x < 0) or (ball.get_global_pos().x > screen_size.x)):
reset_match()

func reset_match():
ball.set_pos(Vector2(screen_size.x * 0.5,screen_size.y * 0.5))
ball.set_angular_velocity(0)

func _input(event):
if event.is_action_pressed(“btn_return”):
OS.get_main_loop().quit()

func _on_PlayArea_body_exit( body ):
var Area = get_node(“PlayArea”)
var distance = body.get_pos() - Area.get_pos()
if(distance.y < 0):
var1 += 1
get_node(“Scores”).get_child(0).set_text(str(var1))
if var1 == 10:
print(“Player 1 win”)
else:
var2 += 1
get_node(“Scores”).get_child(1).set_text(str(var2))
if var2 == 10:
print(“Player 2 win”)

:bust_in_silhouette: Reply From: Warlaan

To sum up the problem: you need to know which player is the one that should have kept the ball from leaving.
In the callback method on_PlayArea_body_exit you read the ball’s position and you know the position of the center of the play area. The vector “distance” points from one to the other, so you just check whether it points up or down (y is positive or negative).

All you need to extend that code to more than 2 players are the vectors from the center of the play area to the start position of each player. Then all you need to do is check which player has the largest dot product with the vector from the center of the play area to the position where the ball left it.

Basically the dot product tells you how similar two directions are, so by checking the dot products with the directions towards the start positions you are checking which start position is closest to the leaving ball.