This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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
var var1 = 0
var var2 = 0
var var3 = 0

func ready():
screensize = getviewportrect().size

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)):

func resetmatch():
pos(Vector2(screensize.x * 0.5,screensize.y * 0.5))

func input(event):

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

in Engine by (15 points)

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,124 points)
selected by
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.