Changing Algorithm - duongddinh/PoolTableSimulator GitHub Wiki
Dependencies: WriteLogF.java (to write log), DrawAsk.java (set-up screen) and FunctionsLol.java (contains functions used by both DrawAsk and DrawTheLuigi).
Changing DrawTheLuigi.java is recommended because it is the "main" file where all the calculations take place; however, you can change whatever you want, just make sure you know what you are doing.
Download DrawTheLuigi.java, Customizer.jar.
Remember to delete package libBezierFiveLines; at the top of each java file (if the file contains it).
Remember to put all your files in the same folder/directory as Customizer.jar
Note: Changing the algorithm will result in hash-mismatched error when running the program, which is not gonna affect the program. Hash-checking is there just to check if there is any modification to the program.
What to change?
Depends on what function you want to change or to improve.
Change the reflection line (DrawTheLuigi.java):
// calculate the reflection line by finding the length between perpend line and projected line
private void getPathSlope() {
//calculating the intersection of the path and 400 (y)
dapathx = (395 - toY)/( (supperBigNumber-toY)/((supperBigNumber/find_perpend_tan(toX))-toX)) + toX -4;
//slope line perpendicular to the path
double hps = (supperBigNumber-toY)/(supperBigNumber/((-1)*(400 - toX)/Math.abs(400 - toY)) - toX);
// the slope of the normal line to the tangent line to the curve
double fpt = ( (supperBigNumber-toY)/((supperBigNumber/find_perpend_tan(toX))-toX));
// the slope of the tangent line
double fd = ( (supperBigNumber-toY)/((supperBigNumber/find_derivative(toX))-toX));
// find the x where they intersect
double testx = (fpt*(toX-4) - fd*(395) + 395 - toY)/(fpt - fd);
// find the y where they intersect
double testy = fd*(testx - 395) + 395;
double lengthd = 250;
// calculating the length 400,400 to the normal line
double length = Math.sqrt( Math.pow( (395 - testx), 2) + Math.pow( (395 - testy), 2));
// calculate the x and y of the predicted path of the ball knowing the slope, length between points and the starting coords
// since it gives two x's and y's
if (dapathx >= 400) {
// perpendicular to the curve
coorx[0] = toX - lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
coory[0] = toY - fpt*lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
//tangent line
coorx[1] = toX + lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coory[1] = toY + fd*lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coorx[2] = toX - lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coory[2] = toY - fd*lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
// perpendicular to the path of the ball
coorx[3] = toX - lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coory[3] = toY - hps*lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coorx[4] = toX + lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coory[4] = toY + hps*lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
//reflection path
coordsx = testx + length*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coordsy = testy + fd*length*Math.sqrt(1/(1+ Math.pow(fd, 2)));
}
else {
coorx[0] = toX + lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
coory[0] = toY + fpt*lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
if ((400 - dapathx <= 6.5) && toX >= 400) {
coorx[0] = toX- lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
coory[0] = toY - fpt*lengthd*Math.sqrt(1/(1+ Math.pow(fpt, 2)));
}
coorx[1] = toX - lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coory[1] = toY - fd*lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coorx[2] = toX + lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coory[2] = toY + fd*lengthd*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coorx[3] = toX + lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coory[3] = toY + hps*lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coorx[4] = toX - lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coory[4] = toY - hps*lengthd*Math.sqrt(1/(1+ Math.pow(hps, 2)));
coordsx = testx - length*Math.sqrt(1/(1+ Math.pow(fd, 2)));
coordsy = testy - fd*length*Math.sqrt(1/(1+ Math.pow(fd, 2)));
}
}
Change ball and cue moving logic (DrawTheLuigi.java)
// calculate the slope of the ball and the cue --> find the path of the ball and the cue
private void ballAndStickLogic() {
//if the ball moves
if(ball_go) {
// if the cue touch the ball
if (stick_cy <= 400) {
// ball keeps going until touches the curve
if ((ball_y) <=toY) {
ball_go= false;
ball_kgo = true;
}else {
ball_y -= theSub; theSub+=1;
// equation to find x according to y; shift right 395 and up 395
ball_x = (int) (((ball_y-395)*(400-toX))/Math.abs(400-toY)) + 395;
}
} else {stick_cy -= the_sub; the_sub +=2;}
} else if (ball_kgo) {
// if ball passes 400, stops.
if (ball_y > 400) {
ball_kgo= false;
}
// ball bounce back
ball_y+=theSub; theSub -=1;
//calculate the path using slope-line equation
ball_x = (int) (((ball_y - toY)/((coordsy-toY)/(coordsx - toX))) + toX-7);
}
}
Change paint() (DrawTheLuigi.java)
public void paint(Graphics g) {
getTopCurve();
drawInformation(g);
drawCurve(g);
drawCoords(g);
drawBall(g);
drawNormalLineAndPathBall(g);
drawPathBounceBack(g);
drawStick(g);
drawTangentLine(g);
initComponents(g);
ballAndStickLogic();
getPathSlope();
writedalog();
}
Change how the cue is generated (DrawTheLuigi.java)
//draw the cue using bunch of lines instead of rect because rect cant rotate
private void drawStick(Graphics g) {
// using the same slope as the path of the ball to the curve
stick_sx = (int) (((stick_cy-395)*(400-toX))/Math.abs(400-toY)) + 395;
g.setColor(Color.ORANGE);
for (int i=0; i< 5; i++)
g.drawLine(stick_sx +i, stick_cy+i, getCueX() + 395+i, stick_cy + 200+i);
}
Change how the curve is generated (DrawTheLuigi.java)
// draw the curve using lines
private void drawCurve(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
double i =top;
double j =bottom;
g.setColor(Color.MAGENTA);
// generate the curve by generating bunch of line; each line is i and j distance from each other
while (i <bottom && j>top) {
g2.draw( new Line2D.Double( ((-1)*(halfw*(i-400)/h) + 200+(-1)*(w/2-200)) ,(i+=DrawAsk.dis) , ((halfw*(j-400)/h)+400+(400-(200+(-1)*(w/2-200)))) , (j-=DrawAsk.dis) ));
count ++;
}
//count number of lines
g.setColor(Color.BLUE);
g.drawString("Number of Lines: "+count, 10, 45);
count = 0;
}
Example of Remapping the on-screen buttons (FunctionsLol.java)
When you click g, instead of starting the ball's animation, the program will turn on debug mode.
from
protected void clickg() {
System.out.println("Clicked g");
wl.writeLog("Clicked g");
ball_go = true;
DrawTheLuigi.timer.start();
}
to
protected void clickg(){
toggle_perl = !toggle_perl;
System.out.println("Clicked d makes debug mode = " + toggle_perl);
wl.writeLog("Clicked d makes debug mode = "+ toggle_perl);
}
Open Customizier.jar, change the second argument to: jar uf PoolSimulator.jar FunctionsLol.class
Next steps are essentially knowing java commands: Javac and jar uf, leave as it if you don't understand.