我正在尝试检测盒形碰撞对象的鼠标选择,但不知何故,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);
}
设置(我认为)并没有什么异常 - 例外的是,我使用的是正交相机。
光线测试代码没有什么特别之处:
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)之间时,才会检测到框中的鼠标单击。在框中的其他部分点击鼠标仍未检测到。
我在这里错过了什么吗?
答案 0 :(得分:1)
显然,rayTest有一个bug(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)都适合我。