如何在libgdx中检测3D模型上的点击坐标?

时间:2015-07-06 14:35:44

标签: java 3d libgdx

我正在创建简单的人体模型: Human body

用户可以旋转身体。我正在使用 Camera 类中的 rotateAround 方法。因此,身体可以以不同的角度旋转。例如: Human body rotated 1 Human body rotated 2 Human body rotated 3

我想知道检测哪个身体部位被点击的最佳方法是什么。如何检查用户是否点击了胸部,头部,腿部等? 在2D中似乎很容易,但不幸的是,我不知道如何在3D模型的情况下编程这样的检测。

另外,我包括我的代码:

private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Model model;
private ModelInstance modelInstance;
private Environment environment;
private int dragX, dragY;
private float lightIntensity = 100f;
private PointLight pointLight;

@Override
public void create () {
    camera = new PerspectiveCamera(75, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(0f, 17.708357f, 13.8000145f);
    camera.lookAt(0f, 7f, 0f);
    camera.near = 0.1f;
    camera.far = 300.0f;

    modelBatch = new ModelBatch();

    UBJsonReader jsonReader = new UBJsonReader();
    G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);

    model = modelLoader.loadModel(Gdx.files.getFileHandle("model.g3db", FileType.Internal));
    modelInstance = new ModelInstance(model);
    modelInstance.transform.rotate(0, 0, 0, 0);
    modelInstance.transform.translate(0, 0, 0);


    environment = new Environment();
    pointLight = new PointLight().set(Color.WHITE, camera.position, lightIntensity);
    environment.add(pointLight);

    Gdx.input.setInputProcessor(this);

}

@Override
public void render() {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    camera.update();

    modelBatch.begin(camera);
    modelBatch.render(modelInstance, environment);
    modelBatch.end();
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    float dX = (float)(screenX-dragX)/(float)Gdx.graphics.getWidth();
    float dY = (float)(dragY-screenY)/(float)Gdx.graphics.getHeight();
    dragX = screenX;
    dragY = screenY;
    Vector3 point = new Vector3(0f, 7f, 0f);
    Vector3 axis = new Vector3(0f, -7f, 0f);
    camera.rotateAround(point, axis, dX * 200f);
    environment.remove(pointLight);
    pointLight = new PointLight().set(Color.WHITE, camera.position, lightIntensity);
    environment.add(pointLight);
    camera.update();
    return true;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    dragX = screenX;
    dragY = screenY;
    return true;
}

0 个答案:

没有答案