首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的物理代码几乎到处都是占用的?

为什么我的物理代码几乎到处都是占用的?
EN

Stack Overflow用户
提问于 2013-06-30 23:38:58
回答 1查看 57关注 0票数 0

我正在做一个像沙子一样的游戏,使用这些游戏的常见物理规则。

它的工作原理是我从一个文件加载我的关卡。

因此,为了检查某个位置是否为空,它会检查加载级别上的RGB值。

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

物理处理方法是先加载一个文件,然后对其进行修改。

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

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

代码语言:javascript
复制
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 )

代码语言:javascript
复制
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})));
}
EN

回答 1

Stack Overflow用户

发布于 2013-06-30 23:48:03

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

代码语言:javascript
复制
materialRGBmap[(i*32)+j]

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

此外,您不是在并行地进行元素级计算。您正在逐个更改元素,而不是同时更改所有元素。你需要临时数组来存储结果,最后将所有结果复制到原始数组中。

注意:当你使用1000 x 1000区域时,不要放置单独的点(绘画部分)。编辑图像,然后绘制更快的图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17392028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档