Java Why Does My Java Bouncy Ball Not Behave as Expected?

AI Thread Summary
The discussion revolves around a Java programming issue related to simulating the physics of a bouncy ball. The primary problem is that the ball does not behave as expected; it moves downward at the specified angle, bounces, but then disappears. The programmer notes that while the code appears theoretically sound, it fails in practice. Key points of concern include the handling of angles, as calculations require radians but the programmer is more comfortable with degrees. The physics calculations involve updating the ball's position based on its angle and speed, but there are issues with how the new x and y velocities are computed. Suggestions from others include using a simple integrator for physics simulation and ensuring angle calculations are properly constrained using modulo operations. The discussion highlights the importance of separating horizontal and vertical movements for better accuracy in physics simulations.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top