Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Object 将对象保留在屏幕上_Object_Collision Detection_Processing - Fatal编程技术网

Object 将对象保留在屏幕上

Object 将对象保留在屏幕上,object,collision-detection,processing,Object,Collision Detection,Processing,现在我相信你们都听够了这些愚蠢的台球游戏问题,这应该是我的最后一次了,我已经解决了碰撞问题,但是把球留在屏幕上有点问题。我该怎么做呢 class Ball { int xpos, ypos; int ballDiam; color myColor; Ball(int tempdiam, color tempColor) { ballDiam=tempdiam; myColor=tempColor; } void update() { fill(

现在我相信你们都听够了这些愚蠢的台球游戏问题,这应该是我的最后一次了,我已经解决了碰撞问题,但是把球留在屏幕上有点问题。我该怎么做呢

 class Ball {
  int xpos, ypos;
  int ballDiam;
  color myColor;
  Ball(int tempdiam, color tempColor) {
    ballDiam=tempdiam;
    myColor=tempColor;
  }

  void update() {
    fill(myColor);
    ellipse(xpos, ypos, ballDiam, ballDiam);
  }
}

Ball b1, b2;
int click;
String msg;
int steps = 30;
int difx,dify;
Boolean moving = false;
void setup() {
  msg="";
  click=0;
  size(600, 300);
  b2= new Ball(50, #000000);
  b1 = new Ball(50, #ffffff);
}
void draw() {
  background(#009900);
  b1.update();
  b2.update();

  if (click==0) {
    b1.xpos=mouseX;
    b1.ypos=mouseY;

    msg="click to place ball";
  }
  if (click==0) {
    b1.xpos=mouseX;
    b1.ypos=mouseY;
    b1.update();
  }
  else if (click==1) {
    b2.xpos=mouseX;
    b2.ypos=mouseY;
    b2.update();
    difx = b1.xpos-b2.xpos;
    dify = b1.ypos-b2.ypos;
    msg="click to place eightball and shoot";
  } else if(click==2){
    b1.xpos-=difx/steps;
    b1.ypos-=dify/steps;
    b1.update();
    //cdistance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
    float distance = dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
    if(distance>b2.ballDiam/2){
      moving = true;
      b1.xpos-=difx/steps;
      b1.ypos-=dify/steps;
    }
    else{
      moving = false;
      msg="new";
      click=3;

    }

  }

  else if(click==3){
    if (b1.xpos<b2.xpos && b1.ypos<b2.ypos){
        b2.xpos+=5;
        b2.ypos+=5;
      }
    if (b1.xpos>b2.xpos && b1.ypos>b2.ypos){
        b2.xpos-=5;
        b2.ypos-=5;
      }
    if (b1.xpos>b2.xpos && b1.ypos<b2.ypos){
        b2.xpos-=5;
        b2.ypos+=5;
      }
    if (b1.xpos<b2.xpos && b1.ypos>b2.ypos){
        b2.xpos+=5;
        b2.ypos-=5;
      }
    msg="click again to start over";
  }
 else if(click==4){
   setup();
 }


  textSize(20);
  text(msg, 0, height-5);
}


void mouseClicked() {
  if(!moving){
    click++;
  }
}
班级舞会{
int XPO,YPO;
int-ballDiam;
色霉色;
球(int tempdiam,color tempColor){
ballDiam=tempdiam;
myColor=tempColor;
}
无效更新(){
填充物(myColor);
椭圆(xpo、ypos、ballDiam、ballDiam);
}
}
球b1、b2;
点击;
串味精;
int步数=30;
int-difx,dify;
布尔移动=假;
无效设置(){
msg=“”;
单击=0;
大小(600300);
b2=新球(50,#000000);
b1=新球(50,#ffffff);
}
作废提款(){
背景(#009900);
b1.update();
b2.update();
如果(单击==0){
b1.xpos=mouseX;
b1.ypos=老鼠;
msg=“单击以放置球”;
}
如果(单击==0){
b1.xpos=mouseX;
b1.ypos=老鼠;
b1.update();
}
否则如果(单击==1){
b2.xpos=mouseX;
b2.ypos=老鼠;
b2.update();
difx=b1.xpos-b2.xpos;
dify=b1.ypos-b2.ypos;
msg=“单击以放置八个球并射击”;
}否则如果(单击==2){
b1.xpos-=difx/步;
b1.ypos-=dify/步数;
b1.update();
//cdistance=dist(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
浮动距离=距离(b1.xpos,b1.ypos,b2.xpos,b2.ypos);
如果(距离>b2.直径/2){
移动=真;
b1.xpos-=difx/步;
b1.ypos-=dify/步数;
}
否则{
移动=假;
msg=“新”;
点击=3;
}
}
否则如果(单击==3){
if(b1.xposb2.ypos){
b2.xpos-=5;
b2.ypos-=5;
}

如果(b1.xpos>b2.xpos&&b1.ypos您可以这样做,这几乎与您在两个球之间进行碰撞检测的方式相同,您需要在球和屏幕边界之间进行测试。。。 您还需要确保利用Ball类的diameter属性来确保您真正获得球的边缘,并且球不会半反弹回来

if( b1.xpos + b1.ballDiam >= width ) { // middle of the ball plus its diameter is over the right screen bounds
  println("ball hit the right side");
  b1.xpos = width - b1.ballDiam; // clamping the position to the bounds of the screen
  b1.reverse(); // you probably want to do something about making it bounce back at this point
}

if( b1.xpos - b1.ballDiam <= 0 ) { // middle of the ball minus its parameter is over the left screen bounds
  println("ball hit the left side");
  // all the other stuff here
}

if( b1.ypos - b1.ballDiam <= 0 ) { // middle of the ball minus its parameter is over the top screen bounds
  println("ball hit the top side");
  // all the other stuff here
}

if( b1.ypos + b1.ballDiam >= height ) { // middle of the ball minus its parameter is over the bottom screen bounds
  println("ball hit the bottom side");
  // all the other stuff here
}
如果(b1.xpos+b1.ballDiam>=width){//球的中间加上它的直径超过了右屏幕边界
println(“球击中右侧”);
b1.xpos=宽度-b1.ballDiam;//将位置夹紧到屏幕边界
b1.reverse();//您可能想做点什么,让它在此时反弹回来
}

如果(B1.XPOS-B1.BaldiaM)你在用什么语言?我正在使用程序处理。嘿,请考虑一下投票和/或接受我的答案,如果它对你有帮助…