如何在Libgdx Java中拖动3D对象?

时间:2019-05-05 14:55:50

标签: java android 3d libgdx

我试图通过重写touchDragged函数来调用3D立方体。我已经找到了如何在XZ平面上拖动对象,但是我不知道如何在3D模式下拖动对​​象。

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    if (selecting < 0)
        return false;
    if (selected == selecting) {
        Ray ray = cam.getPickRay(screenX, screenY);
        final float distanceY = -ray.origin.y / ray.direction.y;
        final float distanceX = -ray.origin.x / ray.direction.x;
        final float distanceZ = -ray.origin.z / ray.direction.z;
        final float distance;
        if (oldRay!=null){
            float absDifferenceX = Math.abs(ray.direction.x-oldRay.direction.x);
            float absDifferenceY = Math.abs(ray.direction.y-oldRay.direction.y);
            float absDifferenceZ = Math.abs(ray.direction.z-oldRay.direction.z);
            float minAbsDifference =  Math.min(Math.min(absDifferenceX,absDifferenceY),absDifferenceZ);
            if (minAbsDifference == absDifferenceX ){
                distance = distanceX;
            }else if (minAbsDifference == absDifferenceY){
                distance = distanceY;
            }else{
                distance = distanceZ;
            }
        }else{
            distance = (distanceX+distanceY+distanceZ)/3.0f;
        }

        position.set(ray.direction).scl(distance).add(ray.origin);
        objects.get(selected).transform.setTranslation(position);
        oldRay = ray;
    }

    return true;
}

0 个答案:

没有答案