Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

java - RayCasting in Libgdx 3d

Alright so I've been trying for quite some time too Raycast in libgdx for 3d collision detection based on where im looking.. But I've drawn a blank and doesn't seem to be any documentation anywhere. Would someone be kind enough to send me in the right direction?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is actually really easy with libgdx to achieve what you are trying to do. The following is what I'm using to do a ray test and find the closest collision object that my ray would hit. Let's assume it is located in a class called 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;
}

Now what else you need is an InputProcessor that reacts on the click events.

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;
    }
}

The Viewport is responsible to create the picking Ray. You should use the viewport here which manages the PerspectiveCamera that you use to render the world. Don't forget to register the input processor via Gdx.input.setInputProcessor(new BulletInputProcessor(collisionWorld, viewport)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...