PDA

View Full Version : Java Bouncy Ball: Physics Problem


relion65
Jan4-12, 09:57 PM
I'm having a bit of a problem with physics calculation for a bouncy ball program im creating in java. Ive been java programming for almost three years, and excelled in my class, but when it comes to physics for java games and what not, im totally useless. Heres the problem: a Ball, when put on screen, does not behave how it should; i checked my code... in theory... this code works, but it doesnt obviously. itll move down at the input angle, bounce, but then immediately go back down and disappear. NOTE: some of the physics calculation need radians for input, but im familiar with degrees more, so the var angle should always be in degrees. if u catch somewhere in this code a radian value being added to the angle var, please let me know.
class Ball
{
double x,y,diam,angle,yspd,grav;
Rectangle rect;//used for collision detection, whenever i get to it.
Color col;
public Ball(double x,double y,double diam,double angle,Color col,Dimension dim)
{
this.x = x;
this.y = y;
this.diam = diam;
this.angle = angle;//starting angle in degrees
this.col = col;
yspd = 1.0;//controls acceleration on y-axis
grav = 1.25;
rect = new Rectangle((int)x,(int)y,(int)diam,(int)diam);
}
public void draw(Graphics g)
{
g.setColor(col);
g.fillOval((int)x,(int)y,(int)diam,(int)diam);
calcXY();
}
public void calcXY()
{
if(yspd>11)
{
yspd = 11;//caps the value at 11, much higher and it freaks out.
}
if(y+diam>dim.height)//from here down is where the problems are, dont know where
{
angle-=90;//this slightly works
yspd*=-1;//recalculating the angle isnt enough for java, so i have to reverse yspd
}
else if(x+diam>dim.width||x<dim.width)
{
angle-=90;//again, in theory, this should work
}
else
{
angle = Physics.calcAngle(x,y,diam,dim.height);//updates the angle var for current position
System.out.println(angle);//for seeing if my physics calculations are correct
}
if(angle>359)
{
angle = 0;
}//here is where the x and y coords are updated, and where most likely the problem is...
x+=5+Physics.calcAngleMoveX(angle));
y+=((yspd+=grav)+Physics.calcAngleMoveY(angle));
rect.setLocation((int)x,(int)y);//updates collisions rectangle
}
}
This is the physics class i created:
public abstract class Physics
{
public static double calcAngle(double x,double y,double diam,double height)
{
//in short, it creates a virtual right triangle, with a base of 10 pixels.
Point one = new Point(x+diam,y+diam);//the ball
Point two = new Point(x+diam,height);
Point three = new Point(x+diam+10,height);
//then it calculates the length of the legs
double s1 = dist(one.x,one.y,two.x,two.y);
double s2 = dist(two.x,two.y,three.x,three.y);
//finally, it uses arc tangent to calculate the angle of the collision
double angle = angle(s1,s2);
return angle;
}
public static double calcAngleMoveX(double angle)
//some method i got from a java programming book that supposedly adds realistic motion to an object
{
return (double)(Math.cos(angle*Math.PI/180));
}
public static double calcAngleMoveY(double angle)
//same thing with this one
{
return (double)(Math.sin(angle*Math.PI/180));
}
public static double dist(double x1,double y1,double x2,double y2)
//simple: the Distance formula used in geometry.
{
return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
public static double angle(double s1,double s2)
//the final angle calculation using arc tangent
{
return Math.atan(s1/s2);
}
}
class Point
{
double x,y;
public Point(double x,double y)
{
this.x = x;
this.y = y;
}
}

Mark44
Jan4-12, 10:39 PM
I'm having a bit of a problem with physics calculation for a bouncy ball program im creating in java. Ive been java programming for almost three years, and excelled in my class, but when it comes to physics for java games and what not, im totally useless. Heres the problem: a Ball, when put on screen, does not behave how it should; i checked my code... in theory... this code works, but it doesnt obviously. itll move down at the input angle, bounce, but then immediately go back down and disappear. NOTE: some of the physics calculation need radians for input, but im familiar with degrees more, so the var angle should always be in degrees. if u catch somewhere in this code a radian value being added to the angle var, please let me know.
class Ball
{
double x,y,diam,angle,yspd,grav;
Rectangle rect;//used for collision detection, whenever i get to it.
Color col;
public Ball(double x,double y,double diam,double angle,Color col,Dimension dim)
{
this.x = x;
this.y = y;
this.diam = diam;
this.angle = angle;//starting angle in degrees
this.col = col;
yspd = 1.0;//controls acceleration on y-axis
grav = 1.25;
rect = new Rectangle((int)x,(int)y,(int)diam,(int)diam);
}
public void draw(Graphics g)
{
g.setColor(col);
g.fillOval((int)x,(int)y,(int)diam,(int)diam);
calcXY();
}
public void calcXY()
{
if(yspd>11)
{
yspd = 11;//caps the value at 11, much higher and it freaks out.
}
if(y+diam>dim.height)//from here down is where the problems are, dont know where
{
angle-=90;//this slightly works
yspd*=-1;//recalculating the angle isnt enough for java, so i have to reverse yspd
}
else if(x+diam>dim.width||x<dim.width)
{
angle-=90;//again, in theory, this should work
}
else
{
angle = Physics.calcAngle(x,y,diam,dim.height);//updates the angle var for current position
System.out.println(angle);//for seeing if my physics calculations are correct
}
if(angle>359)
{
angle = 0;
}//here is where the x and y coords are updated, and where most likely the problem is...
x+=5+Physics.calcAngleMoveX(angle));
y+=((yspd+=grav)+Physics.calcAngleMoveY(angle));
rect.setLocation((int)x,(int)y);//updates collisions rectangle
}
}
This is the physics class i created:
public abstract class Physics
{
public static double calcAngle(double x,double y,double diam,double height)
{
//in short, it creates a virtual right triangle, with a base of 10 pixels.
Point one = new Point(x+diam,y+diam);//the ball
Point two = new Point(x+diam,height);
Point three = new Point(x+diam+10,height);
//then it calculates the length of the legs
double s1 = dist(one.x,one.y,two.x,two.y);
double s2 = dist(two.x,two.y,three.x,three.y);
//finally, it uses arc tangent to calculate the angle of the collision
double angle = angle(s1,s2);
return angle;
}
public static double calcAngleMoveX(double angle)
//some method i got from a java programming book that supposedly adds realistic motion to an object
{
return (double)(Math.cos(angle*Math.PI/180));
}
public static double calcAngleMoveY(double angle)
//same thing with this one
{
return (double)(Math.sin(angle*Math.PI/180));
}
public static double dist(double x1,double y1,double x2,double y2)
//simple: the Distance formula used in geometry.
{
return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
public static double angle(double s1,double s2)
//the final angle calculation using arc tangent
{
return Math.atan(s1/s2);
}
}
class Point
{
double x,y;
public Point(double x,double y)
{
this.x = x;
this.y = y;
}
}

Are you taking into account the fact that atan returns an angle in radians between -\pi/2 and +\pi/2? I see this function being used in your angle method, and the angle method is called by the calcAngle method.

relion65
Jan5-12, 06:53 PM
well, i fixed that... and it still doesnt work... its probably how im calculating the new x and y velocities... idk tho...

jgabase
Feb6-12, 03:53 AM
Why do you think you need to work with angles? You should better work with the horizontal and vertical movements separately.

chiro
Feb6-12, 05:39 AM
Hey relion65.

I'm just curious as to why you didn't use a simple integrator to do your physics. It doesn't have to be anything fancy (an Euler one would do for this example).

The first question I have for you is do you want to do more complex demos later on that simulate physics?

Apart from the other comments above I have noticed that if your angle is > 359 you set it to zero. This doesn't make sense because if you want to have an angle that is representative of the main branch (between 0 and 359 inclusive) then you have to calculate (angle) MOD 360 which returns the angle in the principle branch that represents the same information as it was in another branch.