对象以常规速度通过Java中的Path2D曲线移动

时间:2017-06-11 19:56:46

标签: java

我正在学习编程,我目前正在进行OOP课程。

我必须克隆一个Kingdom Rush,我获得了this example的路径。

我的问题是如何让对象在路径上迭代以常规速度移动。

我看到this post提出同样问题但却不明白如何实施它。

在这个例子中,我得到了一个库来创建方向向量(处理/核心)。

这是代码:

import java.awt.*;
import java.awt.event.*;

import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;
import java.text.*;

import com.entropyinteractive.*;

public class DemoRuta extends Game {
    Fondo fondo;
    Shape carrilUno;
    java.util.List<Point2D> puntos;
    int radius=16;
    Point2D pos=new Point2D.Float(310 - (radius/2),0 - (radius/2) );
    double dbl_idx;

    public static void main(String[] args) {
        DemoRuta game = new DemoRuta();
        game.run(1.0 / 60.0);
        System.exit(0);
    }

    public DemoRuta() {
           super("DemoRuta ", 700, 600);
    }

    public void gameStartup() {
        Path2D path = new Path2D.Double();
        path.moveTo(310,0);
        path.curveTo(310,50,315,100,   320,140);
        path.curveTo(320,170,310,190,  285,220);
        path.curveTo(260,230, 230,250, 200,250);
        path.curveTo(170,275,155,320,  150,350);
        path.curveTo(165,380,190,420,  240,430);
        path.curveTo(280,430,330,430,  400,420);
        path.curveTo(440,440,470,430,  500,450);
        path.curveTo(520,420,550,400,  575,370);
        path.curveTo(620,360,660,360,  700,360);
        carrilUno = path;

        puntos = new ArrayList<>(50);
        PathIterator pi = carrilUno.getPathIterator(null, 0.01);
        while (!pi.isDone()) {
            double[] coords = new double[6];
            switch (pi.currentSegment(coords)) {
                case PathIterator.SEG_MOVETO:
                case PathIterator.SEG_LINETO:
                    puntos.add(new Point2D.Double(coords[0], coords[1]));
                    break;
            }
            pi.next();
        }
        System.out.println("Cantidad de puntos de la ruta: " + puntos.size());
        fondo = new Fondo();
    }

    public void gameUpdate(double delta) {
        dbl_idx += 0.0005;
        int index = Math.min(Math.max(0, (int) (puntos.size() * dbl_idx)), puntos.size() - 1);
        pos = puntos.get(index); // saco una posicion
        dbl_idx = (index >=puntos.size() - 1) ? 0.0 : dbl_idx;

        Keyboard keyboard = getKeyboard();
        LinkedList < KeyEvent > keyEvents = keyboard.getEvents();
        for (KeyEvent event: keyEvents) {
            if ((event.getID() == KeyEvent.KEY_PRESSED) &&
                (event.getKeyCode() == KeyEvent.VK_ESCAPE)) {
                stop();
            }
        }
    }

    public void gameDraw(Graphics2D g) {
        fondo.display(g);

        g.setColor(Color.white);
        g.drawString("Tecla ESC = Fin del Juego ",490,20);

        g.setColor(Color.red);
        g.draw(carrilUno);

        g.setColor(Color.black);
        g.fillOval((int)(pos.getX()- (radius/2)),  (int)(pos.getY()- (radius/2)), radius,radius);
    }

    public void gameShutdown() {

    }
}

class Fondo {
    private BufferedImage img_fondo = null;

    public Fondo() {
            try {
                img_fondo= ImageIO.read(getClass().getResource("nivel1.png"));
            } catch (IOException e) {
                System.out.println(e);
            }
    }

    public int getWidth(){
        return img_fondo.getWidth();
    }
    public int getHeight(){

        return img_fondo.getHeight();
    }

    public void display(Graphics2D g2) {
        g2.drawImage(img_fondo,0,0,null);//movimiento del fondo
     }    
}

2 个答案:

答案 0 :(得分:0)

记住高中物理,distance = speed * time。在更新循环中,您将获得自上次更新以来经过的时间量:

public void gameUpdate(double delta)

不要在每次更新时向dbl_idx添加一些非常小的数字,而是尝试将时间乘以某个速度,然后添加:

dbl_idx = SPEED * delta; // you can choose any value you like for speed

答案 1 :(得分:0)

今天老师给了我们两个新的库,解释了如何用4个点(不相关)制作Bezier曲线,但是在这个例子中,在制作List来存储构成曲线的点时,引入了这个:

while (!pi.isDone() && Point2D.distance(puntos[0],puntos[1],cursor[0],cursor[1]) < 10){
    pi.next();
    if (!pi.isDone())
        pi.currentSegment(puntos);
    }
}

这部分( Point2D.distance(puntos [0],puntos [1],光标[0],光标[1])&lt; 10 )使用Point2D的方法距离跳过点如果他们相对接近,在这个例子中是10px。