1

I'm doing a billiard game in processing and am trying to get the cue ball to hit the ball. I have the cue ball staying within the table, I don't quite understand collision. And for some reason I have to click twice in order to drop the cue ball.

  class Ball{
  PVector postions;
  PVector velocity;
  float r,m;
  Ball(float x, float y, float r_) {
  postion = new PVector(x,y);
  velocity = PVector.random2D();
  velocity.mult(3);
  m = r*.1;
  int xpos,ypos,diam;
  Ball(
  int tempxpos, 
  int tempypos, 
  int tempdiam
  ){
  xpos=tempxpos;
  ypos=tempypos;
  diam=tempdiam;


  }
  void update(){
  postion.add(velocity);
  ellipse(xpos,ypos,diam,diam);
  }

  void checkBoundaryCollision(){
  if(postion.x > width-r) {
  positon.x = width-r;
  veloctiy.x *= -1;
  }
  else if (position.x <r) {
  position.x = r;
  velocity.x*=-1;
  }
  else if (position.y > height-r) {
  position.y = height-r;
  velocity.y*=-1;
   }
  }

  void checkCollision(Ball other) {
  PVector bVect = PVector.sub(other.position, position);
  float bVectMag = bVect.mag();

  if(bVectMag < r + other.r){
   float theta = bVect.heading();
   float sine = sin(theta);
   float cosine = cos(theta);

  PVector[] bTemp = {
    new PVector(), new PVector()
  };

  bTemp[1].x = cosine * bVect.x + sine * bVect.y;
  bTemp[1].y = cosine * bVect.y - sine * bVect.x;

  PVector[] vTemp = {
    new PVector(), new PVector() 
  };

  vTemp[0].x = cosine * velocity.x + sine * velocity.y;
  vTemp[0].y = cosine * velocity.y - sine * velocity.x;
  vTemp[1].x = cosine * other.velocity.x + sine * other.velocity.y;
  vTemp[2].y = cosine * other.velocity.y - sine * other.velocity.x;

  PVector[] vFinal = {
    new PVector(), new PVector()
  };

  vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
  vFinal[0].y = vTemp[0].y;

  vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
  vFinal[1].y = vTemp[1].y;

  void display() {
    noStroke();
  }



    Ball b1, b2;
    int mouseClick=0;
    String msg;
    int steps = 20;
    int difx, dify;

   void setup() {
   msg="";
   b1 = new Ball(mouseX, mouseY, 50);
   b2 = new Ball(mouseX, mouseY, 50);
   size(600, 400);
  }



void draw() {
  background(0, 153, 0);
  fill(255, 255, 255);
  b1.update();
  fill(0, 0, 0);
  b2.update();
  textSize(20);
  text(msg, 0, height-5);

  if (mouseClick==0) {
    b1.xpos=mouseX;
    b1.ypos=mouseY;
    msg="click twice to place the ball";
  } 
  else if (mouseClick==1) {
    b1.xpos=mouseX;
    b1.ypos=mouseY;
    fill(255, 255, 255);
    b1.update();
  } 
  else if (mouseClick==3) {
    msg="click to shoot";
    difx = b1.xpos-b2.xpos;
    dify = b2.ypos-b2.ypos;
    b1.xpos-=difx/steps;
    b1.ypos-=dify/steps;
    b1.xpos+=5;
  }

  if (mouseClick==2) {
    msg="click to place the eight ball";
    b2.xpos=mouseX;
    b2.ypos=mouseY;
    b2.update();
  }
}


void mousePressed() {
  mouseClick++;
}
Leopold Stoich
  • 467
  • 1
  • 4
  • 11
  • What college/work place is it that in the last 3-4 days there have been 3-4 different accounts all "making a billiards game"? This is what I told one of you: For collision detection, See this: processing.org/examples/circlecollision.html Now read that, and then ask specific questions with @ and my name and I'll help you out. If all you say is "I don't understand collision" then there's no way I or anyone else can actually help you without giving you the solution. I'm not interested in just writing the solution for you though. – Nico Apr 20 '14 at 00:56
  • Also, post the Ball class if you want help. Or copy paste it from one of the 10 other questions about this same problem. – Nico Apr 20 '14 at 00:57
  • I edited my original and posted my Ball class, my apologies, I wasn't aware that others have been posting similar questions. I am receiving an unexpected token error and am wondering it may be. It's highlighting the "int tempxpos". – Leopold Stoich Apr 20 '14 at 02:44
  • I'll check it out later and see what's up. – Nico Apr 20 '14 at 03:56
  • Code's not compilable. All your braces are out of whack and you have deleted the other part of the code that is needed. Just copy paste everything from your sketch and paste it here. The thing needs to at least be properly formatted (use ctrl + t in Processing PDE), all the braces need to be there, etc. – Nico Apr 20 '14 at 06:52
  • I added the the other part of the code, taking a look at my brackets now. – Leopold Stoich Apr 20 '14 at 14:33
  • See my answer http://stackoverflow.com/a/23193044/3088138 on the physics of collision, including fixed disks. For billiard you may want to include the spin of the ball in some fashion, but this is dark science, no internet sources to be found. And extremely dependent on the material properties of the ball and its surface. – Lutz Lehmann Apr 21 '14 at 13:20
  • See also the related question http://stackoverflow.com/questions/22919567/javascript-circle-circle-collision-issue – Lutz Lehmann Apr 21 '14 at 14:01

0 Answers0