我正在尝试检测盒形碰撞对象的鼠标尖刺,但是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)之间时,才检测到该框中的鼠标单击。框的另一部分中的鼠标单击保持未被检测到。
我在这里想念什么吗?
最佳答案
显然,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)都对我有效。