NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
October 17, 2017, 08:28:03 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
An interdisciplinary approach. ...Wisdom
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

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

Posts: 1

«
Embed this message
on: March 02, 2011, 07:42:32 am » posted from:Springfield,Illinois,United States

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?
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #1 on: March 02, 2011, 10:08:16 am » posted from:Taipei,T\'ai-pei,Taiwan

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.
Logged
Pages: [1]   Go Up
  Print  
An interdisciplinary approach. ...Wisdom
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
cork ball in water
Question related to Physics or physics related simulation
pradeep123 2 6459 Last post June 26, 2010, 09:56:34 am
by hectorloshuk
Ball falling in the sand
Request for physics Simulations
GwenArBreizhou 2 5487 Last post October 21, 2010, 02:54:24 pm
by nagftre
Elastic bouncing ball
kinematics
ahmedelshfie 0 4596 Last post May 27, 2010, 07:18:10 pm
by ahmedelshfie
Elastic ball
Request for physics Simulations
ahmedelshfie 4 4682 Last post June 20, 2010, 01:10:08 am
by ahmedelshfie
Throwing a ball
Simulations from other web sites
ahmedelshfie 0 6134 Last post February 08, 2011, 07:05:51 pm
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.062 seconds with 22 queries.since 2011/06/15