如何让球在角落里顺利滚动?

时间:2014-12-24 20:22:22

标签: java processing collision-detection collision

在此程序中,当鼠标位于另一侧时,矩形应阻挡球。当球和鼠标位于相邻侧时,球应该从角落滚出。问题是,这并没有发生,球被困在角落里。这是完整的程序:

PVector p = new PVector(100, 100); //position
PVector v = new PVector(0, 0); // velocity

void setup()
{
  size(600, 600);
  frameRate(120);
}

void draw()
{
  background(0);
  rect(250, 250, 200, 100);
  ellipse(p.x, p.y, 20, 20);
  v.setMag(2.5);
  p.add(v);

  if (p.x + 10 >= 250 && p.x - 10 <= 450 && p.y + 10 >= 250 && p.y - 10 <= 350) // ball is inside box
  {
    if (p.y  <= 250 || p.y  >= 350) // ball came from above 
    {
      v.y = -v.y;
    } 
    if (p.x <= 250 || p.x >= 450) // ball came from sides
    {
      v.x = -v.x;
    }
  } 
  else // move ball
  { 
    PVector dir = new PVector(mouseX, mouseY);
    v = PVector.sub(dir, p);
  }
}

我试图通过检查角落是否被击中但球仍然卡在角落来解决这个问题。

PVector p = new PVector(100, 100); // Pisition
PVector v = new PVector(0, 0); // Velocity

void setup()
{
  size(600, 600);
  frameRate(120);
}

void draw()
{
  background(0);
  rect(250, 250, 200, 100);
  ellipse(p.x, p.y, 20, 20);
  v.setMag(2.5);
  p.add(v);

  if (dist(p.x, p.y, 250, 250)<10) // corner has been hit
  {
    p = new PVector(239, 239); // move ball away from corner
  } 
  else if (dist(p.x, p.y, 450, 250)<10)
  {
    p = new PVector(461, 239);
  } 
  else if (dist(p.x, p.y, 450, 350)<10)
  {
    p = new PVector(461, 361);
  } 
  else if (dist(p.x, p.y, 250, 350)<10)
  {
    p = new PVector(239, 361);
  } 
  else if (p.x + 10 >= 250 && p.x - 10 <= 450 && p.y + 10 >= 250 && p.y - 10 <= 350) // ball is inside box
  {
    if (p.y  <= 250 || p.y  >= 350) // ball came from above 
    {
      v.y = -v.y;
    } 
    if (p.x <= 250 || p.x >= 450) // ball came from sides
    {
      v.x = -v.x;
    }
  } 
  else // move ball towards mouse
  { 
    PVector dir = new PVector(mouseX, mouseY);
    v = PVector.sub(dir, p);
  }
}

有没有办法让球在角落里顺利滚动?任何帮助表示赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

我已修改草图以解决您的问题。诀窍是在每个角落增加一点间隙,使得球可以穿过直角。

PVector p = new PVector(100, 100); //Position
PVector v = new PVector(0, 0);     //Velocity
PVector dir;                       //Direction
int sizeX = 600, sizeY = 600;      //Window size
int rectX = 250, rectY = 250;      //Rectangle position
int rectW = 200, rectH = 100;      //Rectangle size
int ballR = 10;                    //Ball radius
int gap = 2;                       //Corner gap

void setup(){
  size(sizeX, sizeY);
  frameRate(120);
}

void draw(){
  background(0);
  rect(rectX, rectY, rectW, rectH);
  dir = new PVector(mouseX, mouseY);
  v = PVector.sub(dir, p);
  if((v.x>0 && p.x+ballR > rectX         && p.x+ballR < rectX + rectW &&
               p.y+ballR > rectY + gap   && p.y-ballR < rectY + rectH - gap)
   ||(v.x<0 && p.x-ballR < rectX + rectW && p.x-ballR > rectX &&
               p.y+ballR > rectY + gap   && p.y-ballR < rectY + rectH - gap)){
      v.x = -v.x;
  } 
  if((v.y>0 && p.y+ballR > rectY         && p.y+ballR < rectY + rectH && 
               p.x+ballR > rectX + gap   && p.x-ballR < rectX + rectW - gap)
   ||(v.y<0 && p.y-ballR < rectY + rectH && p.y-ballR > rectY &&
               p.x+ballR > rectX + gap   && p.x-ballR < rectX + rectW - gap)){
      v.y = -v.y;
  }
  v.setMag(2.5);
  p.add(v);
  ellipse(p.x, p.y, 2*ballR, 2*ballR);
}