简体   繁体   English

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

[英]How to make the bouncing ball collide with the array of rectangles on Processing?

im trying to make the bouncing ball bounce on the arrays of rectangles. 我试图使弹跳球在矩形阵列上反弹。 I've looked at various other codes but cant seem to find a solution. 我看过其他各种代码,但似乎找不到解决方案。 Would appreciate any help!!! 将不胜感激!

Basically, i want the bouncing ball to recognise that theres the rectangles there and for it to be able to jump onto the rectangles. 基本上,我希望弹跳球能够识别出那里存在矩形,并且能够跳到矩形上。

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

The best advice we can give you is to break your problem down into smaller steps and to take those steps on one at a time. 我们可以为您提供的最佳建议是将您的问题分解为更小的步骤,并一次执行一次。

For example, can you create a simple sketch that just shows a single hard-coded circle and a single hard-coded rectangle? 例如,您可以创建一个仅显示单个硬编码圆和单个硬编码矩形的简单草图吗? Now add some code that prints a message to the console if they're colliding. 现在添加一些代码,如果它们发生冲突,则会向控制台打印一条消息。 You're going to have to do some research into collision detection , but here's a hint: a common technique is to treat the ball as a rectangle, so you can do rectangle-rectangle collision detection. 您将不得不对碰撞检测进行一些研究,但这是一个提示:一种常见的技术是将球视为矩形,因此您可以进行矩形-矩形碰撞检测。

Get that working perfectly by itself, and then work your way forward in small steps. 使其完全正常工作,然后逐步进行下一步。 Can you add a second rectangle to your sketch? 您可以在草图中添加第二个矩形吗? How about a third? 三分之一呢?

Then if you get stuck, you can post a MCVE (not your whole project, just a small example) along with a more specific question. 然后,如果遇到困难,可以发布MCVE (而不是整个项目,只是一个小例子)以及一个更具体的问题。 Good luck. 祝好运。

Here's a few suggestions: 这里有一些建议:

You're best off using a Rectangle class. 最好使用Rectangle类。 That way, you don't have to store the locations in an array, and the collide function can be a method of the class. 这样,您不必将位置存储在数组中,collide函数可以是该类的方法。 It's easier to just call the positions of the rectangles "x" and "y", but this would obviously conflict with the x and y global variables which you declared at the top of the code. 仅将矩形的位置称为“ x”和“ y”会更容易,但这显然与您在代码顶部声明的x和y全局变量冲突。 Assuming that you would want to make the ball bounce if it collided, you would need to have a "ballLastx" and a "ballLasty" in order to keep track of which direction the ball came from. 假设您想使球碰撞时弹跳,则需要有一个“ ballLastx”和一个“ ballLasty”,以便跟踪球的方向。 You would also need to store the Rectangles in an array or arrayList. 您还需要将矩形存储在array或arrayList中。 It would be something like this: 就像这样:

PVector lastLocation;
Rectangle[] rects;

As for the rectangle class, here's how it would probably look like this: 至于矩形类,这可能是这样的:

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))
  }
}

And then, in your setup function, you could declare the Rectangle classes in a for-loop: 然后,在设置函数中,可以在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;
}

In order to detect the collision and to bounce the ball, you would need to loop through all of the Rectangles and see if the ball should bounce off any of them: 为了检测碰撞并弹起球,您将需要遍历所有矩形,并查看球是否应该从其中任何一个弹起:

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;

Finally, don't forget to set the lastLocation PVector to the current location, just before moving the current location: 最后,不要忘记在移动当前位置之前将lastLocation PVector设置为当前位置:

lastLocation = location.copy();

//move the ball...

Hope this was helpful! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM