0 votes

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 = getnode("Ball")
var screen
size
var var1 = 0
var var2 = 0
var var3 = 0

func ready():
set
process(true)
screensize = getviewportrect().size
set
process_input(true)

func process(delta):
if ((ball.get
globalpos().y < 0) or (ball.getglobalpos().y > screensize.y) or (ball.getglobalpos().x < 0) or (ball.getglobalpos().x > screensize.x)):
reset
match()

func resetmatch():
ball.set
pos(Vector2(screensize.x * 0.5,screensize.y * 0.5))
ball.setangularvelocity(0)

func input(event):
if event.is
actionpressed("btnreturn"):
OS.getmainloop().quit()

func onPlayAreabodyexit( body ):
var Area = getnode("PlayArea")
var distance = body.get
pos() - Area.getpos()
if(distance.y < 0):
var1 += 1
get
node("Scores").getchild(0).settext(str(var1))
if var1 == 10:
print("Player 1 win")
else:
var2 += 1
getnode("Scores").getchild(1).set_text(str(var2))
if var2 == 10:
print("Player 2 win")

in Engine

## 1 Answer

+1 vote
Best answer

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.

by (1,114 points)
selected by