【问题标题】:Libgdx/Java: After converting GPS to pixelcoordinates, route is rotated 90°Libgdx/Java:将 GPS 转换为像素坐标后,路线旋转 90°
【发布时间】:2018-12-09 04:07:33
【问题描述】:

我之前写过关于在我的 libgdx 项目中实现地图的文章。

我使用上述谷歌地图的快照,导入快照的 GPS 边界、路线纬度值、位置服务(通过接口)和快照作为 Gdx.files.local 字符串。

希望我现在遇到的最后一个问题是路线顺时针旋转了大约 45 度。否则我的“敌人”会走上完美的道路。我已经发现我必须“翻转”我的 y 轴;在此之前它被旋转并倒置。

我希望这里有更多经验的人之前可能处理过类似的事情并提供一些建议:)

这基本上是创建 Waypoint 数组的代码,在将 GPS 坐标转换为对应于地图快照的 gps 边界的像素坐标(左下角和右上角 see here ,以及地图纹理的宽度和高度。

private void convertPathToScreen(double[] gpsRoute){
    for(int i = 0; i<gpsRoute.length; i++){
        if(i % 2 != 0) {
            screenRouteCoordinates[i] = 
x_GpsToScreenConversion(gpsRouteCoordinates[i]);
        }
        else{
            screenRouteCoordinates[i] = 
y_GpsToScreenConversion(gpsRouteCoordinates[i]);
        }

    }
}

public int x_GpsToScreenConversion(double x_pointInGpsCoords){
    double percentage = 1 - Math.abs((x_pointInGpsCoords - x_GpsMin) / 
(x_GpsMax - x_GpsMin));
    return  (int)((percentage* Math.abs(mapWidth - mapOrigin)) + mapOrigin);
}


public int y_GpsToScreenConversion(double y_pointInGpsCoords){
    double percentage = (y_pointInGpsCoords - y_GpsMin) / (y_GpsMax - y_GpsMin);
    return  (int)((percentage* Math.abs(mapHeight - mapOrigin)) + mapOrigin);
}

编辑:现在我想起来了,错误可能在我的寻路代码中,尽管我在推进我的项目之前对其进行了测试,并且它对于我输入的所有值都有效。无论如何,为了完整性......为了方便

private void calculatePathing(){
    angle = (float) (Math.atan2(waypointsToGoal[waypoint].y - getY(), waypointsToGoal[waypoint].x - getX()));
    velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);
}

所以问题基本上是:如何解决让我的游戏出错的 90° 顺时针旋转问题?我可以围绕地图中心(所有敌人走到哪里)旋转数组的坐标还是这里的转换代码有错误?

【问题讨论】:

    标签: java android libgdx android-gps


    【解决方案1】:

    解决方案:(拼凑,但它可以完成工作!)

    我只是将我的航路点旋转了我需要围绕目的地点的度数。它没有解决根本问题,但它解决了症状。

    private void createWaypointArray(){
    
        //formula requires radians
        double angle = Math.toRadians(90);
    
        double current_x;
        double current_y;
    
        // waypointCache.size()-1 gets me the last waypoint, the destination around which I rotate
        double center_x = waypointCache.get(waypointCache.size()-1).x;
        double center_y= waypointCache.get(waypointCache.size()-1).y;
    
        // Loop through Vector2 Array, rotate the points around destination and save them
        for(int i = 0; i < waypointCache.size()-1; i++){
            current_x = waypointCache.get(i).x;
            current_y = waypointCache.get(i).y;
    
            waypointCache.get(i).x = (float)((current_x-center_x) * Math.cos(angle) - (current_y-center_y) * Math.sin(angle) + center_x);
            waypointCache.get(i).y = (float)((current_x-center_x) * Math.sin(angle) + (current_y-center_y) * Math.cos(angle) + center_y);
    
        //  this does work, but also translates the points because it rotates around the 
        //  worldaxis, usable when points lie on normal kartesian axis I guess
        //  waypointCache.get(i).rotate(90);
        }
        this.wayPointArray = waypointCache.toArray(new Vector2[waypointCache.size()]);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多