0

I'm trying to animate a shot for my game in JavaFX(Scorched earth). I've been told, since I don't work with a gameloop, the best way would be to make a custom transition with an interpolator but after watching the javadocs I can not seem to figure it out. This is the bullet that gets shot

public void shootBullet(int angle, int v0, double wind) throws ArtilleryException {
        try {
            double speed = v0;
            Tank playerTank = GamePresenter.getPlayers()[0].getTank();

            xValues.clear();
            yValues.clear();
            double cornerRadians = Math.toRadians(angle);
            double endX = GameView.CANVAS_WIDTH;

            for (int x = 0; x < endX - playerTank.getPosX(); x++) {
                int y = (int) (-G / 2 / pow((speed * cos(cornerRadians) - wind), 2) * pow((x), 2) + (x) * sin(cornerRadians) / (cos(cornerRadians) - wind / speed) + playerTank.getPosY());

                    if (x + playerTank.getPosX() + Tank.getWIDTH() < GameView.CANVAS_WIDTH) {
                        xValues.add(x + playerTank.getPosX() + Tank.getWIDTH());//
                        yValues.add(-y + (playerTank.getPosY() + playerTank.getPosY()) - Tank.getHEIGHT() / 2);
                    }
            }


        } catch (NumberFormatException e) {
            throw new ArtilleryException("Couldn't shoot");
        }
    }

What I'm basically trying to do is to show an image trough an animation over the xValues list and yValues list. Could someone give me some basic guidelines on how to fill in the public void interpolate(double frac) method?

  • Can you give some actual information about what you want this transition to do? Maybe post the `Shot` class (or at least the helpful parts of it). What are the two lists you refer to? What do you actually want to do with them? – James_D Mar 17 '16 at 17:10
  • Since the shot class is very big, I just posted the most important method. the getShot() method in the second block of code is going to return the xValues list and the yValues list. I want the bullet Image to move over these lists of coordinates since they both have the same length. – Niels Van den Broeck Mar 17 '16 at 17:37

2 Answers2

1

Why don't you just use a PathTransition instead of building your own. From your x,y-coordinates you can easily create a Path object and then you can use a PathTransition to animate an ImageView along this path.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • so I just add moveTo objects for each element in the list? – Niels Van den Broeck Mar 17 '16 at 18:07
  • See this example for [creating a path based upon a function](http://stackoverflow.com/questions/24005247/draw-cartesian-plane-graphi-with-canvas-in-javafx). I'm not sure, and you won't be able to use it exactly as is, but perhaps such an approach might be useful for you. – jewelsea Mar 17 '16 at 18:08
  • @Niels No, of course not. You have to use a moveTo only for the first element and then you add for example lineTo objects. Just moving arround does not define a path. – mipa Mar 17 '16 at 18:24
  • Thank you all very much for your help. I'm going to work it out now and post the endresult when it works – Niels Van den Broeck Mar 17 '16 at 18:30
0

SOLUTION: I decided to use a PathTransition instead

private PathTransition makePathTrans(List<Integer> xCor, List<Integer> yCor) {
        view.getPath().getElements().clear();
        view.getPath().getElements().add(new MoveTo(xCor.get(0), yCor.get(0)));
        for (int i = 0; i < xCor.size(); i++) {
        view.getPath().getElements().add(new LineTo(xCor.get(i), yCor.get(i)));
    }
        PathTransition pthTrans = new PathTransition(Duration.seconds(2), view.getPath(), view.getBullet());
        pthTrans.setCycleCount(1);
        pthTrans.setInterpolator(Interpolator.LINEAR);
        pthTrans.setAutoReverse(false);
        return pthTrans;
}

//in my presenter
    view.getBullet().setVisible(true);
    PathTransition shot = makePathTrans(xCor, yCor);
    shot.play();