0

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);
    }
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
mander39
  • 29
  • 2
  • 2
    Why not just multiply the velocity by negative 1? Or any fraction of it if velocity is lost – Matthew Kerian Dec 05 '18 at 05:53
  • That was one of the first things I tried and it didn't work – mander39 Dec 05 '18 at 05:56
  • What do you mean it didn't work? That's literally how it works? The problem is you also have to reflect the angles. e.g. if it's going in at 60 degrees above the vertical it bumps off at 30 degrees above the vertical – Matthew Kerian Dec 05 '18 at 05:57
  • I'm not too worried about the angles right now, I just would like it to bounce without having to relaunch. I set the velocity to negate when the viewport is hit and the ball just vanishes. I'm probably doing it wrong though somehow – mander39 Dec 05 '18 at 06:04
  • The thing is my x and y values dont depend at all on vx or vy and I'm not sure how to adjust that – mander39 Dec 05 '18 at 06:05
  • `a` is relative to what? (I.e., initial value of 70 is 70 degrees on a 360 degree circumference with 0/360 at the 12 o'clock position? Is it a 70 degree slope while descending towards the viewport edge?) I believe what you are lacking is determining the bounce angle (you could simplify it by assuming that the approach and exit trajectories are offset by exactly 90 deg... Why you'd then use to recompute vx and vy and repeat every time you hit the viewport. You don't seem to be accounting for the fact that you'll eventually bounce on a side or the top of the viewport and not just the bottom, too – Javier Larroulet Dec 05 '18 at 06:10
  • This looks like a duplicate of [Can't flip direction of ball without messing up gravity](https://stackoverflow.com/a/53637567/2521214) use vectors you do not need the angle at all ... In case the wall is not axis aligned than you need also its normal but again no angles needed vector math solves the problem on its own easily ... – Spektre Dec 06 '18 at 08:37

1 Answers1

0

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;
}
Matthew Kerian
  • 812
  • 5
  • 18
  • Thanks so much for the help. Only problem is my projectile accelerates very quickly now into outer space on the first bounce, resetting the velocity to the original value upon the bounce doesn't seem to do anything though. Thanks again I was really going about it the wrong way – mander39 Dec 05 '18 at 06:45
  • Main problem now is that without t being reset it accelerates to infinity – mander39 Dec 05 '18 at 06:58