This is java syntax, but anybody can read it, so I'm posting it here. More of a math question.

Basically, I'm handling ball (molecule) collisions.

java Code:// other is the other molecule we're colliding with // x and y distances between molecules double xDist = this.getX() - other.getX(); double yDist = this.getY() - other.getY(); // get the distance between circle centers double hypotenuse = Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2)); // create the line tangent to the two circles (through the collision point) and get its direction double tangentAngle = Math.toDegrees(Math.atan(Math.abs(yDist / xDist))) + 90; if (yDist < 0 && xDist != 0) { tangentAngle += 90; tangentAngle %= 360; } // rounds to nearest decimal if it's very, very close to it hypotenuse = MyMath.roundIfClose(hypotenuse); tangentAngle = MyMath.roundIfClose(tangentAngle); // rotate the two molecules until the line tangent to the circles is vertical double toAdd = 90 - tangentAngle; double thisNewTheta = (this.getVel().getTheta() + toAdd + 360) % 360; double otherNewTheta = (other.getVel().getTheta() + toAdd + 360) % 360; // get the speed of the molecule in the x direction (toward the tangent line is x direction) double v1 = this.getVel().getSpeed() * Math.cos(Math.toRadians(thisNewTheta)); double v2 = other.getVel().getSpeed() * Math.cos(Math.toRadians(otherNewTheta)); // masses double m1 = this.getMass(); double m2 = other.getMass(); // new velocities based on mass double newv1 = ((v1*(m1 - m2)) + 2 * m2 * v2) / (m1 + m2); double newv2 = ((v2*(m2 - m1)) + 2 * m1 * v1) / (m1 + m2); // get the speed of the molecule in the y direction (up and down the tangent line, not toward it) double thisY = MyMath.roundIfClose(this.getVel().getSpeed() * Math.sin(Math.toRadians(thisNewTheta))); double otherY = MyMath.roundIfClose(other.getVel().getSpeed() * Math.sin(Math.toRadians(otherNewTheta))); // gets the new direction of the molecules - shown later double newt1 = getThetaAfterCollision(newv1, thisY, toAdd, true); double newt2 = getThetaAfterCollision(newv2, otherY, toAdd, false); // sets directions this.getVel().setTheta(newt1); other.getVel().setTheta(newt2); // gets the new speeds double thisNewSpeed = MyMath.roundIfClose(Math.sqrt(Math.pow(newv1, 2) + Math.pow(thisY, 2))); double otherNewSpeed = MyMath.roundIfClose(Math.sqrt(Math.pow(newv2, 2) + Math.pow(otherY, 2))); // and sets them this.getVel().setSpeed(thisNewSpeed); other.getVel().setSpeed(otherNewSpeed);

And here is the collision angle returning method

java Code:double getThetaAfterCollision(double xChange, double yChange, double toAdd, boolean thisMolecule) { double theta = 0; if (yChange == 0) { // the molecule is traveling perpendicularly to the tangent line if (xChange > 0) { theta = -toAdd; // bounce perpendicularly off the tangent line } else if (xChange < 0) { theta = 180 - toAdd; // bounce perpendicularly off the tangent line } else { theta = 0; } } else { theta = (Math.toDegrees(Math.atan(xChange / yChange)) - toAdd + 360) % 360; if (xChange < 0 && yChange != 0) { theta += 180; theta %= 360; } } System.out.println("New theta for " + (thisMolecule == true ? "this" : "other") + " molecule: " + theta); return theta; }

My math doesn't look too right. Can anyone help out a bit?

It works a lot of the time (~80%) but sometimes it just does completely wrong things.

agrees