0

Ok so I have been try to get this ball to bounce naturally for a few weeks now and can't seem to get it right. The program should allow the user to input a set amount of gravity and then have the ball bounce according to that amount. It works for the first few bounces but stopping the ball is the problem. I have to deliberately set its movement to 0 or else it will endlessly bounce in place but not move on the x-axis. This issue only happens when the gravity is set to 15, other amount and things really go bad. The ball will bounce naturally but then keep rolling on x-axis forever. I've never taken a physics class so the issue is probably in the physics code. Anyone know where the issue is?

Here is the code my code-

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

import javax.swing.JOptionPane;

//key stuff
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;


public class StartingPoint extends Applet implements Runnable{

//key press
private static boolean wPressed = false;
public static boolean isWPressed() {
    synchronized (StartingPoint.class) {
        return wPressed;
    }
}
//for position of circle
int x = 0;
int y= 0;

//for position change
double dx = 2;
double dy = 2;

//for circle size
int rad = 11;

//image for update()
private Image i;
private Graphics gTwo;

//Physics
double grav = 15;
double engloss= .65;
double tc = .2;
double friction = .9;



@Override
public void init() {
    // sets window size
    setSize(800,600);


}

@Override
public void start() {
    //"this" refers to the implemented run method
    Thread threadOne = new Thread(this);
    //this goes to run()
    threadOne.start();

}

@Override
public void run() {

    String input = JOptionPane.showInputDialog( "How much gravity?" );
    grav=  Double.parseDouble(input);
    // sets frame rate
    while (true){
        //makes sure it doesn't go off screen x wise
        //right side
        if (x + dx > this.getWidth() - rad -1){
            x= this.getWidth() -rad -1; //blocks it from moving past boundary
            dx = -dx; //Reveres it 
        }
        //left side
        else if (x + dx < 1 + rad){
            x= 1+rad; //ball bounces from the center so it adjusts for this by adding one and rad to pad out the radius of the ball plus one pixel.
            dx = -dx; //Inverters its movement so it will bounce
        }
        //makes the ball move
        else{
            x += dx; // if its not hitting anything it keeps adding dx to x so it will move. 


        }



        //friction
        if(y == this.getHeight()-rad -1){
            dx *= friction; //every time the ball hits the bottom dx is decreased by 10% by multiplying by .9
            //Keeps it from micro bouncing for ever
            if (Math.abs(dy) < 4){ // if the speed of y (dy) is less than .4 it is set to 0

                dy= 0;

            }

            /**if (Math.abs(dx) < .00000000000000000001){ // if the speed of x (dx) is less than .00000000000000000001 it is set to 0, this value doesn't seem to matter
                dx = 0;


            }**/
        }


        //makes sure it doesn't go off screen y wise
        //down
        if (y > this.getHeight() - rad -0){ // TODO Check how getHieght is measured.
            y= this.getHeight() -rad -0;
            //makes ball loose speed.
            dy *= engloss;
            dy = -dy;

        }
        else {
            //velocity
            // tc makes grav smaller. Total of which is added to dy. To increase velocity as the ball goes down.
            dy += grav *tc;
            //gravity
            //new dy is decreased by tc + .5 multiplied by gravity and tc squared. This makes the ball bounce lower every time based on its speed
            y += dy*tc + .5*grav*tc*tc;
        }


        //frame rate
        repaint();
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //end frame rate

}

@Override
public void stop() {

}

@Override
public void destroy() {

}

@Override
public void update(Graphics g) {
    //keeps it from flickering... don't know how though
    if(i == null){
        i = createImage(this.getSize().width, this.getSize().height);
        gTwo = i.getGraphics();
    }
    gTwo.setColor(getBackground());
    gTwo.fillRect(0, 0, this.getSize().width, this.getSize().height);

    gTwo.setColor(getForeground());
    paint(gTwo);

    g.drawImage(i, 0, 0, this);

    //do some thing with setDoubleBuffered


}

@Override
public void paint(Graphics g) {
    g.setColor(Color.BLUE);
    g.fillOval(x-rad, y-rad, rad*2, rad*2);
}




}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    You should keep the swing threading schema in mind, only use the swing event thread for swing methods: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ – Ferrybig Feb 02 '16 at 13:12
  • @DavidG How so? The only thing I am using from swing is JOptionPane, I tried running the program without it but I still have the same issue. – Lightning Jimmy Joe Johnson Feb 02 '16 at 23:26
  • No point asking me, all I did was edit out the profanity you had in the question... – DavidG Feb 02 '16 at 23:28
  • @DavidG oh thanks for that. – Lightning Jimmy Joe Johnson Feb 03 '16 at 01:52
  • 1
    1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 03 '16 at 03:07
  • *"The only thing I am using from swing is JOptionPane,"* Was apparently in reply to @Ferrybig. Note that both Swing and AWT apps. use the Event Dispatch Thread, so that advice is relevant to both. *"I tried running the program without it but I still have the same issue."* Comments are for things that are 'not an answer'. While Ferrybig was not trying to answer the question, they were offering some valuable advice you should not ignore. – Andrew Thompson Feb 03 '16 at 03:09
  • *"`// sets window size`"* Not when it's in a browser. *"`setSize(800,600);`"* so this should be set by the HTML that embeds the applet. – Andrew Thompson Feb 03 '16 at 03:11

0 Answers0