我正在尝试检测鼠标选中的盒形碰撞对象,但 Bullet 的 rayTest 以某种方式显示出奇怪的行为。
这是我的世界设置:
public void create () {
Bullet.init();
Gdx.app.log("Bullet", "Version = " + LinearMath.btGetVersion());
// camera:
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 10f);
camera.up.set(0f, 1f, 0f);
camera.lookAt(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0);
camera.near = 1f;
camera.far = +100f;
camera.update();
closestRayResultCallback = new ClosestRayResultCallback(Vector3.Zero, Vector3.Z);
btDefaultCollisionConfiguration collisionConfig = new btDefaultCollisionConfiguration();
btCollisionDispatcher dispatcher = new btCollisionDispatcher(collisionConfig);
btDbvtBroadphase broadphase = new btDbvtBroadphase();
collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfig);
btCollisionShape shape = new btBoxShape(new Vector3(0.50f, 0.50f, 0.50f));
btCollisionObject obj = new btCollisionObject();
obj.setCollisionShape(shape);
collisionWorld.addCollisionObject(obj);
Matrix4 transform = new Matrix4();
transform.setTranslation(0.90f, 0.90f, 0f);
obj.setWorldTransform(transform);
// Debug:
debugDrawer = new DebugDrawer();
collisionWorld.setDebugDrawer(debugDrawer);
debugDrawer.setDebugMode(btIDebugDraw.DebugDrawModes.DBG_MAX_DEBUG_DRAW_MODE);
}
设置(我认为)没有什么不寻常 - 也许异常(exception)是我使用了正交相机。
光线测试代码也没有什么特别之处:
private btCollisionObject rayTest(int x, int y) {
Ray ray = camera.getPickRay(x, y);
rayFrom.set(ray.origin);
rayTo.set(ray.direction.scl(50)).add(ray.origin);
Gdx.app.log("Bullet", "rayTest - rayFrom: " + rayFrom + ", rayTo: " + rayTo);
// we reuse the ClosestRayResultCallback, thus we need to reset its values:
closestRayResultCallback.setCollisionObject(null);
closestRayResultCallback.setClosestHitFraction(1f);
closestRayResultCallback.setRayFromWorld(rayFrom);
closestRayResultCallback.setRayToWorld(rayTo);
collisionWorld.rayTest(rayFrom, rayTo, closestRayResultCallback);
if (closestRayResultCallback.hasHit()) {
Gdx.app.log("Bullet", "rayTest - has hit");
return closestRayResultCallback.getCollisionObject();
}
return null;
}
在我的渲染循环中,我 1) 监听鼠标点击并在点击时调用具有相应鼠标坐标的 rayTest 方法,以及 2) 使用调试抽屉渲染碰撞体的框形状。
现在,我的观察是:
框按预期绘制在应用程序窗口左下角附近。但是 hitTest 仅在框的左下角检测到鼠标点击框形状。 更准确地说,只有当鼠标位于框的左下角(此处:(41, 41))和 (49, 49) 之间时,才会检测到鼠标在框中的点击。框的其他部分的鼠标点击仍未被检测到。
我是不是漏掉了什么?
最佳答案
显然,rayTest 有一个错误 (Bullet v2.82)。
至少,http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=10187&p=34250&hilit=rayTest#p34250和 http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=10205&p=34323&hilit=rayTest#p34323报告相同的观察结果。这些帖子中提到的两种解决方法(使用 btCollisionWorld.updateAAbbs() 或使用 btDiscreteDynamicsWorld+btRigidBody+StepSimulation)对我都有效。
关于java - 在 LibGDX 中使用 Bullet 的奇怪 rayTest 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005674/