我正在使用Decal类(请参阅https://code.google.com/p/libgdx-users/wiki/Decals)来渲染我的场景,我需要测试用户是否点击了其中一个贴花,类似于舞台中演员的实现方式。
是否有针对此类命中测试的实现?如果没有,libgdx中是否有任何低级API可以帮助我实现它?
答案 0 :(得分:4)
我有一个很好的方法,使用onTouch()可以轻松更改鼠标事件,
Decal decal1 = Decal.newDecal(1, 1, textures[1], true);
decal1.setPosition(1.5f, 0, .5f);
decals.add(decal1);
Decal decal2 = Decal.newDecal(1, 1, textures[0], true);
decal2.setPosition(1f, 0, .5f);
decals.add(decal2);
decal2.translate(0, 0, 1f);
//...
for (int i = 0; i < positions.length; i++) {
positions[i] = new Vector3(decal1.getPosition());
new Vector3(decal2.getPosition());
}
positions[0].set(decal1.getPosition());
positions[1].set(decal2.getPosition());
renderer = new ImmediateModeRenderer10();
Vector3 intersection = new Vector3();
//render method
Ray pickRay = null;
renderTowers();
camera3d.update();
if (Gdx.input.isTouched()) {
pickRay = camera3d.getPickRay(Gdx.input.getX(), Gdx.input.getY(),
TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
}
boolean intersected1 = false;
boolean intersected2 = false;
for (int i = 0; i < positions.length; i++) {
if (pickRay != null
&& Intersector.intersectRaySphere(pickRay, positions[0],
.5f, intersection)) {
gl.glColor4f(1, 0, 0, 1);
intersected1 = true;
} else {
gl.glColor4f(1, 1, 1, 1);
}
gl.glPushMatrix();
gl.glTranslatef(positions[i].x, positions[i].y, positions[i].z);
gl.glPopMatrix();
}
for (int j = 0; j < positions.length; j++) {
if (pickRay != null
&& Intersector.intersectRaySphere(pickRay, positions[1],
.5f, intersection)) {
gl.glColor4f(1, 0, 0, 1);
intersected2 = true;
} else {
gl.glColor4f(1, 1, 1, 1);
}
gl.glPushMatrix();
gl.glTranslatef(positions[j].x, positions[j].y, positions[j].z);
gl.glPopMatrix();
}
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
sbatch.begin();
if (intersected1) {
stage.addActor(FireImage);
camera3d.project(intersection, TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
}
if (intersected2) {
stage.addActor(IceImage);
camera3d.project(intersection, TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
}
不是很干净,但现在可以使用,其中部分内容来自于LIBGDX射线拣选的一些优秀在线教程,所有这些归功于他们!我只是为贴花定制了一下。希望它有所帮助,如果你想出一个更清洁的方法,我相信有一个请发帖,我很乐意看到它。