java - Libgdx/Java : After converting GPS to pixelcoordinates, 路由旋转90°

标签 java android libgdx android-gps

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

我是使用所述谷歌地图的快照来做到这一点的,导入快照的 GPS 范围、route-latlong 值、位置服务(通过接口(interface))和作为 Gdx.files.local 字符串的快照。

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

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

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

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° 顺时针旋转?我可以围绕 map 中心(所有敌人走到的地方)旋转阵列的坐标吗?还是这里的转换代码有误?

最佳答案

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

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

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()]);

}

关于java - Libgdx/Java : After converting GPS to pixelcoordinates, 路由旋转90°,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110597/

相关文章:

java - android 动态壁纸设置按钮单击会导致错误 - 不幸的是,动态壁纸选择器已停止

java - 无法使用 Java 添加/实现 DocuSign 下拉框?

android - WearableListView setEnableGestureNavigation 不可用

android - Java.Lang.NoSuchMethodError : No non-static method "Landroid/content/Context;.getColorStateList(I)Landroid/content/res/ColorStateList;" 错误

java - 折线轮廓构造/绘制粗折线

java - 如何将 gdx-bullet 合并到我的游戏引擎中?

java - 更新 Eclipse Luna 和 Android SDK 显示 : "Updating Software" Has encountered a

java - Android 10 中没有适用于 Android 开发人员的 IMEI

java - 在java中,线程本地缓存内存在销毁时会刷新到主内存吗???

android - Gradle - 哪个任务创建了 'build' 目录?