Why Does My Java Bouncy Ball Not Behave as Expected?

In summary, the Ball doesn't behave as it should when it is put on the screen. The code seems to work in theory, but when I run it, it doesn't move the way it is supposed to. I think the problem might be with the angle variable, because if I change it to degrees, the program works fine.
  • #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.
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
        }
    }
This is the physics class i created:
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;
    }
}
 
Technology news on Phys.org
  • #2
relion65 said:
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.
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
        }
    }
This is the physics class i created:
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;
    }
}

Are you taking into account the fact that atan returns an angle in radians between -[itex]\pi/2[/itex] and +[itex]\pi/2[/itex]? I see this function being used in your angle method, and the angle method is called by the calcAngle method.
 
  • #3
well, i fixed that... and it still doesn't work... its probably how I am calculating the new x and y velocities... idk tho...
 
  • #4
Why do you think you need to work with angles? You should better work with the horizontal and vertical movements separately.
 
  • #5
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.
 

1. How does the Java Bouncy Ball simulation work?

The Java Bouncy Ball simulation uses the laws of physics, specifically Newton's laws of motion, to model the behavior of a bouncing ball. The code calculates the ball's position, velocity, and acceleration at each time step, taking into account factors such as gravity, air resistance, and collisions with other objects.

2. Can I change the parameters of the simulation?

Yes, you can change the parameters of the simulation such as the initial velocity, size of the ball, and gravity. These parameters can be adjusted in the code, allowing you to customize the simulation according to your needs.

3. How accurate is the simulation?

The Java Bouncy Ball simulation is an accurate representation of the physics principles it is based on. However, due to limitations in the code and computer processing power, there may be slight discrepancies between the simulation and real-life scenarios.

4. Can I use this simulation for educational purposes?

Yes, this simulation can be a useful tool for educational purposes. It can help students understand the concepts of physics, such as motion, forces, and energy, in a visual and interactive way.

5. Is the Java Bouncy Ball simulation realistic?

The simulation is based on real-world physics principles, so it is as realistic as the parameters and conditions set in the code. However, it is important to note that it is a simplified model and may not account for all variables and complexities in real-life scenarios.

Similar threads

  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top