Loading...
Logo
Processing Forum
This is the base code for a four way pong game my group and I are trying to create. So that four different players can play over a network. What we need is for the ball to bounce off of all four paddles. I know there are collision detection examples on Processing.org but for the sake of this program I'm wondering is it possible to create collision detection using color?

So for example: When white (255) hits grey (100) go the other direction.

Is there a simple way of implementing this?

// CLIENT
/* This sketch includes update on gamestates as well as the boolean
intersection to read whether the ball has hit the object.
Needs to be added: the ball hitting the player... if successful, it
should read in println "hit"and score will be added.

*/

import processing.net.*;

int gameState;
int INTRO = 0;
int GAME = 1;
int GAME_OVER = 2;

float ballColor = 100;
float paddleColor = 255;

PFont titleFont = createFont("SansSerif", 34);
PFont basicFont = createFont("SansSerif", 12);

Client client;

Player player1;
Player player2;
Player player3;
Player player4;

/* int i;
Ball [] ball; */

Ball ball;

String input;
int data[];

//scoring
//int score = 0;
//PFont font;

//define for boolean hit
float[] playerX;
Player[] playerCheck;

void setup()
{
 
//scoring
//  font = loadFont("Serif-48.vlw");
  size(600, 600);
  smooth();
//  background(204);
//  stroke(0);

  player1 = new Player(50, 250, 25, 70);
  player2 = new Player(500, 250, 25, 70);
  player3 = new Player(250, 500, 70, 25);
  player4 = new Player(250, 50, 70, 25);
 
//  ball = new Ball[1];
  ball = new Ball(50, 50);

  // Connect to the server's IP address and port
  //client = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
  client = new Client(this, "99.236.62.214", 12345); // Replace with your server's IP and port

noStroke();

/*BALL
  frameRate(30);
  smooth();
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/2; */

}

void draw() {
  if(gameState == INTRO) {
    intro();
  }
  else if (gameState == GAME) {
    game();
  }
  else if (gameState == GAME_OVER) {
    game_over();
  } 
}

void intro () {
  background(32, 32, 32);
  fill(255);
  textFont(titleFont);
  textAlign(CENTER);
  text("Pong", width/2, height/2);
 
  textFont(basicFont);
  text("Click anywhere to begin!", width/2, height/3.5*2);
}
 
void game() {
  background (32, 32, 32);
// scoring
//  textFont(font,100);
//  text(score, 275, 275);
//  fill(0);
 
  player1.display();
  player2.display();
  player3.display();
  player4.display();
  if (mousePressed == true &&
    (mouseX < player1.xPos + 50 && mouseX > player1.xPos) &&
    (mouseY < player1.yPos + 100 && mouseY > player1.yPos))
  {
    player1.yPos = mouseY - 50;
    //update the other clients
    client.write(1 + " " + player1.xPos + " " + player1.yPos + "\n");
  }
  if (mousePressed == true &&
    (mouseX < player2.xPos + 50 && mouseX > player2.xPos) &&
    (mouseY < player2.yPos + 100 && mouseY > player2.yPos))
  {
    player2.yPos = mouseY - 50;
    //update the other clients
    client.write(2 + " " + player2.xPos + " " + player2.yPos + "\n");
  }
  // problem only moving left
  if (mousePressed == true &&
    (mouseX < player3.xPos + 50 && mouseX > player3.xPos) &&
    (mouseY < player3.yPos + 100 && mouseY > player3.yPos))
  {
    player3.xPos = mouseX - 25;
    //update the other clients
    client.write(3 + " " + player3.xPos + " " + player3.yPos + "\n");
  }
  // problem only moving left
  if (mousePressed == true &&
    (mouseX < player4.xPos + 50 && mouseX > player4.xPos) &&
    (mouseY < player4.yPos + 100 && mouseY > player4.yPos))
  {
    player4.xPos = mouseX - 25;
    //update the other clients
    client.write(4 + " " + player4.xPos + " " + player4.yPos + "\n");
  }
 
 
  //Here we would need to include else if (mousePressed == true && (CO-ORDINATES FOR OTHER PADDLE)
 
  ball.display();

//  for (int k = 0; k < i; k++) {
//  ball[k].display();
//  }

  // Receive data from server
  while (client.available() > 0) {
    input = client.readStringUntil('\n');

    println(input);
    if (input != null) {

      input = input.substring(0, input.indexOf("\n")); // Only up to the newline
      data = int(split(input, ' ')); // Split values into an array
      // Draw line using received coords
      switch (data[0])
      {
      case 1:
        player1.xPos = data[1];
        player1.yPos = data[2];
        break;
      case 2:
        player2.xPos = data[1];
        player2.yPos = data[2];
        break;
      case 3:
        player3.xPos = data[1];
        player3.yPos = data[2];
        break;
      case 4:
        player4.xPos = data[1];
        player4.yPos = data[2];
        break;
      }
    }
  }
}

void game_over() {
 
}

void mousePressed() {
  if (gameState == GAME_OVER || gameState == INTRO) {
    gameState = GAME;
  }
}

//read if the ball has "hit" the player object
boolean intersect(Ball ball, Player player) {
  float d = dist(ball.x, ball.y, player.x, player.y);
 
  if(d < (ball.diameter + player.diameter)) {
    ball.hit = true;

    println("hit");
    return true;
  }
  else {
    return false;
  }
}

Replies(1)

Hi,

You could probably devise a collision detection system based on pixels by drawing each game entity (player1, player2, ball) to a different PGraphics context and then looping through the pixel data for each context every frame looking for overlaps but I can't really think why you'd want to do it like that given that the standard method that you already have works fine and is a lot more efficient.  Can you explain why you're not happy with the "normal" method?

Jonny