Why Does My Java Bouncy Ball Not Behave as Expected?

  • Context: Java 
  • Thread starter Thread starter relion65
  • Start date Start date
  • Tags Tags
    Ball Java Physics
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to the physics calculations for a bouncy ball simulation. Participants explore the behavior of the ball in the program, particularly its movement and collision responses, while addressing the challenges of implementing physics in a game context.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • The ball moves down at the input angle but bounces and then disappears, suggesting issues with the physics calculations.
  • Some participants note that the angle is intended to be in degrees, while certain calculations may require radians, leading to potential confusion.
  • There is a mention of a cap on the vertical speed (yspd) at 11, which is said to prevent erratic behavior.
  • One participant suggests that reversing the vertical speed (yspd) upon collision is necessary, but questions remain about the effectiveness of this approach.
  • Another point raised is the calculation of the angle during collisions, which is derived from a virtual right triangle, but its accuracy is questioned.
  • Participants discuss the need for updating the angle based on the ball's position, but there is uncertainty about how this impacts the ball's movement.
  • There is a specific inquiry about whether any radian values are being incorrectly applied to the angle variable.

Areas of Agreement / Disagreement

Participants express various concerns and hypotheses about the ball's behavior, indicating that multiple competing views remain regarding the correct implementation of the physics calculations. The discussion does not reach a consensus on the issues presented.

Contextual Notes

There are limitations in the current understanding of how the angle calculations interact with the ball's movement and collision detection. Additionally, the dependence on degrees versus radians in the calculations is a significant point of contention.

relion65
Messages
5
Reaction score
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
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 -\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.
 
well, i fixed that... and it still doesn't work... its probably how I am calculating the new x and y velocities... idk tho...
 
Why do you think you need to work with angles? You should better work with the horizontal and vertical movements separately.
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K