【发布时间】:2015-03-26 22:55:07
【问题描述】:
我正在尝试查看我的相机是否面向建筑物。
当沿该地点移动相机时,我使用xDelta 和zDelta
xDelta=sine/4 和 zDelta=cosine/4(限制移动速度)。
基本上,我的想法是获取建筑物的距离,并将缩放到建筑物距离的增量应用于相机的坐标,以获得一组投影坐标。然后我尝试获取建筑物坐标和投影坐标之间的距离,以查看相机是否面向它。
我可以确认到建筑物的距离是正确的,但是我认为投影坐标有问题。到目前为止,这是我的代码:
building1Distance = Math.pow((double)(CAMERA_X - BUILDING1_X), 2) + Math.pow((double)(CAMERA_Z - BUILDING1_Z), 2);
building1Distance = Math.sqrt(building1Distance);
building2Distance = Math.pow((double)(CAMERA_X - BUILDING2_X), 2) + Math.pow((double)(CAMERA_Z - BUILDING2_Z), 2);
building2Distance = Math.sqrt(building2Distance);
building3Distance = Math.pow((double)(CAMERA_X - BUILDING3_X), 2) + Math.pow((double)(CAMERA_Z - BUILDING3_Z), 2);
building3Distance = Math.sqrt(building3Distance);
building4Distance = Math.pow((double)(CAMERA_X - BUILDING4_X), 2) + Math.pow((double)(CAMERA_Z - BUILDING4_Z), 2);
building4Distance = Math.sqrt(building4Distance);
//sort them
distances = new double[4];
distances[0] = building1Distance;
distances[1] = building2Distance;
distances[2] = building3Distance;
distances[3] = building4Distance;
Arrays.sort(distances);
//search to see if pointing at building
for(int i = 0; i < distances.length; i++){
//projected coordinates of where camera is facing at that distance
pX = CAMERA_X + (xDelta * 4.0 * distances[i]);
pZ = CAMERA_Z + (zDelta * 4.0 * distances[i]);
//check to see which building is at that distance
if(building1Distance == distances[i]){
//check to see if its within diameter of the building.
if(Math.sqrt(Math.pow((double)(CAMERA_X - pX), 2) + Math.pow((double)(CAMERA_Z - pZ), 2)) < 50){
buildingIndex = 1;
return;
}
}else if(building2Distance == distances[i]){
if(Math.sqrt(Math.pow((double)(CAMERA_X - pX), 2) + Math.pow((double)(CAMERA_Z - pZ), 2)) < 30){
buildingIndex = 2;
return;
}
}else if(building3Distance == distances[i]){
if(Math.sqrt(Math.pow((double)(CAMERA_X - pX), 2) + Math.pow((double)(CAMERA_Z - pZ), 2)) < 50){
buildingIndex = 3;
return;
}
}else{
if(Math.sqrt(Math.pow((double)(CAMERA_X - pX), 2) + Math.pow((double)(CAMERA_Z - pZ), 2)) < 10){
buildingIndex = 4;
return;
}
}
}
buildingIndex = -1;
似乎总是将buildingIndex 设置为-1。
调试并不容易,因为我正在制作一个 Google Cardboard 应用程序,并且要在我的世界中移动,我必须拔下 USB 并插入键盘(我没有蓝牙键盘)。
谁能看出我的逻辑/代码有问题?
谢谢
【问题讨论】:
标签: java android camera opengl-es-2.0 trigonometry