java - 我怎样才能在java中使图形对象成为实体?

标签 java collision-detection game-physics

我正在开发一个简单的游戏,我需要这些方形的保险杠,它们只是闲置,当被击中时,会发生碰撞并反射球。但目前球只是飞过我的方形保险杠。我只能使用 java awt 和 swing 库。代码如下:

class squareBumper {
      private int x = 300;
      private int y = 300;
      private Color color = new Color(66,139,139);

      public void paint(Graphics g) {
        Rectangle clipRect = g.getClipBounds();
          g.setColor(color);
          g.fillRect(x, y, 31, 31);
      }
   }

class BouncingBall {
  // Overview: A BouncingBall is a mutable data type.  It simulates a
  // rubber ball bouncing inside a two dimensional box.  It also
  // provides methods that are useful for creating animations of the
  // ball as it moves.

  private int x = 320;
  private int y = 598;
  public static double vx;
  public static double vy; 
  private int radius = 6;
  private Color color = new Color(0, 0, 0);

  public void move() {
    // modifies: this
    // effects: Move the ball according to its velocity.  Reflections off
    // walls cause the ball to change direction.
    x += vx;
    if (x <= radius) { x = radius; vx = -vx; }
    if (x >= 610-radius) { x = 610-radius; vx = -vx; }

    y += vy;
    if (y <= radius) { y = radius; vy = -vy; }
    if (y >= 605-radius) { y = 605-radius; vy = -vy; }
  }

  public void randomBump() {
    // modifies: this
    // effects: Changes the velocity of the ball by a random amount
    vx += (int)((Math.random() * 10.0) - 5.0);
    vx = -vx;
    vy += (int)((Math.random() * 10.0) - 5.0);
    vy = -vy;
  }

  public void paint(Graphics g) {
    // modifies: the Graphics object <g>.
    // effects: paints a circle on <g> reflecting the current position
    // of the ball.

    // the "clip rectangle" is the area of the screen that needs to be
    // modified
    Rectangle clipRect = g.getClipBounds();

    // For this tiny program, testing whether we need to redraw is
    // kind of silly.  But when there are lots of objects all over the
    // screen this is a very important performance optimization
    if (clipRect.intersects(this.boundingBox())) {
      g.setColor(color);
      g.fillOval(x-radius, y-radius, radius+radius, radius+radius);
    }
  }

  public Rectangle boundingBox() {
    // effect: Returns the smallest rectangle that completely covers the
    //         current position of the ball.

    // a Rectangle is the x,y for the upper left corner and then the
    // width and height
    return new Rectangle(x-radius, y-radius, radius+radius+1, radius+radius+1);
  }
}

最佳答案

看一下实现 Shape 的类界面。有椭圆形和其他形状,它们都实现了 intersects(Rectangle2D) 方法。如果您不想自己执行交叉操作,它可能会对您有所帮助。

至于处理碰撞,嗯,这取决于你想要的精确度。简单地使边缘球偏转是非常容易的。只需确定矩形碰撞边是垂直还是水平,并相应地抵消相应的速度分量即可。如果你想处理角落,那就有点复杂了。

关于java - 我怎样才能在java中使图形对象成为实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527198/

相关文章:

java - 递归方法中的逻辑错误

c# - 两条线段在 c# 或 vb.net 中使用 xna 相交吗?

javascript - 使用 cocos 2d js 和 Chipmunk 创建动态主体

java - 如何让物体在另一个物体接触后消失?

ios - 在 cocos2d 3.1 中拖放物理体?

linux - Unity3D Linux Build 在启动时崩溃。 (Ubuntu 16.04 长期支持版)

swift - Spritekit联系人检测崩溃(EXC_breakpoint)

java - 使用分割字符串中的新对象类型创建嵌套映射

java - Android WallpaperManager 方法 "soft-bricking"用户的手机并导致设备无响应

java - DatagramSocket 不适用于 java,但适用于 javaw