我的碰撞检测在Java图形中不起作用。

huangapple 未分类评论49阅读模式
英文:

My collision detection does not work for Java graphics

问题

int border = 30;
boolean balldown = true;
boolean ballright = true;
int bounce = 0;

public void moveBall() {
    if (balldown == true) {
        rect3.b++;
    }

    if (balldown == false) {
        rect3.b--;
    }

    if (rect3.b == getHeight() - border) {
        balldown = false;
        bounce++;
    }

    if (rect3.b == 0) {
        balldown = true;
        bounce++;
    }

    if (ballright == true) {
        rect3.a++;
    }

    if (ballright == false) {
        rect3.a--;
    }

    if (colision == true) {
        ballright = false;
        bounce++;
    }
    colision = false;

    if (colision == true) {
        ballright = true;
        bounce++;
    }
}

rect rect1;
rect rect2;
rect rect3;
Timer time;
boolean colision = false;

public Pong() {
    rect1 = new rect(0, 0);
    rect2 = new rect(977, 0);
    rect3 = new rect(40, 40);
    time = new Timer(5, this);
    time.start();
}

public class rect {
    int a, b;

    public rect(int startA, int startB) {
        a = startA;
        b = startB;
    }

    public Rectangle bounds() {
        return (new Rectangle(a, b, 30, 200));
    }

    public Rectangle bounds2() {
        return (new Rectangle(a, b, 30, 30));
    }
}

@Override
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);
    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, rect2.b, 30, 200);

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

    if (colision == true)
        g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
        g.drawString("Colision", 350, 50);

    g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    g.setColor(Color.WHITE);
    g.drawString("BOUNCES: " + bounce, 40, 20);
}

public void colision() {
    Rectangle rectangle1 = rect1.bounds();
    Rectangle rectangle2 = rect2.bounds();
    Rectangle rectangle3 = rect3.bounds2();

    if (rectangle3.intersects(rectangle1))
        colision = true;
    else
        colision = false;

    if (rectangle3.intersects(rectangle2))
        colision = true;
    else
        colision = false;
}

@Override
public void actionPerformed(ActionEvent e) {
    moveBall();
    colision();
    repaint();
}

Please note that the code you provided has some issues with indentation and formatting, so I've tried to preserve the original structure while providing the translated content. If you have any further questions or need clarifications, feel free to ask.

英文:
int border=30;
boolean balldown=true;
boolean ballright=true;
int bounce=0;


public void moveBall(){
    
    
    
    if (balldown==true){
        //y++; 
        rect3.b++;
        
    }
    
    
    if (balldown==false){
        y--;
        rect3.b--;
    }
    //y==getHeight()-border
    if(rect3.b==getHeight()-border){
        
        
        balldown=false;
        bounce++;
    }
    //y==0
    if(rect3.b==0){
        
        
        balldown=true;
        bounce++;
    }
    
    if (ballright==true){
        x++; 
        rect3.a++;
    }
    
    if (ballright==false){
        x--;
        rect3.a--;
    }
    //x==getWidth
        if(colision==true){
        
        
        ballright=false;
        bounce++;
        
    }
    colision=false;
    
    //x==0
    //rect3.bounds2()==rect1.bounds()
    if(colision==true){
        
        
        ballright=true;
        bounce++;
        
    }
        
    }
   /* if(rect3.a==getWidth()){
        ballright=false;
    }
    if(rect3.a==0){
        ballright=true;
    }*/
}

   // int a=0;
   // int b=0;
    //int width=30;
   // int height=200;
    
    rect rect1;
    rect rect2;
    rect rect3;
    Timer time;
    boolean colision=false;

    public Pong() {
        rect1 = new rect(0,0);
        rect2 = new rect(977,0);
        rect3=new rect(40,40);
        time=new Timer(5,this);
        time.start();
    }
    
    
    

public class rect{
    int a,b;
    
    public rect(int startA, int startB){
        a=startA;
        b=startB;
    }

    public Rectangle bounds(){
    
    return (new Rectangle(a,b,30,200));
}
    public Rectangle bounds2(){
    
    return (new Rectangle(a,b,30,30));
    }
}
    



@Override
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, rect2.b, 30, 200);
    
    g2d.fillRect(520, 0, 10, 760);
    
    if (colision==true)
        g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
        g.drawString("Colision",350, 50);
        
    
        
     g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
 
        g.setColor(Color.WHITE);

        g.drawString("BOUNCES: "+bounce, 40, 20);
}


public void colision(){
    
    Rectangle rectangle1=rect1.bounds();
    Rectangle rectangle2=rect2.bounds();
    Rectangle rectangle3=rect3.bounds2();
    
        if(rectangle3.intersects(rectangle1))
            colision=true;
        else
            colision=false;
        
        if(rectangle3.intersects(rectangle2))
            colision=true;
        
        else
            colision=false;
        
       
}
@Override
public void actionPerformed(ActionEvent e){
        moveBall();
        colision();
        repaint();

So basically my if statement that uses intersects never returns true whenever my ball hits the rectangle on the side. Also in my moveball method is supposed supposed to "bounce" in the other direction whenever those bounds meet but it does not work and simply passes trough with no collision detection even. Did I implement it wrong? When I checked with a system.out.print it kept returning the message like 2 times per second or even faster. Im a java beginner to dont scream at me for obvious mistakes and the bad structure. Thanks

huangapple
  • 本文由 发表于 2020年5月5日 17:00:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61609450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定