LibGDX:检测Ray击中3D对象的位置

时间:2015-08-04 09:44:10

标签: libgdx

我正在LibGDX中构建一个3D游戏,如果我触摸一个对象,我已经使用了下面的代码,但是如何找出它碰撞的位置。我没有使用Bullet,因为我想制作一个HTML5端口

    public int getObject (int screenX, int screenY) {

        int result = -1;
        float distance = -1;

        Ray ray = camera.getPickRay(screenX, screenY);
        Vector3 pos = new Vector3(camera.position);

        for (int i = 0; i < boxInstance.size; i++) {

            GameObject instance = boxInstance.get(i);
            instance.transform.getTranslation(pos);

            float dist2 = ray.origin.dst2(pos);
            if (distance >= 0f && dist2 > distance) continue;

            if (Intersector.intersectRayBoundsFast(ray, pos, instance.dimensions)) {
                result = i;
                distance = dist2;

                Vector3 v = new Vector3();
                if (Intersector.intersectRayBounds(ray, instance.bounds, v))
                {
                    boxInstance.get(result).materials.get(0).set(ColorAttribute.createDiffuse(Color.RED));
                }
            }
        }

        if (result > -1)
        {

            //boxInstance.removeIndex(result);
        }

        return 1;
    };

我需要这个的原因是,如果我触摸一个大平面,我希望能够触摸我触摸的物体。

更新 我发现intersectRayBounds应该做我想要的但它永远不会触发

这是我的GameObject类。也许我的BoundingBox错了?

public class GameObject extends ModelInstance {
    public final Vector3 center = new Vector3();
    public final Vector3 dimensions = new Vector3();
    public static BoundingBox bounds = new BoundingBox();

    public GameObject (Model model, float x, float y, float z) {
        super(model, x,y,z);
        calculateBoundingBox(bounds);
        bounds.getCenter(center);
        bounds.getDimensions(dimensions);
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定为什么这是修复,但似乎你必须每次都计算边界框。请参阅下面的代码。

    public int getObject (int screenX, int screenY) {

        int result = -1;
        float distance = -1;

        Ray ray = camera.getPickRay(screenX, screenY);
        Vector3 pos = new Vector3(camera.position);

        for (int i = 0; i < boxInstance.size; i++) {

            GameObject instance = boxInstance.get(i);
            instance.transform.getTranslation(pos);
            instance.updateBox();

            float dist2 = ray.origin.dst2(pos);
            if (distance >= 0f && dist2 > distance) continue;


            Vector3 v = new Vector3();
            if (Intersector.intersectRayBounds(ray, instance.bounds, v))
            {
                result = i;
                distance = dist2;
                Gdx.app.log("MyTag 2", "x " + v.x + " z " + v.z);

            }

        }

        if (result > -1)
        {
            boxInstance.get(result).materials.get(0).set(ColorAttribute.createDiffuse(Color.RED));
        }

        return 1;
    };


public class GameObject extends ModelInstance {
    public final Vector3 center = new Vector3();
    public final Vector3 dimensions = new Vector3();
    public static BoundingBox bounds = new BoundingBox();
    private float x,y,z;


    public GameObject (Model model, float x, float y, float z) {
        super(model, x, y, z);
        this.x = x;
        this.y = y;
        this.z = z;
        updateBox();
    }

    public void updateBox()
    {
        calculateBoundingBox(bounds);
        bounds.getCenter(center);
        bounds.getDimensions(dimensions);
        bounds.set(bounds.min.add(x,y,z), bounds.max.add(x,y,z));

        Gdx.app.log("MyTag 2", "x " + bounds.min + " z " + bounds.max );
    }

}