NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Information about this web site => Question related to Physics or physics related simulation => Topic started by: relion65 on March 02, 2011, 07:42:32 am

 Title: Bouncy Ball/board:29-101- Post by: relion65 on March 02, 2011, 07:42:32 am I am trying to create a game for my java class that involves trying to get a bouncing ball thru a hole... something like pong, i guess. since this is my second freshman year and i dont know how to take an algorithm and turn it into code, im kinda stuck. Well, i was able to create a Ball class based off of wat i could understand from searching google... and for my other bouncy ball related game, it works just fine, but its not good enough for this application. The ball follows an arc, like a real bouncy ball would do... but each successive bounce is the same height as the last, so the physics of the ball wont allow it to go thru the hole in the wall... it realizes its "touching" the wall and not the opening anymore, and it reverses its direction. the ball needs to be more... i guess you could say "elastic", if thats correct... so its doesnt follow the same arc path every time. just for clarification because im terrible at getting my point across for people to understand... it follows the same drop angle path everytime... so the hole in the wall would have to be huge for it to make it thru with the current physics algorithm(if you could call it that)that i made. here's the code for class Ball:Code:class Ball{    int x,y,diam;//starting coordinates and diameter    double xspd,yspd,fric,grav;// x-axis speed, y-axis speed, friction, gravity    Rectangle rect;//the bounding rectangle that is used to check for collisions with this instance    Dimension dim;//set to the dimensions of the screen for checking for collisions with the edges of the screen    Random rnd;//used to generate the random color    Color col;//the color used to paint the ball    public Ball(int x,int y,double xspd,double yspd,double fric,double grav,int diam,Dimension dim,Random rnd)    {        this.x = x;        this.y = y;        this.xspd = xspd;        this.yspd = yspd;        this.fric = fric;        this.grav = grav;        this.diam = diam;        this.dim = dim;        rect = new Rectangle(x,y,diam,diam);        this.rnd = rnd;        col = new Color(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255));    }    public void draw(Graphics g,Rectangle r1,Rectangle r2,Rectangle o)    {//uses the graphics from the buffered image to paint the ball to the screen, and passing on 3 rectangles        g.setColor(col);        g.fillOval(x,y,diam,diam);        calcXY(r1,r2,o);    }    //calcXY does just as it says... calculates the new x and y coordinates    //also, the three rectangles passed on are the positions of wall1, the opening between the two walls, and wall2    public void calcXY(Rectangle r1,Rectangle r2,Rectangle o)    {        if(yspd>=15)        {            yspd = 15;//limits the yspd so that it doesnt get over 15        }        if(y-diam>=dim.height||y+diam<=1)        {            yspd*=-1;//if it touches the top or the bottom of the screen, taking in account for diameter of the ball(thats where the dimension comes in) then reverse directions        }        if(x-diam>=dim.width||x+diam<=1)        {            xspd*=-1;//same thing as above, but for the x axis        }        if(rect.intersects(r1)||rect.intersects(r2)&&!rect.intersects(o))        {            xspd*=-1;//if it touches wall one or wall two and does not touch the opening, reverse directions//i did that so that the ball wouldnt just bounce off the opening between the two walls, like it did before i integrated the opening rect        }        if(fric>=3)        {            fric = 3;//dont mind this... this was something i put in to test something out        }        x+=xspd-fric;//what calculates the new x coordinate        y+=grav+(yspd+=grav-fric);//what calculates the new y coordinate        rect.setLocation(x,y);//set the new location of the bounding rectangle to properly check for collisions    }could somebody please help me? Title: Re: Bouncy Ball/board:29-101- Post by: Fu-Kwun Hwang on March 02, 2011, 10:08:16 am It will be much better if you can attach some screen shot of the program you have created,and explain the problem with those images.