简体   繁体   English

当两个矩形在 Java 中触摸时,如何使它们相互检测?

[英]How do I make 2 rectangles detect eachother when they touch in Java?

  if(rect3.a+30>rect2.a){


        ballright=false;
        bounce++;
    }
     if(rect3.a-30<rect1.a){

        ballright=true;
        bounce++;
    }

 public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d=(Graphics2D) g;
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 1080, 760);

    g2d.setColor(Color.WHITE);
   // g.fillOval(x, y, 30, 30);
   g2d.fillRect(rect3.a, rect3.b, 30, 30);


    g2d.setColor(Color.WHITE);
    g2d.fillRect(rect1.a, rect1.b, 30, 200);

    g2d.setColor(Color.WHITE);
    g2d.fillRect(rect2.a, rect3.b-50, 30, 200);

    g2d.fillRect(520, 0, 10, 760);

So I have these rectnagles to be the objects for a pong game.所以我有这些矩形作为乒乓球比赛的对象。 Except I cant seem to figure out how to make the ball (rect3) bounce of the paddles (rect1,2) and if the paddles is not there go to the edge of the window to score a point.除了我似乎无法弄清楚如何使球(rect3)反弹桨(rect1,2),如果桨不存在 go 到 window 的边缘得分。 The way I have it now it simply bounces of the whole y axis at 0 and 1080. Is there a way to make it bounce only when the paddle is there and if it is not score a point?我现在拥有它的方式只是在 0 和 1080 处反弹整个 y 轴。有没有办法让它只有在桨在那里并且没有得分时才反弹?

Let's start with the basic objects here:让我们从这里的基本对象开始:

  1. Left Paddle (left:top:height:width = x1, y1, h, w)左桨(左:上:高:宽 = x1,y1,h,w)
  2. Right Paddle (left:top:height:width = x2, y2, h, w)右桨(左:上:高:宽 = x2,y2,h,w)
  3. Ball (centreXpos:centreYpos:radius = bx, by, r)球 (centreXpos:centreYpos:radius = bx, by, r)
  4. Board (left:top:height:width = boardX, boardY, boardH, boardW)板(左:顶部:高度:宽度 = boardX,boardY,boardH,boardW)

Note: - width here refers to the short side - height refers to the length of the long side of rectangle注意:这里的宽度是指短边高度是指矩形长边的长度

Now that we have the objects out the way let's identify edges against which you want to detect collision:现在我们已经将对象排除在外,让我们确定要检测碰撞的边缘:

1. Left Paddle - Collision surface would be the long right side (ie All pixels from x=(left + width) y=top to x=(left + width) y = (top + height)) 1. Left Paddle - 碰撞表面将是长的右侧(即从 x=(left + width) y=top 到 x=(left + width) y = (top + height) 的所有像素)

2. Right Paddle - Collision surface would be the long left side (ie All pixels from x=(left) y=top to x=(left) y = (top + height)) 2. Right Paddle - 碰撞面将是长的左侧(即从 x=(left) y=top 到 x=(left) y = (top + height) 的所有像素)

3. Ball - Collision surface is all pixels on the edge of the circle - so bx +/- radius and by +/- radius 3. 球- 碰撞表面是圆边缘上的所有像素 - 所以 bx +/- 半径和 +/- 半径

4. Board - Collision surface are the edges of the board - Long edges should cause you to bounce - short edges should end the rally 4. 棋盘- 碰撞面是棋盘的边缘 - 长边应该让你反弹 - 短边应该结束反弹

Collision detection - if collision surface of the ball is >= collision surface of right side paddle or - if collision surface of the ball is <= collision surface of left side paddle碰撞检测- 如果球的碰撞表面 >= 右侧桨的碰撞表面或 - 如果球的碰撞表面 <= 左侧桨的碰撞表面

What to do when collision is detected?检测到碰撞时该怎么办? - if collision is detected with left paddle change direction to move to right (change increments to bx and by to make it take a different direction) - if collision is detected with left end but not the paddle stop animation - if collision is detected with right paddle change direction to move to left - if collision is detected with right end but not the paddle stop animation - 如果检测到与左桨的碰撞改变方向向右移动(将增量更改为 bx 并使其采取不同的方向) - 如果检测到左端碰撞但未检测到桨停止 animation - 如果检测到右碰撞桨改变方向向左移动 - 如果检测到右端碰撞但桨停止 animation

Suggestion around Objects围绕对象的建议

It would help if you modelled your objects and then in your animation loop called collision detector with each surface that you care about The collision detector could then respond with an enumeration.如果您对对象进行建模,然后在称为碰撞检测器的 animation 循环中对您关心的每个表面进行建模,这将有所帮助,然后碰撞检测器可以通过枚举进行响应。 You can have another function act based on the enumeration您可以根据枚举让另一个 function 行动

From what I see so far you would need从我目前看到的情况来看,你需要

GameObjects
|-- Ball
|-- Paddle
    |-- Left
    |-- Right
|-- Board

you can choose to use inheritance.您可以选择使用 inheritance。

CollisionDetector碰撞检测器

  • iterate through all Game objects and Detect if ball has collided with any surfaces that interest you遍历所有游戏对象并检测球是否与您感兴趣的任何表面发生碰撞
  • Here you could get both object say, Ball and Left-Paddle and have specific collision routines written for each在这里,您可以同时获得 object,例如 Ball 和 Left-Paddle,并为每个都编写特定的碰撞例程

( BTW.... with this you can even write test cases around Collision detection and ball movement rather than trying to play it everytime to see if it works as expected ) 顺便说一句......你甚至可以围绕碰撞检测和球运动编写测试用例,而不是每次都尝试播放它以查看它是否按预期工作

I believe this will make it simpler.我相信这会让事情变得更简单。 If you have a running copy somewhere on github I am happy to help you with it.如果您在 github 某处有运行副本,我很乐意为您提供帮助。

Reading back on the above seems like a long-winded explanation for what you were looking for and something more.回顾上面的内容似乎是对您正在寻找的内容以及更多内容的冗长解释。

The above does not really depend on Java.. you could do that in almost any language.以上内容并不真正依赖于 Java .. 你几乎可以用任何语言做到这一点。

Hope this helps:)希望这可以帮助:)

you are only checking if ball.x + ball.width > paddle.x you should also account for the case that ball.y is between paddle.y and paddle.y + paddle.height您只检查 ball.x + ball.width > paddle.x 您还应该考虑 ball.y 在 paddle.y 和 paddle.y + paddle.height 之间的情况

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

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