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

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

1. 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;
Random number = new Random();
t.setSize( 1800, 1000 );
t.setVisible( true );
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++){
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 {
} 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 {
} 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 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;
done=1;
}
}
for(int z=1;z<particles+1;z++){
g.drawLine(1600, 0, 1600, 1000);
g.drawLine(x[z], y[z], x[z-1], y[z-1]);
}

}
}```

2. 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?

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

Originally Posted by Norm
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. 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.

5. 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.