矢量缩放问题

时间:2012-08-21 03:48:43

标签: java math vector minecraft

您好我正在开发一个基于Minecraft源代码的Minecraft机器人,我需要编写一个fly to block方法......但是我的方法似乎是故障,我无法弄清楚原因。我错过了什么吗? (抱歉模糊命名,Local表示它是函数本地的值,本地向量实际存储在数组中)

private void flyTo(int x, int y, int z,double posX,double posY,double posZ) {
    double xLocal = posX;
    double yLocal = posY;
    double zLocal = posZ;
    while(xLocal!=x &&yLocal!=y&&zLocal!=z){
        double[] v = new double[3];
        v[0] = x-xLocal;
        v[1] = y-yLocal;
        v[2] = z-zLocal;
        double magxz = Math.sqrt(Math.pow(v[0], 2)+Math.pow(v[2], 2));
        if(magxz>0.6D){
            double scaler = 0.6D/magxz;
            v[0] *= scaler;
            v[2] *= scaler;
        }
        if(Math.abs(v[1])>1){
            if(v[1]>0){
                v[1]=1;
            }else{
                v[1]=-1;
            }
        }
        xLocal+=v[0];
        yLocal+=v[1];
        zLocal+=v[2];
        mc.getSendQueue().addToSendQueue(new Packet11PlayerPosition((double)xLocal,(double)yLocal,yLocal+0.83,(double)zLocal,false));
        mc.getSendQueue().processReadPackets();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果没有更多信息,我唯一可以建议的是标准化v,因为它似乎是一个方向向量:

v[0] = x-xLocal;
v[1] = y-yLocal;
v[2] = z-zLocal;

double mag = Math.sqrt(Math.pow(v[0], 2) + Math.pow(v[1], 2) + Math.pow(v[2], 2));

v[0] /= mag;
v[1] /= mag;
v[2] /= mag;