【问题标题】:Why is my physics code emitting almost everywhere as occupied?为什么我的物理代码几乎到处都被占用?
【发布时间】:2013-06-30 15:38:57
【问题描述】:

我正在使用这些游戏的常用物理规则制作一个类似落沙的游戏。

它适用于我从文件加载关卡这一事实。

因此,要检查一个地方是否为空,它会检查已加载级别上的RGB 值。

但由于某种原因,它返回几乎每个单元格(除了相同的几个单元格)都被某些东西占用,即使它不是。

物理是通过最初加载一个文件,然后修改它来处理的。

文件类型为PNG格式,BufferedImage类型为TYPE_INT_ARGB

这是主类中的所有物理处理代码。

public static void updateParticles()
    {
    for(int i=0;i<100;i++)
        {
        for(int j=0;j<75;j++)
        {
        int rgb = levels.getRGB(i,j);
        int sx = getMappedCoordX(levels.getRGB(i,j));
        int sy = getMappedCoordY(levels.getRGB(i,j));
        try{
            if(materialRGBmap[(i*32)+j]==solRGB)
                {
                int[] coords = physicsRules.getSolidMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),getFree(i-1,j+1),getFree(i+1,j+1),i*8,j*8);
                levels.setRGB(i,j,map_null.getRGB(0,0));
                levels.setRGB(coords[0]/8,coords[1]/8,rgb);
                }
            if(materialRGBmap[(i*32)+j]==liqRGB)
                {
                int[] coords = physicsRules.getLiquidMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),i*8,j*8);
                levels.setRGB(i,j,map_null.getRGB(0,0));
                levels.setRGB(coords[0]/8,coords[1]/8,rgb);
                }
            if(materialRGBmap[(i*32)+j]==gasRGB)
                {
                int[] coords = physicsRules.getGasMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),getFree(i+1,j),i*8,j*8);
                levels.setRGB(i,j,map_null.getRGB(0,0));
                levels.setRGB(coords[0]/8,coords[1]/8,rgb);
                }
            }catch(Exception e){
                System.out.println("lolcat error");
                }
            }
        }
    }

这是PhysicsRules 中的代码。 (如上图变量physicsRules

public int[] getLiquidMovable(boolean onGround, boolean leftFree, boolean rightFree, int x, int y)
{
return onGround?(rand.nextInt(2)==0 && leftFree?new int[]{x-8, y}:(rightFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}

public static int[] getLiquidMovableStatic(boolean onGround, boolean leftFree, boolean rightFree, int x, int y)
{
return onGround?(Srand.nextInt(2)==0 && leftFree?new int[]{x-8, y}:(rightFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}

public int[] getSolidMovable(boolean onGround, boolean leftFree, boolean rightFree, boolean leftUFree, boolean rightUFree, int x, int y)
{
return onGround?(rand.nextInt(2)==0 && leftFree && leftUFree?new int[]{x-8, y}:(rightFree && rightUFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}

public int[] getSolidMovableStatic(boolean onGround, boolean leftFree, boolean rightFree, boolean leftUFree, boolean rightUFree, int x, int y)
{
return onGround?(Srand.nextInt(2)==0 && leftFree && leftUFree?new int[]{x-8, y}:(rightFree && rightUFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}

public int[] getGasMovable(boolean leftFree, boolean rightFree, boolean upFree, boolean downFree, int x, int y)
{
int dir = rand.nextInt(4);
return dir==0 && upFree?new int[]{x,y-8}:(dir==1 && leftFree?new int[]{x+8,y}:(dir==2 && downFree?new int[]{x,y+8}:(dir==3 && rightFree?new int[]{x-8,y}:new int[]{x,y})));
}

public static int[] getGasMovableStatic(boolean leftFree, boolean rightFree, boolean upFree, boolean downFree, int x, int y)
{
int dir = Srand.nextInt(4);
return dir==0 && upFree?new int[]{x,y-8}:(dir==1 && leftFree?new int[]{x+8,y}:(dir==2 && downFree?new int[]{x,y+8}:(dir==3 && rightFree?new int[]{x-8,y}:new int[]{x,y})));
}

【问题讨论】:

  • 请在您的问题中缩进代码。目前很难阅读。 (老实说,我鼓励你接受一般的空白......)
  • 您的标题指的是“物理”代码,但您的文字清楚地表明该问题与模拟物理无关,与文件访问或初始化内存有关代表。
  • 该代码用于通过最初加载文件然后对其进行编辑来处理物理。
  • @BottleFact:但是您的编码问题与物理或模拟无关,因此您标题的这一部分是无关紧要且不必要的。只是一个帮助你的建议,因为一个更清晰的问题通常会得到更好更快的答案。此外,您将希望摆脱代码中的幻数。还可以考虑使用一点数学来防止此问题发生。
  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。

标签: java physics rgb bufferedimage


【解决方案1】:

J 是从 0 到 75,但 i 使用 32 步宽(乘以 32),因此 J 与下一个元素变量字段重叠。

materialRGBmap[(i*32)+j]

正在使用上次迭代使用的地图元素。如果将 j 的限制更改为 32,则每次 i 迭代只能计算/使用 32 宽的字段。

另外,您并没有像并行那样进行逐元素计算。您正在一个一个地改变元素,而不是同时改变所有元素。您需要临时数组来存储结果,最后将全部复制到原始数组中。

深切注意:当您使用 1000 x 1000 区域时,请远离放置单个点(绘画部分)。编辑图像然后绘图更快。

【讨论】:

  • 它的目的是为了将两个X和Y坐标转换为数组的一个数字。
  • 就像素/元素而言,您的落沙面积是多少?
  • 我使用 printStackTrace 来解决这个错误,显然这是一个 ArrayOutOfBoundsException: Coordinate out of bounds!难道是我没有限制 coord[] 数组?
  • 只需将区域重新调整为 32*100 或仅使用 materialRGBmap[(i*75)+j]
  • 我正在使用 (i*75)+j,但仍然无法正常工作。 D= 如果有帮助,请使用 PhysicsRules 中的代码更新问题。
猜你喜欢
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2022-01-15
  • 2016-09-22
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
相关资源
最近更新 更多