Raycasting没有报告发生碰撞

时间:2014-07-30 17:39:28

标签: java libgdx bulletphysics

好的,所以我试图让我的光线投射碰撞的对象回来,但我总是报告回null。这是代码。

    public static PerspectiveCamera cam;
static btCollisionWorld collisionWorld;
DebugDrawer debugDrawer;
public ModelBatch modelBatch;
btCollisionConfiguration collisionConfig;
static btDispatcher dispatcher;
btBroadphaseInterface broadphase;
public static btCollisionShape voxelShape;
public static Viewport viewport;
public static Model model;
public static ModelInstance test;
public static btCollisionShape collisiontest;
public static btCollisionObject collisiontestobject;


@Override
public void create () {
    Bullet.init();
    collisionConfig = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfig);
    broadphase = new btDbvtBroadphase();
    collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfig);

    cam = new PerspectiveCamera(67, 1280, 720);
    cam.position.set(10f,10f,10f);
    cam.lookAt(0,0,0);
    cam.near = 1f;
    cam.far = 300f;
    cam.update();

    modelBatch = new ModelBatch();


    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(3f, 3f, 3f,
            new Material(ColorAttribute.createDiffuse(Color.BLUE)),
            Usage.Position | Usage.Normal);

    test = new ModelInstance(model);

    test.transform.setTranslation(0,0,0);

    collisiontest = new btBoxShape(new Vector3 (1.5f,1.5f,1.5f));

    collisiontestobject = new btCollisionObject();
    collisiontestobject.setCollisionShape(collisiontest);
    collisiontestobject.setWorldTransform(new Matrix4());

    debugDrawer = new DebugDrawer();
    debugDrawer.setDebugMode(btIDebugDraw.DebugDrawModes.DBG_MAX_DEBUG_DRAW_MODE);

    collisionWorld.setDebugDrawer(debugDrawer);

    collisionWorld.addCollisionObject(collisiontestobject);

    viewport = new FitViewport(1280, 720, cam);

    Gdx.input.setInputProcessor(new BulletInputProcessor(viewport, collisionWorld));


}

@Override
public void render () {

    cam.update();


    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);


    debugDrawer.begin(cam);
    collisionWorld.debugDrawWorld();
    debugDrawer.end();

    modelBatch.begin(cam);
    modelBatch.render(test);
    modelBatch.end();


}

下一部分

public class RayTesting {

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(Viewport viewport, btCollisionWorld collisionWorld){

    this.pickingViewport = viewport;

    this.collisionWorld = collisionWorld;



}

@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 = RayTesting.rayTest(collisionWorld, pickRay);
        if (body != null) {
            System.out.println("Test");
            return true;
        }
    }

    return false;
}

}

我完全失去了我理解大多数Libgdx的东西但是子弹让我感到困惑..顺便说一句,谢谢@noone所有的帮助!真的很感激:D

1 个答案:

答案 0 :(得分:0)

您的视口不会像它应该的那样更新。它的视口大小为0。这会产生RaypickingViewport.getPickRay(...)),其中只包含NaN(非数字)值。 Bullet似乎能够毫无例外地处理这个问题,但它当然无法正常工作。将以下内容添加到您的ApplicationAdapterScreen或您正在使用的任何内容中。

@Override
public void resize(int width, int height) {
    viewport.update(width, height);
}