I have a problem with my code. There are a Methods to shoot a bullet and update UI in game. The problem is that: My code doesn't work as I had imagined infact the bullet make a traiectory and Parabolic path but not always the calculated function is fine for this program. I think that obviously the problem is in the mathematical formula but i dont understand how to debug this. please help me!!! thanks and sorry but in code some variables are in Italian please have patience with me i'm a student.
public class NuovoGiocoController {
Cannone cannone;
Aereo aereo;
double vel = 100;
Proiettile pro;
int CAR = 10;
int raf = 2;
@FXML
private Rectangle canna;
@FXML
private Rectangle iconaAereo;
@FXML
private Circle P;
@FXML
private AnchorPane MP;
void Aggiorna() throws InterruptedException { //Aggiorna = Update
//vel = Slider.getValue();
double angle = canna.getRotate();
boolean morto = false;
double Gravity = 10;
while (morto == false) {
double X = P.getLayoutX();
double Y = P.getLayoutY();
if (X > 1 && Y > 1 && X < MP.getWidth() && Y < MP.getHeight()) {
System.out.println("x: " + X + " y: " + Y + " maxX: " + MP.getWidth() + " maxY: " + MP.getHeight());
angle = Math.abs(angle);
double x = P.getLayoutX();
double y = P.getLayoutY();
P.setLayoutX(x += (Math.tan(angle)-(Gravity/(2*vel*vel)*(Math.cos(angle))) ));
P.setLayoutY(y -= (Math.tan(angle)+(Gravity/(2*vel*vel)*(Math.cos(angle)))*(x*x) ));
System.out.println("VIVO");
TimeUnit.MILLISECONDS.sleep(100);
} else {
System.out.println("MORTO");
P.setLayoutX(pro.posX);
P.setLayoutY(pro.posY);
morto = true;
}
}
}
void nuovoProcesso() { // nuovoProcesso = newProcess
Task task = new Task<Void>() {
@Override
public Void call() throws InterruptedException {
for (int i = 1; i <= raf; i++) {
if (isCancelled()) {
break;
}
Aggiorna();
}
return null;
}
};
new Thread(task).start();
}
@FXML
void Spara(ActionEvent event) throws InterruptedException {//Spara=shoot
if (raf <= CAR) {
this.CAR -= raf;
//car.setText(String.valueOf(CAR));
nuovoProcesso();
}
if (CAR <= 0) {
//metodo che fà uscire e andare allo score
System.exit(0);
}
}
the Formula to move the bullet is:this
the angle depending on the rotation of a cannon at the center of the screen
public void spostaCannaDestra(ActionEvent event) {
if (canna.getRotate() < + 60) {
canna.setRotate(canna.getRotate() + 10);
}
}
public void spostaCannaSinistra(ActionEvent event) {
if (canna.getRotate() > - 60) {
canna.setRotate(canna.getRotate() - 10);
}
}
@FXML
public void initialize() {
cannone = new Cannone(0);
canna.setLayoutX(cannone.getPosX());
canna.setLayoutY(cannone.getPosY());
canna.setWidth(cannone.getX());
canna.setHeight(cannone.getY());
pro = new Proiettile(16, 16, 300, 340, vel);
P.setLayoutX(pro.posX);
P.setLayoutY(pro.posY);
}
The Speed IS initialized like double vel = 100;(if this is what you mean)