【问题标题】:Java Rectangle Intersect Always TrueJava 矩形相交始终为真
【发布时间】:2015-05-29 04:25:25
【问题描述】:

我正在为学校做一个项目,并实施了一种方法来检查两个对象是否相交。我已经定义了两个矩形,但是当它检查它们是否相交时,它总是正确的,即使它们位于 JFrame 的相对两侧。 Boolean Collision 在开始时设置为 false,x 和 y 是屏幕上两张照片的坐标。

public Rectangle Bounds() {
    return (new Rectangle(x, y, 225, 225)) ;
}
public Rectangle TrollBounds() {
    return (new Rectangle(x, y, 24, 24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

检查交叉点的部分

if (hitbox.intersects(Hitbox2)) {
            collision = true ;
            System.out.println("collision") ;
        } else {
            collision = false ;

全班

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable, KeyListener {

int health = 1200;

boolean collision = false ;

BufferedImage Troll ;
int cubex = 600 ;
int cubey = 100 ;

int cubexx = 200 ;
int cubeyy = 200 ;

public boolean running = true ;
BufferedImage Player;
int x = 100 ;
int y = 100 ;

boolean Jumping = false ;
int startJump = 10 ;

public void Jump() {

}

boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;

public Screen() {
    loadImages();
    Thread thread = new Thread(this);
    thread.start();
}

private void loadImages() {
    try {
        Player = ImageIO.read(getClass().getResource("/sanic.png"));
        Troll = ImageIO.read(getClass().getResourceAsStream("/Trollface.png")) ;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_W) {
        up = true ;
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
        left = true ; 
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
        right = true ;
    }
    if (e.getKeyCode() == KeyEvent.VK_S) {
        down = true ;
    }
    if (e.getKeyCode() == 31) {
        Jump() ;
    }


}

public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == 87) {
        up = false ;
    }
    if (e.getKeyCode() == 65) {
        left = false; 
    }
    if (e.getKeyCode() == 68) {
        right = false ;
    }
    if (e.getKeyCode() == 83) {
        down = false ;
    }

}

@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void run() {

    while(running) {
        //gravity

        //gravity
        //Player movement
        if (left) {
            if (x <= -10) {
                x = 1464 ;
            }
            x = x - 2 ;
        }
        if (up) {
            if(y <= -112) {
                y = 910 ;
            }
            y = y - 2 ;
        }
        if (right) {
            if (x >= 1416) {
                x = -24 ;
            }
            x = x + 2;
        }
        if (down) {
            if (y >= 900) {
                y = -10 ;
            }
            y = y + 2 ;
        }
        //Player movement

        //ball movement
        if (cubey > y) {
            cubey-- ;
        }
        if(cubey < y) {
            cubey++ ;
        }
        if (cubex > x) {
            cubex-- ;
        }
        if (cubex < x) {
            cubex++ ;
        }
        //ball movement

        if (hitbox.intersects(Hitbox2)) {
            collision = true ;
            System.out.println("collision") ;
        } else {
            collision = false ;
        }

        if (collision) {
            health -- ;
        }
        System.out.println(collision) ;

        repaint() ;

        try {
            Thread.sleep(3) ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    //Graphical loop start
    g.drawImage(Player, x, y, null) ;   
    g.drawImage(Troll, cubex, cubey, null) ;
    g.drawString("SANIC", x, y) ;
    g2d.fillRect(220, 200, health, 50) ;
    //Graphical loop end

}

public Rectangle Bounds() {
    return (new Rectangle(x,y,cubex+225,cubey+225)) ;
}
public Rectangle TrollBounds() {
    return (new Rectangle(cubex,cubey,cubex+24,cubey+24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

}

【问题讨论】:

  • 您的两个矩形都包含相同的起点(x, y)。因此难怪它们总是相交。
  • 不,为我返回 falsebounds = new Rectangle(100, 100, 225, 225)trollBounds = new Rectangle(0, 0, 24, 24)
  • 在这种情况下,您应该将Rectangles 绘制到屏幕上 (Graphics2D#draw(Shape)) 以查看它们实际上是什么;)
  • 请参阅Collision detection with complex shapes 了解工作示例。
  • 您能否添加其余代码,以便我们查看是否有任何逻辑错误?也许是您设置后刚刚打印出碰撞但没有再次调用 intersect 方法的部分?诸如此类的事情。谢谢。

标签: java swing rectangles


【解决方案1】:

如果这段代码按照它的顺序运行,hitbox 和 Hitbox2 紧随其后并且不改变其间的 x 和 y,那么它应该返回 true。

它将创建一个从 x, y 到 255, 255 的矩形和另一个从 x,y 到 24,24 的矩形,这些矩形确实相交。

这是RectangleShape.intersects() 上的JavaDoc。

【讨论】:

    猜你喜欢
    • 2015-12-05
    • 2019-02-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多