我之前写过关于在libgdx项目中实现地图的文章。
我使用了上述Google地图的快照,将快照的GPS边界,route-latlong值,locationservice(通过接口)和快照作为Gdx.files.local字符串导入。
希望,我现在遇到的最后一个问题是路线是顺时针旋转45度。否则我的“敌人”走的是完美的道路。我已经知道必须“翻转”我的y轴。在此之前,它被旋转并上下翻转。
我希望这里有更多经验的人以前可能已经处理过类似的事情并提出了一些建议:)
基本上,这是将GPS坐标转换为与地图快照的gps边界相对应的像素坐标(底部左角和右上角{{3 }},以及地图纹理的宽度和高度。
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度顺时针旋转?我可以围绕地图中心(所有敌人都走到的地方)旋转数组的坐标吗?还是这里的转换代码有误?
答案 0 :(得分:0)
解决方案:(拼凑而成,但可以完成工作!)
我只是简单地将航点旋转了所需的角度,使其绕着目的地点旋转。它不能解决根本问题,但可以解决症状。
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()]);
}