简体   繁体   中英

Falling object, stuck on using Timer

I'm trying to simulate a falling object with an initial horizontal speed. I understand how to make it move horizontally (no acceleration) but I have some trouble making it move vertically because of the equation y = gt^2/2 + vt + y0. I have problems because of the quadratic equation.

What I tried to do is to do make a time variable which would increase by one every time the action is performed by the SwingTimer. So that I would actually have a timevariable. But I don't think that is the best way to do it?

Can somebody push me in the right direction?

Below you can find the code I have already written:

    public class Simulation extends JPanel implements ActionListener
    {
    Timer timer = new Timer(5,this);;
    private int Xpos=0, Ypos=0, velX, velY;
    private int Px,Py;

    JButton dropknop;
    private boolean drop = false;

    public Simulation()
    {
        this.setBackground(Color.white);
        velX = 2;
        velY = 2;

        dropknop = new JButton("DROP");
        dropknop.addActionListener(this);
        this.add(dropknop);
    }
    public int getXpos() {
        return Xpos;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawRect(Xpos, 0, 20, 20);

        if(drop)
        {
            g.fillRect(Px, Py, 5, 5);
        }
    }

    public void actionPerformed(ActionEvent ae) 
    {
        if(ae.getSource() == dropknop)
        {
            Px = getXpos();
            this.drop = true;
        }
        if(Xpos<0 || Xpos>986)
        {
            velX = -velX;
        }
        if(Ypos<0 || Ypos>708)
        {
            velY = - velY;
        }

        if(drop)
        {
            Px += velY;
            Py += velX;
        }

        Ypos += velY;
        Xpos += velX;
        repaint();
    }
}

Thank you in advance!

Gravity can be implemented by simply subtracting (assuming that the positive Y direction is up) a constant (the acceleration due to gravity) from the Y velocity in each frame.

The formula y = gt^2/2 + vt + y0 is for calculating the position after a specified time, but you're simulating it for every frame, so you need an incremental method:

velY = velY - gravity;

This yields an approximation of y = gt^2/2 + vt + y0 . How much it diverges depends on the time step. The reason for the difference is that this does not simulate the continuous acceleration between steps. (In theory, the result would be the same if the time step was infinitely small).

One way to make it more accurate (used by at least some physics engines) is to calculate multiple steps of this per frame (ie do a few iterations of the physics before updating the display).

For a scientifically accurate simulation, if no other forces are acting on the body, the method you suggest in the post is the most accurate.


In your actionPerformed method, you should probably be checking that the action is the timer event, before updating the positions (so that it doesn't move faster if another event occurs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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