简体   繁体   中英

Java - Bouncing a projectile ball - How can I reverse vertical velocity?

I'm attempting to bounce a ball in java, but I'm struggling to figure out how to reverse the vertical component of the ball's velocity when it hits the viewport. My main issue (which might be dumb) is that I don't understand how the velocity component's directions (- in this case) contribute to the overall velocity when the velocity formula squares the negative values. Thanks for the help.

Currently I sort of relaunch the projectile with an offset when it hits the viewport, but this has the issue of having the wrong angles.

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Trajectory5 extends JComponent {
private static final long serialVersionUID = 1L;

Timer timer; double x; double y; double vy; double vx;
double t = 0; double a = 70; double v = 40;
double offsetx = 0; double offsety = 400;
double xmult = 0;

public Trajectory5() {
    setFocusable(true);
    timer = new Timer(1, new TimerCallback()); 
    timer.start();}

    @Override
    public void paintComponent(Graphics g) {
        g.fillOval((int) x, (int) y, 10, 10);
    }

    protected class TimerCallback implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            t += .01;
            x = ((int) v*Math.cos(Math.toRadians(a)) * t + offsetx) + -xmult;
            y = getHeight() - ((int) v * Math.sin(Math.toRadians(a)) * t-.5 * 9.8 * (Math.pow(t, 2))) - offsety;
            vy = v * Math.sin(Math.toRadians(a)) - (t * 9.8);
            vx = v * Math.cos(Math.toRadians(a));

            System.out.println(vy + " " + vx);

            if (y > getHeight()) {
                offsetx = x;
                offsety = 0;
                v = Math.sqrt(Math.pow(vx, 2)+Math.pow(vy, 2));
                t = 0;
            }
            repaint();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame(); frame.setSize(1000, 800);
        Trajectory5 canvas = new Trajectory5();
        frame.add(canvas); frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

So the issue here is really a structural one, you're making it more complex than it needs to be. This is really more of a math than programming question.

Your x and y coordinates start at a certain position, and then you have to adjust it based on a certain factor. You'll adjust the position based on a). The velocity at the time, and c). an arbitrary fraction based what units you're using

To step through those steps, it really depends on what you're doing, but to make it simple lets say you have your vx and vy calculations already done (you can figure that out yourself), your actionPerformed would look like:

public void a...{
    deltaX = vx * getTimeFraction();
    deltaY = vy * getTimeFraction();
    ...
}

deltaX and deltaY is the actual distance you're going to move the ball. So to move it we have to apply it to the actual x and y variables.

public void a...{
    deltaX = vx * getTimeFraction();
    deltaY = vy * getTimeFraction();

    x += deltaX;
    y += deltaY;
}

This will move x and y, deltaX and deltaY distance respectively. Note that if deltaX is negative, it'll move left, and if deltaY is negative, it'll move it down (or up, depending on which you decide is negative)

We're nearly done, so at this point we have to define getTimeFraction()

public double getTimeFraction(){
    return 1 / iterationsPerSecond;
}

iterationsPerSecond is going to be defined as the amount of small movements you want per second.

This covers all of the movement, however right before this you need to define what vx and vy are, so your end result will look similar to:

public void a...{
    calculateVelocities();

    deltaX = vx * getTimeFraction();
    deltaY = vy * getTimeFraction();

    x += deltaX;
    y += deltaY;
}

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