停止后,彩绘的形状会继续振动

时间:2015-05-16 12:48:35

标签: java swing collision-detection

我尝试通过创建一个从侧面反弹并减速的球来重新创建一些物理。球在x方向上停止移动,但它在y方向上仅向上和向下振动1个像素。它也会在底部边框上方稍微做一点。

另外,我的代码是可读/良好的做法吗?

Bouncy.java

package Bouncy;

import javax.swing.*;

public class Bouncy {

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Bouncy Balls");
        Board board = new Board();
        frame.getContentPane().add(board);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocation(2000, 50);
        board.requestFocusInWindow();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

Board.java

package Bouncy;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Board extends JPanel implements ActionListener {

    public static final int BOARDWIDTH = 800;
    public static final int BOARDHEIGHT = 800;

    private Ball ball;

    public Board() {

        Dimension preferedDimension = new Dimension(BOARDWIDTH, BOARDHEIGHT);
        setPreferredSize(preferedDimension);
        ball = new Ball(15, 0);
        Timer animationTimer = new Timer(17, this);
        animationTimer.start();
    }

    public void actionPerformed(ActionEvent e) {
        ball.applyPhysics();
        repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(rh);

        ball.display(g2d);

        g2d.dispose();
    }
}

Ball.java

package Bouncy;

import java.awt.*;

public class Ball {

    private int xPos;
    private int yPos;
    private double dx;
    private double dy;
    private int ballWidth;
    private int ballHeight;

    public Ball() {
        this(0, 0);
    }

    public Ball(int dx, int dy) {
        ballWidth = 50;
        ballHeight = ballWidth;
        xPos = 0;
        yPos = 0;
        this.dx = dx;
        this.dy = dy;
    }

    public void applyPhysics() {
        bounceX();
        applyXFriction();
        bounceY();
    }

    public void bounceX() {
        if (xPos > Board.BOARDWIDTH - ballWidth) {
            xPos = Board.BOARDWIDTH - ballWidth;
            dx = -dx;
        } else if (xPos < 0) {
            xPos = 0;
            dx = -dx;
        } else {
            xPos += dx;
        }
    }

    public void applyXFriction() {
        final double xFriction = .95;

        if (yPos == Board.BOARDHEIGHT - ballHeight) {
            dx *= xFriction;
            if (Math.abs(dx) < .5) {
                dx = 0;
            }
        }
    }

    public void bounceY() {
        final int gravity = 12;
        final double energyLoss = .75;
        final double dt = .2;

        if (yPos > Board.BOARDHEIGHT - ballHeight){
            yPos = Board.BOARDHEIGHT - ballHeight;
            dy = -dy * energyLoss;
        } else if (yPos < 0) {
            yPos = 0;
            dy *= -energyLoss;
        } else {
            dy += gravity * dt;
            yPos += dy * dt + .5 * gravity * dt * dt;
        }
    }

    public void display(Graphics2D g2d) {
        g2d.fillOval(xPos, yPos, ballWidth, ballHeight);
    }
}

1 个答案:

答案 0 :(得分:5)

在你的应用摩擦方法中,你将dx设置为0,同时将dy设置为0.这将阻止球沿X轴和Y轴移动

if (Math.abs(dx) < .5)
{
       dx = 0;
       dy = 0;
}

这将阻止球振动:

if (Math.abs(dy * dt + .5 * gravity * dt * dt) < 1.5 && yPos == Board.BOARDHEIGHT - ballHeight) 
{
    dy = 0;
    yPos = Board.BOARDHEIGHT - ballHeight;
}