swing - Java JFrame Bouncing Ball Physics Simulation- Ball Motion Conflicts Upon Creation of Creation -


so, right now, i'm able create multiple balls , update them through vector of objects , loop update each object independently. problem every ball after first, ball created seems affect momentum , position of other balls, causing balls abruptly change flight paths.

code creating frame , managing ball creation based on mouselistener:

public class framecreation extends jframe{     static vector<ballcreate> ballobjects = new vector<ballcreate>();     static timer timer;     point m1;     point m2;     static jframe frame1;     static mousehandler mouse;      public static void main(string args[]){         framecreation frame = new framecreation();         frame1 = new jframe("phyiscs test");         frame1.setvisible(true);         frame1.setsize(520,530);         frame1.setdefaultcloseoperation(frame1.exit_on_close);         mouse =  frame.new mousehandler();         frame1.addmouselistener(mouse);     }      public class mousehandler extends jpanel implements mouselistener{         public void mouseclicked(mouseevent e) {         }          @override         public void mousepressed(mouseevent e) {             m1 = e.getpoint();             system.out.println("mouse pressed");         }          @override         public void mousereleased(mouseevent e) {             m2 = e.getpoint();             thread querythread  = new thread(){                     double vx = m2.getx() - m1.getx();                     double vy = m2.gety() - m1.gety();                      public void run(){                         createball(m1.getx(),m1.gety(),vx,vy);                     }                 };             querythread.start();         }          @override         public void mouseentered(mouseevent e) {         }          @override         public void mouseexited(mouseevent e) {         }     }      public void createball(double posx, double posy, double vx, double vy){         ballcreate newball = new ballcreate(posx,posy,50,vx,vy);         ballobjects.add(newball);         frame1.add(newball);         frame1.revalidate();         newball.repaint();     } } 

code drawing , updating balls:

public class ballcreate extends jpanel {         timer timer;     int diam;     color color;     double vx, vy, posx, posy;     final double g = 30;      public ballcreate(double posx, double posy, int diam, double vx, double vy){         this.posx = posx;         this.posy = posy;         this.vx = vx;         this.vy = vy;         this.diam = diam;         color = getrandomcolor();         timer = new timer(100, new movementupdate());         timer.start();     }      public void drawing(){         repaint();     }      public void motionupdate(){         for(ballcreate ball : framecreation.ballobjects){             double t = 0.1;             double dx = ball.vx*t;             double dy = (ball.vy + g*t)*t;             double v2y = g*t + ball.vy;             system.out.println("ball v2y: " + ball.vy);             ball.posx = ball.posx + dx;             ball.posy = ball.posy + dy;             vy = v2y;             drawing();             wallcollisioncheck();         }     }      @override     public void paintcomponent(graphics g){         super.paintcomponent(g);         for(ballcreate ball : framecreation.ballobjects){             g.setcolor(ball.color);             //system.out.println("pos x: " + posx);             //system.out.println("pos y: " + posy);             g.filloval((int)ball.posx, (int)ball.posy,                         ball.diam ,ball.diam);         }         }      public void wallcollisioncheck(){         for(ballcreate ball : framecreation.ballobjects){             double botbound = 500-(ball.posy + ball.diam);             if((botbound <=0 && vy>=0)|| (ball.posy <=0 && ball.vy <=0 )){                 //system.out.println("collision noted");                 //system.out.println("prev vy: " + ball.vy);                 ball.vy = ball.vy * -0.65;                 //system.out.println("post vy: " + ball.vy);                 ball.posy = 460;             }             if((ball.posx <= 0 && ball.vx <= 0) || ((math.abs(500 - ball.posx) <= 40)&& ball.vx >=0)){                 ball.vx = ball.vx * -0.65;              }             drawing();         }     }      public color getrandomcolor(){         int r = (int)(255*math.random());         int g = (int)(255*math.random());         int b = (int)(255*math.random());         color c = new color(r, g , b);         return c;      }     public class movementupdate implements actionlistener{         @override         public void actionperformed(actionevent e) {             for(ballcreate ball : framecreation.ballobjects){                 ball.motionupdate();             }         }     }    } 

i realize problem vague, it's hard describe without being able show image of happening. appreciated, , clarification of code can done if necessary. thanks.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -