Java libgdx3d中的光线投射

Java libgdx3d中的光线投射,java,libgdx,collision-detection,raycasting,Java,Libgdx,Collision Detection,Raycasting,好吧,我已经尝试了很长一段时间了,在libgdx中使用Raycast,根据我看的地方进行三维碰撞检测。。但是我画了一个空白,似乎没有任何文档。有人能帮我找到正确的方向吗?事实上,使用libgdx很容易实现您想要做的事情。下面是我用来进行光线测试并找到光线将击中的最近碰撞对象的内容。假设它位于一个名为BulletUtil的类中 private static final Vector3 rayFrom = new Vector3(); private static final Vector3 ray

好吧,我已经尝试了很长一段时间了,在libgdx中使用Raycast,根据我看的地方进行三维碰撞检测。。但是我画了一个空白,似乎没有任何文档。有人能帮我找到正确的方向吗?

事实上,使用libgdx很容易实现您想要做的事情。下面是我用来进行光线测试并找到光线将击中的最近碰撞对象的内容。假设它位于一个名为
BulletUtil
的类中

private static final Vector3 rayFrom = new Vector3();
private static final Vector3 rayTo = new Vector3();
private static final ClosestRayResultCallback callback = new ClosestRayResultCallback(rayFrom, rayTo);

public static btCollisionObject rayTest(btCollisionWorld collisionWorld, Ray ray) {
    rayFrom.set(ray.origin);
    // 50 meters max from the origin
    rayTo.set(ray.direction).scl(50f).add(rayFrom);

    // we reuse the ClosestRayResultCallback, thus we need to reset its
    // values
    callback.setCollisionObject(null);
    callback.setClosestHitFraction(1f);
    callback.getRayFromWorld().setValue(rayFrom.x, rayFrom.y, rayFrom.z);
    callback.getRayToWorld().setValue(rayTo.x, rayTo.y, rayTo.z);

    collisionWorld.rayTest(rayFrom, rayTo, callback);

    if (callback.hasHit()) {
        return callback.getCollisionObject();
    }

    return null;
}
现在,您还需要一个对单击事件作出反应的
输入处理器

public class BulletInputProcessor extends InputAdapter {

    private Viewport pickingViewport;
    private btCollisionWorld collisionWorld;

    // a constructor which takes a Viewport and the collision world and stores them
    public BulletInputProcessor(...) { ... }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (button == Input.Buttons.LEFT) {
            Ray pickRay = pickingViewport.getPickRay(screenX, screenY);

            btCollisionObject body = BulletUtil.rayTest(collisionWorld, pickRay);
            if (body != null) {
                // do whatever you want with this body now
                return true;
            }
        }

        return false;
    }
}

视口
负责创建拾取光线。您应该在此处使用视口,该视口管理用于渲染世界的
PerspectiveCamera
。别忘了通过
Gdx.input.setInputProcessor(新BulletInputProcessor(collisionWorld,viewport))
注册输入处理器。在您的代码中,有两行代码对我不起作用。可能会被弃用吗

callback.getRayFromWorld().setValue(rayFrom.x, rayFrom.y, rayFrom.z);
callback.getRayToWorld().setValue(rayTo.x, rayTo.y, rayTo.z);
我把它们改成了

  Vector3 rayFromWorld = new Vector3();
  rayFromWorld.set(rayFrom.x, rayFrom.y, rayFrom.z);
  Vector3 rayToWorld   = new Vector3();
  rayToWorld.set(rayTo.x, rayTo.y, rayTo.z);
这使得整个功能工作起来很有魅力

我发现的另一件事是,你必须激活身体(这个函数返回)才能对它施加力。大多数僵硬的身体会在一段时间后进入睡眠状态,而ClosestRayResultCallback不会唤醒他们

btCollisionObject target = CollisionUtils.rayTest(dynamicsWorld, ray);
//test for collisionflags and stuff here
//and if your target is instance of btRigidBody
btRigidBody body = (btRigidBody)target;
body.activate();
body.applyCentralForce(ray.direction.scl(50000));

你使用子弹物理吗?是的,对不起,我完全忘了提那件事,好吧,我想我明白你的意思,但是我要睡觉了,所以明天才能测试。顺便说一句,谢谢你的快速回复!好吧,看来我已经在0.0的某个地方严重失败了,无论我做什么,它都没有注册它与任何其他碰撞对象碰撞。当前存在此问题(几小时前刚刚发布[)。如何找到ModelInstance指向的“光线”?