- #1
relion65
- 5
- 0
I'm having a bit of a problem with physics calculation for a bouncy ball program I am creating in java. I've been java programming for almost three years, and excelled in my class, but when it comes to physics for java games and what not, I am 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 doesn't 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 I am 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.
This is the physics class i created:
Code:
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, don't know where
{
angle-=90;//this slightly works
yspd*=-1;//recalculating the angle isn't 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
}
}
Code:
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;
}
}