如何使弹跳球与处理中的矩形阵列碰撞?

时间:2018-03-07 10:44:18

标签: java arrays processing

我试图让弹跳球在矩形阵列上反弹。我查看了其他各种代码,但似乎无法找到解决方案。非常感谢任何帮助!!!

基本上,我希望弹跳球能够识别那里的矩形以及它能够跳到矩形上。

PVector location;  // Location of shape
PVector velocity;  // Velocity of shape
PVector gravity;   // Gravity acts at the shape's acceleration
PVector upwardForce;
PImage bg;
int radius = 10, directionX = 1, directionY = 0;
float x=20, y=20, speed=0.5;
int xarray[] = new int[20];
int  yarray[] = new int[20];

// =========================================================

void setup() {
  size(380,750);
  location = new PVector(100,50);
  velocity = new PVector(0.0,2.1);
  upwardForce = new PVector(0.0,-10.0);

  gravity = new PVector(0,0.4);
  bg = loadImage("bg.png");
  bg.resize(1600,1600);
  background(0);
  for(int i =0; i< 20;i++){
     xarray[i]= i*100;
     yarray[i] = 750-int(random(10))*50;
  }

}
int xd =0, yd=0;
void draw() {
  background(0);
    noStroke();
   xd--;
   yd++;
    // display image twice:
  image(bg, y, 0);
  image(bg, y+bg.height, 0);
  // pos 
  y--;
  if (y<-bg.height) 
    y=0;

    for (int i = 0;i< 20;i++){
      if (xarray[i] <100 && xarray[i]+100 >100){
         fill(255,0,0); 


      }
      else {
        fill(255); 
      }
       rect(xarray[i],yarray[i],100,1200);
       fill(255);
       xarray[i]=xarray[i]-4;
       //yarray[i]=yarray[i]+1;
       if (xarray[i] + 100 < 0){
          xarray[i]+=2000; 
         // yarray[i]-=850;
       }

    }


   // changing Position
  x=x+speed*directionX;
  y=y+speed*directionY; 
  // check boundaries
  if ((x>width-radius) || (x<radius))
  {   
    directionX=-directionX;
  }
  if ((y>height-radius) || (y<radius))
  {   
    directionY=-directionY;
  } 
  // draw
  // if(direction==1)

  // Add velocity to the location.
  location.add(velocity);
  // Add gravity to velocity
  velocity.add(gravity);

  // Bounce off edges
  if ((location.x > width) || (location.x < 0)) {
    velocity.x = velocity.x * -1;
  }
  if ((location.y > height) || (location.y < 0)){
    // We're reducing velocity ever so slightly 
    // when it hits the bottom of the window
    velocity.y = velocity.y * -0.95; 
    location.y = height;
  }

  // Display circle at location vector
  stroke(255);
  strokeWeight(0);
  fill(255);
  ellipse(location.x,location.y,30,30);

}

 void keyPressed()
        { 
         velocity.add(upwardForce);
        }

2 个答案:

答案 0 :(得分:0)

我们可以为您提供的最佳建议是break your problem down into smaller steps,并逐步采取这些步骤。

例如,您是否可以创建一个简单的草图,只显示一个硬编码的圆圈和一个硬编码的矩形?现在添加一些代码,如果它们发生冲突,则会向控制台输出消息。您将不得不对collision detection进行一些研究,但这里有一个提示:一种常见的技术是将球视为矩形,因此您可以进行矩形 - 矩形碰撞检测。 / p>

让它自己完美地工作,然后以小步骤前进。你能在草图上添加第二个矩形吗?三分之一怎么样?

然后,如果您遇到困难,您可以发布MCVE(不是您的整个项目,只是一个小例子)以及更具体的问题。祝你好运。

答案 1 :(得分:0)

以下是一些建议:

你最好使用Rectangle类。这样,您不必将位置存储在数组中,并且碰撞函数可以是类的方法。调用矩形“x”和“y”的位置更容易,但这显然会与您在代码顶部声明的x和y全局变量冲突。假设你想让球在碰撞时反弹,你需要有一个“ballLastx”和一个“ballLasty”来跟踪球的来自哪个方向。您还需要将Rectangles存储在数组或arrayList中。它会是这样的:

PVector lastLocation;
Rectangle[] rects;

至于矩形类,这里看起来像是这样的:

class Rectangle {
  float x, y;
  Rectangle(float x_, float y_) {
    x = x_;
    y = y_;
  }

  void show() {
    //Displays rectangle
    if (x < 100 && x+100 > 100) fill(255,0,0); 
    else fill(255); 
    rect(x,y,100,1200);

    fill(255);
    x=x-4;
    if (x + 100 < 0) x+=2000; 
  }

  private boolean insideX(PVector pos) {
    return (pos.x + 15 >= x && pos.x - 15 <= x+100);
  }

  private boolean insideY(PVector pos) {
    return (pos.y + 15 >= y && pos.y - 15 <= x + 1200);
  }

  boolean collidedX() {
    //Detects if the ball has collided along the x-axis
    return ((insideX(location) && !insideX(lastLocation)) && insideY(location))
  }

  boolean collidedY() {
    //Detects if the ball has collided along the y-axis
    return ((insideY(location) && !insideY(lastLocation)) && insideX(location))
  }
}

然后,在你的setup函数中,你可以在for循环中声明Rectangle类:

//declare the rects array
rects = new Rectangle[20];

//declare each item of the rects array to be a Rectangle
for(int i = 0; i < rects.length; i++) {
  rects[i] = new Rectangle(i*100, 750-int(random(0,10))*50;
}

为了检测碰撞和反弹球,你需要循环遍历所有的矩形,看看球是否应该反弹掉任何一个:

boolean bouncex = false;
boolean bouncey = false;

//see if any of the rects are colliding with the ball
for(Rectangle r : rects) {
  if(r.collidedX()) bouncex = true;
  if(r.collidedY()) bouncey = true;
}

//if any are colliding, bounce the ball
if(bouncex) velocity.x = -velocity.x;
if(bouncey) velocity.y = -velocity.y;

最后,不要忘记在移动当前位置之前将lastLocation PVector设置为当前位置:

lastLocation = location.copy();

//move the ball...

希望这有用!