Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Physics between two balls, incorrect calculated angle. Help!

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Physics between two balls, incorrect calculated angle. Help!

    I've been stuck on this one for a while. If you scroll down to the while loop there is some code that calculates the angle of a triangle created by two circles colliding. The problem is, The angle between the hypotenuse / x axis, and the angel between the hypotenuse / y axis never go above 65 degrees and i have no clue why, please help!

    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    public class MainTest extends JFrame {
        public rectt g = new rectt();
     
        public static void main( String[] args ) throws InterruptedException {
            MainTest t = new MainTest();
     
            int particles = 2;
            int radius =100;
            Random number = new Random();
            t.setSize( 1800, 1000 );
            t.setVisible( true );
            t.getContentPane().add( t.g );
            t.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            int xVelocity[] = new int[particles+1];
            int yVelocity[] = new int[particles+1];
            int mass[] = new int[particles+1];
            for(int z=1;z<particles+1;z++){
            	xVelocity[z]= (20-number.nextInt(40));
            	yVelocity[z]= (20-number.nextInt(40));
                mass[z]=5;
            }
     
     
            while ( true ) {
            	for(int z=1;z<particles+1;z++){
            		if (t.g.x[z] <=0 + radius/2|| t.g.x[z]>=1600-radius/2){
            			xVelocity[z] = xVelocity[z]*-1;
            		}
            		if (t.g.y[z] <=0 || t.g.y[z]>=900){
            			yVelocity[z] = yVelocity[z]*-1;
            		}
            		if (t.g.y[z]>=900){
            			yVelocity[z]= yVelocity[z]-1;
            		}
            		for (int c=1;c<particles+1;c++){
            			String temp1 = Integer.toString(t.g.x[c]);
            			String temp2 = Integer.toString(t.g.x[z]);
            			String temp3 = Integer.toString(t.g.y[c]);
            			String temp4 = Integer.toString(t.g.y[z]);
            			Double temp11 = Double.parseDouble(temp1);
            			Double temp22 = Double.parseDouble(temp2);
            			Double temp33 = Double.parseDouble(temp3);
            			Double temp44 = Double.parseDouble(temp4);
     
            			if (Math.sqrt(Math.pow(temp11-temp22,2)+Math.pow(temp33-temp44,2))<radius && Math.sqrt(Math.pow(temp11-temp22,2)+Math.pow(temp33-temp44,2)) !=0 && (temp11-temp22) !=0 && (temp33-temp44) !=0){
            				double hypotenuse = Math.sqrt(Math.pow(temp11-temp22,2)+Math.pow(temp33-temp44,2));
            				double x = (temp11-temp22);
            				double y = (temp33-temp44);
            				double hypx = Math.asin(((Math.sin(90)/hypotenuse)*y));
            				//double hypx = Math.acos((Math.pow(hypotenuse,2)-Math.pow(x,2)-Math.pow(y,2))/-2*x*y);
            				double hypy = Math.asin(((Math.sin(90))/hypotenuse)*x);
            				//System.out.println(Math.abs(Math.toDegrees(hypx))+90+Math.abs(Math.toDegrees(hypy)));
            				System.out.println(Math.toDegrees(hypx));
            				try {
            		                Thread.sleep(100);
            		            } catch ( InterruptedException e ) {
            		            }
                		}
            		}
     
            		yVelocity[z]= yVelocity[z]+1;
     
            		t.g.x[z] = t.g.x[z] + xVelocity[z];
                    t.g.y[z] = t.g.y[z] + yVelocity[z];
                    t.repaint();
            	}
                try {
                    Thread.sleep( 10);
                } catch ( InterruptedException e ) {
                }
            }
    }
     
        public void paintComponent( Graphics g ) {
            //g.clearRect( 0, 0, 0, 0 );
        }
    }
     
     
    class rectt extends JComponent {
     
        public static final long serialVersionUID = 1L;
     
        public int particles =2;
        public int radius =100;
     
        public int x[] = new int[particles+1];
        public int y[] = new int[particles+1];
        public int width[] = new int[particles+1];
        public int height[] = new int[particles+1];
        int done=0;
     
        public void paintComponent( Graphics g ) {
            g.setColor( Color.black );
            if (done==0){
            for(int z=1;z<particles+1;z++){
            	x[z]=800;
            	y[z]=500;
            	width[z]=radius;
            	height[z]=radius;
            	done=1;
            }
            }
            for(int z=1;z<particles+1;z++){
            	g.fillOval(x[z]-radius/2, y[z]-radius/2, width[z], height[z]);;
            	g.drawLine(1600, 0, 1600, 1000);
            	g.drawLine(x[z], y[z], x[z-1], y[z-1]);
            }
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Physics between two balls, incorrect calculated angle. Help!

    What variable is not getting the expected value? What values does it get?
    Can you post a table of rows with two columns, one column showing the expected value the other showing the value that is incorrectly computed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Physics between two balls, incorrect calculated angle. Help!

    Quote Originally Posted by Norm View Post
    What variable is not getting the expected value? What values does it get?
    Can you post a table of rows with two columns, one column showing the expected value the other showing the value that is incorrectly computed?
    Ok so the two angles that i am calculating are hypx(the angle between the hypotenuse and the x axis), and hypy(the angle between the hypotenuse and the y axis). If you run the code you can see that when the two balls are almost vertical the hypx should be outputting a near 90 degree angle, but instead it outputs a near 65 degree angle. Likewise, when the balls are near horizontal to each other the hypy should be outputting a near 90 degree, but instead a near 65 degree angle is being outputted.

    hopefully that should provide some insight.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Physics between two balls, incorrect calculated angle. Help!

    Can you make a table that shows the values as I requested?

    How are you debugging the code to see why it is not computing the desired values?


    I suggest that you make a special tester that you can feed a range of values to and get a result that can be compared to a known correct value. When an incorrect value is returned, then the details of the algorithm needs to be closely checked to see why.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Physics between two balls, incorrect calculated angle. Help!

    Also the code has hardcoded values for a larger screen than I am using. It'd be easier to tailor the code for a given screen size if the width and height were obtained from variables instead of being hardcoded.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Distance not being calculated properly
    By floorplay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 18th, 2013, 08:54 AM
  2. Calculation of Reflection Angle
    By Ecen in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 23rd, 2013, 01:27 PM
  3. NEED SIN OF ANGLE
    By CLAYAROBINSON in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: June 2nd, 2011, 10:14 PM
  4. Graphic angle
    By CoffeeBeans in forum Java Theory & Questions
    Replies: 1
    Last Post: August 22nd, 2010, 11:28 AM
  5. [SOLVED] Physics Program
    By pwngrammer in forum Algorithms & Recursion
    Replies: 36
    Last Post: June 15th, 2009, 08:32 AM

Tags for this Thread