【问题标题】:Calculating position in an array with screen coordinates for an isometric game使用等距游戏的屏幕坐标计算数组中的位置
【发布时间】:2013-11-20 19:40:14
【问题描述】:

我正在使用this 站点在 java 中制作一个非常基本的 Isometric 游戏,只有它用于将屏幕坐标转换为数组中的位置的方法不起作用。

这是我用来将屏幕坐标转换为数组位置的方法:

public void GetMouseInArray(){
    int[] pos = new int[2];

    pos[0] = window.getMousePosition().x;
    pos[1] = window.getMousePosition().

    pos = CalcCarPos(pos);

    int[] temppos = new int[2];
    temppos[0] = (int) Math.floor(pos[0]/80);
    temppos[1] = (int) Math.floor(pos[1]/80);
    pos = temppos;
}

这是 CalcCarPos 函数:

private int[] CalcCarPos(int[] pos){
    int[] temppos = new int[2];
    temppos[0] = (2*pos[1]+pos[0])/2;
    temppos[1] = (2*pos[1]-pos[0])/2;
    return temppos;
}

我使用几乎相同的代码在屏幕上绘制数组,我的图块是 80x80 像素。我在这里做错了什么?

【问题讨论】:

  • 建议使用java.awt.Point 来存储坐标,因为它比int[] 稍微干净一些
  • 你怎么知道它不起作用?
  • 谢谢,我把所有的整数都改成了点。我知道它不起作用,因为我将屏幕标题更改为转换后的位置。
  • "更改屏幕标题" 这样您就创建了一个窗口并正在测试其中的代码?代码你也请包括你正在测试它的代码?

标签: java arrays coordinates isometric


【解决方案1】:
temppos[0] = (2*pos[1]+pos[0])/2;
temppos[1] = (2*pos[1]-pos[0])/2;

这是你想要的吗?这就是说将 pos[1] 乘以 2,将 pos[0] 添加到该结果,然后除以 2。还是要将 pos[1] 和 pos[0] 的总和乘以 2?

temppos[0] = (2*(pos[1]+pos[0]))/2;
temppos[1] = (2*(pos[1]-pos[0]))/2;

【讨论】:

  • 假设我选择了 X: 165, Y: 165。你想返回数组位置 1,1?正确。
猜你喜欢
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多