Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: 2D Ball Animation

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2D Ball Animation

    I have been trying to figure out what is wrong with the program, and I just cannot figure it out for the life of me. The program is designed to handle any 2D motion a ball can undergo (ignoring friction). For some reason, after a vertical collision, the ball will not reach its starting height again. This should not be happening since the ball is undergoing an elastic collision. Any help would be appreciated, the code is posted below (I believe the error has to be in the actionlistener function):
    package animation;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferStrategy;
     
    //Class is used to create a jPanel animation of a ball in motion
    public class Animation extends JFrame {
        int dim = 50; //range [50, 400]
        private double G = 9.81; //range [-20, 20]
        private double time = 0; 
        private double yvel = 0*(400/dim); //range [-40, 40]
        private double xvel = 0*(400/dim); //range [-40, 40]
        private double ypos = 400 - 40*(400/dim); //range [0, 400]
        private double xpos = 25*(400/dim); //range [0, 400]
        private double ballh = 2*(400/dim); //range [1, 10]
        private double ballw = 2*(400/dim); //range [1, 10]
        private double cores = 1; //range [0, 1]
     
    // ActionListener is used to perform time based calculations for the ball's position/velocity    
      java.awt.event.ActionListener Move = new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) { 
              time += 0.01;
              int flag = 0;
              //determine if a collsion has occurred
              if (ypos >= (400 - ballh)) {
                  yvel = -Math.abs(yvel*cores);
                  ypos = 400 - ballh;
                  flag = 1;
              }
              else if (ypos <= ballh) {
                  yvel = Math.abs(yvel*cores);
                  ypos = ballh;
              }
              if (xpos >= (400 - ballw)) {
                  xvel = -Math.abs(xvel*cores);
                  xpos = 400 - ballw;
              }
              else if (xpos <= 0) {
                  xvel = Math.abs(xvel*cores);
                  xpos = 0;
              }
              //update values
              ypos += ((yvel*0.01) + (0.5*G*0.001*(400/dim)));
              if (flag == 0) {
                  yvel += (0.01*G)*(400/dim);
              }
              xpos += xvel*0.01;
              repaint();
              if (flag == 1) {
                  System.out.println("Collision has occurred.");
              }
              System.out.println(yvel);
              System.out.println(ypos);
              System.out.println();
          }
      };
     
    //Timer used to call ActionListener
      Timer timer = new Timer(10, Move);
     
    //Constructor initializes JFrame and its properties
        public Animation() {
            super("Animation");
            setSize(400,400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setResizable(false);
            setBackground(Color.WHITE);
            createBufferStrategy(2);
            timer.start();
        }
     
    //Paint function draws the board to a buffer image, then copies over to the screen    
        public void paint(Graphics g) {
            Graphics buffer;
            Image bufferImage;
            bufferImage = createImage(400,400);
            buffer = bufferImage.getGraphics();
            buffer.setColor(Color.red);
            buffer.fillOval((int) xpos, (int) ypos, (int) ballh, (int) ballw);
            g.drawImage(bufferImage, 0, 0, this);
        }
     
    //Function sets values for the animation    
        public void setValues(double gravity, double yvelocity, double xvelocity, double yposition, double xposition, double diameter, double restitution, int dimensions) {
            if ((gravity <= 20 && gravity >= -20) && (yvelocity <= 40 && yvelocity >= -40) && (xvelocity <= 40 && xvelocity >= -40) && (yposition <= 400 && yposition >= 0) && (xposition <= 400 && xposition >= 0) && (diameter <= 10 && diameter >= 1) && (restitution <= 1 && restitution >= 0) && (dimensions <= 400 && dimensions >= 50)) {
                    G = gravity;
                    yvel = yvelocity*(400/dimensions);
                    xvel = xvelocity*(400/dimensions);
                    cores = restitution;
                    dim = dimensions;
                    ballh = diameter*(400/dimensions);
                    ballw = diameter*(400/dimensions);
                    if (yposition < ballh) {
                        ypos = ballh;
                    }
                    else if (yposition > 400 - ballh) {
                        ypos = 400 - ballh;
                    }
                    else {
                        ypos = yposition;
                    }
                    if (xposition > 400 - ballw) {
                        xpos = 400 - ballw;
                    }
                    else {
                        xpos = xposition;
                    }
            }
            else {
                System.out.println("Values out of range, keeping previous values.");
            }
        }
     
    //Function sets the default values depending on the lesson    
        public void setLessonDefaults (int lesson) {
            switch (lesson) {
                case 1: lesson = 1;
                    setValues(9.81, 0, 0, 100, 200, 2, 0, 50);
                    break;
                case 2: lesson = 2;
                    setValues(9.81, 0, 0, 100, 200, 2, 1, 50);
                    break;
                case 3: lesson = 3;
                    setValues(9.81, -10, 10, 384, 0, 2, 1, 50);
                    break;
                default:
                    System.out.println("Invalid lesson.");
                    break;
            }
        }
     
        public static void main(String[] args){
            Animation A = new Animation();
            A.setLessonDefaults(2);
        }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: 2D Ball Animation

    1. Use code tags
    2. Kindly post SSCCE

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D Ball Animation

    Dude what is your exact question ???
    Is ball not jumping back to its initial height is your question then answer is program is design to make the ball stationary slowly slowly after every collision with wall........

    If your doubt is something else then mention it properly where you getting error !!!

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: 2D Ball Animation

    Quote Originally Posted by AJAXx195 View Post
    Dude what is your exact question ???
    Is ball not jumping back to its initial height is your question then answer is program is design to make the ball stationary slowly slowly after every collision with wall........

    If your doubt is something else then mention it properly where you getting error !!!
    Did you carefully read OP's description? (S)he has clearly mentioned this.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D Ball Animation

    Quote Originally Posted by AJAXx195 View Post
    Dude what is your exact question ???
    Is ball not jumping back to its initial height is your question then answer is program is design to make the ball stationary slowly slowly after every collision with wall........

    If your doubt is something else then mention it properly where you getting error !!!
    Well, yes it is designed to use a coefficient of restitution; however, when I set that to one, the ball's bounce height is still slowly sinking. That is the problem...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2D Ball Animation

    Do you have an IDE with an interactive debugger that you can use to see why your code is not doing what you want it to do?
    If not, add some printlns to the code to show the values of variables as they change and to show the execution flow.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2D Ball Animation

    Here are some lines of code that may help you get more dense and useable output. Replace your printlns with something like this:
              if(ypos > Height - 20 || Math.abs(yvel) < 10) { // only show part of data
                 System.out.println("yvel=" + yvel + ", ypos=" + ypos);
              }

Similar Threads

  1. Boucing ball from stanford uni
    By NoviceJAVA in forum AWT / Java Swing
    Replies: 0
    Last Post: October 8th, 2011, 05:38 PM
  2. Cant get the ball to bounce around the canvass
    By RajivdeCosta in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 30th, 2011, 10:18 AM
  3. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  4. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  5. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM