1

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)

Community
  • 1
  • 1
  • What formula do you intend to use? Is this an initial velocity, only under the influence of gravity? What is angle? – matt May 12 '16 at 11:35
  • i improve the question to answer at you comment! thanks for your interest and your help – Nicola Corea May 12 '16 at 13:20
  • It looks like your angle could change during the animation, which means your formula will be incorrect. Then, if I understand what your equation is from, x should be updated by a constant value everytime, which might be correct with what you have. Then you should just calculate Y based on X, you should not use +=. – matt May 12 '16 at 13:59
  • I'm trying some tests, but without success. I hope it's my code to be wrong and not the formula, because this was given me by the professor with whom I will have to sit the java exam, and it would be very embarrassing telling him that he was wrong – Nicola Corea May 12 '16 at 14:27
  • I provided an answer, and updated the answer to include your equation. You have quite a few mistakes in your y equation. – matt May 12 '16 at 14:38

1 Answers1

1

The formulation you have is not a good formulation, what if the bullet is shot straight up in the air? Then the equations break.

First calculate the initial velocity in the x and y.

double vx = Math.cos(angle)*vel;
double vy = Math.sin(angle)*vel;
double x0 = P.getLayoutX();
double y0 = P.getLayoutY();

Now we can use use some sort of time value and update the two variables.

double t = 0;
while(morto == false){
    double x = x0 + vx*t;
    double y = y0 + vy*t - 0.5*Gravity*t*t;
    P.setLayoutX(x);
    P.setLayoutY(y);
    t++;
}

If you want to stick to your formulation, there are a couple errors.

P.setLayoutX(x += (Math.tan(angle)-(Gravity/(2*vel*vel)*(Math.cos(angle))) ));

This cannot be correct, and you haven't provided an equation for x. If I had to guess I would say:

x += vel*Math.sin(angle);

Next you have an equation for y it does not increment.

        P.setLayoutY( 
            Math.tan(angle)*x-Gravity/(2*vel*vel*Math.cos(angle)*Math.cos(angle))*x*x );
matt
  • 10,892
  • 3
  • 22
  • 34
  • sorry for the delay but i had to work. I tried to include your correction but maybe i mistake something couse now the bullet doesn't update in the UI except if i rotate the cannon to right or left at 50° but anyway don't make the correct traiectory. i tried to insert, edit, change, cry on my and your code but nothing... ps:thank you soo much for the patience – Nicola Corea May 13 '16 at 08:08
  • @NicolaCorea do you have a formal for `x`? You gave the formula for `y` only. – matt May 13 '16 at 08:15
  • @NicolaCorea what are you currently trying to use? – matt May 13 '16 at 10:17
  • sorry for delay! i'm really sorry but i study your code better and WOOOOOOORK!!!!!!!!! thank you soo much!!! – Nicola Corea May 17 '16 at 17:37