NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
April 20, 2021, 05:10:59 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register  
Life well spent is long. ..."da Vinci (1452-1519, Italian artist, sculptor, painter, architect, engineer and scientist) "
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
Author Topic: Bouncy Ball/board:29-101-  (Read 4813 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
Offline Offline

Posts: 1

Embed this message
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:
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
  //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)
  yspd = 15;//limits the yspd so that it doesnt get over 15
  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
  xspd*=-1;//same thing as above, but for the x axis
  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
  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?
Fu-Kwun Hwang
Hero Member
Offline Offline

Posts: 3086

Embed this message
Reply #1 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.
Pages: [1]   Go Up
Life well spent is long. ..."da Vinci (1452-1519, Italian artist, sculptor, painter, architect, engineer and scientist) "
Jump to:  

Related Topics
Subject Started by Replies Views Last post
spinny ball after bounce
Question related to Physics or physics related simulation
cam 9 16965 Last post January 17, 2013, 06:16:41 pm
by ahowe42
cork ball in water
Question related to Physics or physics related simulation
pradeep123 2 7626 Last post June 26, 2010, 09:56:34 am
by hectorloshuk
Ball falling in the sand
Request for physics Simulations
GwenArBreizhou 2 6686 Last post October 21, 2010, 02:54:24 pm
by nagftre
Elastic bouncing ball
ahmedelshfie 0 5478 Last post May 27, 2010, 07:18:10 pm
by ahmedelshfie
Elastic ball
Request for physics Simulations
ahmedelshfie 4 6122 Last post June 20, 2010, 01:10:08 am
by ahmedelshfie
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.254 seconds with 23 queries.since 2011/06/15