Processing 与椭圆碰撞

Processing 与椭圆碰撞,processing,collision,ellipse,Processing,Collision,Ellipse,我正在处理一个台球游戏,试图让主球击中球。我把主球放在桌子里,我不太懂碰撞。出于某种原因,我必须点击两下才能放下主球 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

我正在处理一个台球游戏,试图让主球击中球。我把主球放在桌子里,我不太懂碰撞。出于某种原因,我必须点击两下才能放下主球

  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++;
}

在过去的3-4天里,有3-4个不同的账户都在做台球游戏,这是什么大学/工作场所?这就是我告诉你们其中一个人的:关于碰撞检测,请看:processing.org/examples/circlecollision.html现在读一下,然后用@和我的名字问一些具体的问题,我会帮你们的。如果你说的是我不理解碰撞,那么如果不给你解决方案,我或其他任何人都无法真正帮助你。不过,我对为你写解决方案不感兴趣。另外,如果你需要帮助,请发布Ball类。或者从关于这个问题的10个其他问题中复制粘贴它。我编辑了我的原创并发布了我的球类课程,我很抱歉,我不知道其他人也发布了类似的问题。我收到一个意外的令牌错误,我想知道它可能是。它突出显示了int-tempxpos。我稍后会检查它,看看有什么问题。代码不可编译。你所有的大括号都坏了,你已经删除了代码中需要的其他部分。只需复制粘贴草图中的所有内容并粘贴到此处。在处理PDE时,至少需要使用ctrl+t正确格式化,所有大括号都需要在那里,等等。