0

How do you make the Player (Graphics) fall down and hit the border of the window? I do not have the script for the physics but my interpretation of how the code might go is:


//class ProgramGUI
import java.awt.*;

public void actionPerformed(ActionEvent ae) {
      posX += vlx;
      posY += vly;
}

public void paint(Graphics g) {
      Graphics2D player = (Graphics2d) g;
      player.fillRect(posX, posY, sizeX, sizeY)
}

public void fallDown() {
       posY = posY--; //is this correct? [^-^]
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Athran123_
  • 11
  • 2
  • Consider this. You have a up-ward delta (energy) and a down-ward delta (gravity). Over time, the amount of up-ward delta will reduce (due to gravity) until you reach the top of the jump, at which point, the up-ward delta would become negative (and keep decreasing until you either hit the ground or reach terminal velocity). Now, I'm no physicists, so what I would start with is an initial upward delta and I would have some kind of "drag" delta which would be subtracting from it on each update. I would also do a range check so the upward delta never went below a "terminal" amount – MadProgrammer Oct 28 '21 at 03:00
  • For [example](https://stackoverflow.com/questions/16493809/how-to-make-sprite-jump-in-java/16494178#16494178) and [example](https://stackoverflow.com/questions/66717212/how-can-i-make-my-jump-animation-work-all-at-once/66739586#66739586) – MadProgrammer Oct 28 '21 at 03:00

1 Answers1

1

A bit of an overly simplified example. When you press Space, the object will begin to fall. A small amount of "gravity" is continuously added to the vertical delta till either you hit the bottom of the view or it reaches a terminal velocity.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private double yDelta = 0;
        private double gravityDelta = 0.01;
        private double terminalVelocity = 4.0;

        private boolean isFalling = false;

        private Timer timer;

        private Rectangle2D player;

        public TestPane() {
            player = new Rectangle2D.Double(90, 0, 20, 20);

            getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "start");
            getActionMap().put("start", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (isFalling) {
                        return; 
                    }
                    yDelta = 0;
                    isFalling = true;
                    player = new Rectangle2D.Double(90, 0, 20, 20);
                }
            });
        }

        protected void doNextUpdate() {
            if (isFalling) {
                yDelta += gravityDelta;
                if (yDelta > terminalVelocity) {
                    yDelta = terminalVelocity;
                }

                player.setRect(player.getX(), player.getY() + yDelta, player.getWidth(), player.getHeight());
                if (player.getY() + player.getHeight() > getHeight()) {
                    yDelta = 0;
                    isFalling = false;
                    player.setRect(player.getX(), getHeight() - player.getHeight(), player.getWidth(), player.getHeight());
                }
            }

            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 400);
        }

        @Override
        public void addNotify() {
            super.addNotify();
            if (timer != null) {
                timer.stop();
            }
            timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    doNextUpdate();
                }
            });
            timer.start();
        }

        @Override
        public void removeNotify() {
            super.removeNotify();
            if (timer != null) {
                timer.stop();
            }
            timer = null;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(player);
            g2d.dispose();
        }

    }
}

Some thing to keep in mind. A positive vertical delta will move you down, a negative one will move you up

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366