在阵列中制作一个圆圈(基于 Tile 的游戏光照贴图)
Making a circle in an array (Tile based game light map)
我正在 java 中制作一个基于图块的游戏,我想制作一张光照贴图。
我遇到了一些问题。我有光照贴图阵列,上面放置了影响阵列的灯光。灯以圆形发射。到目前为止看起来还不错,但这并不是我想要的。
到目前为止,这是我的代码:
for(float i = 0; i < strength + 1; i++){
for(double u = 0.0f; u < 360; u += 0.5){
double angle = u * Math.PI / 180;
int x2 = (int)(x + i * Math.cos(angle));
int y2 = (int)(y + i * Math.sin(angle));
if(map[y2][x2] > 1 - 1 / i)
map[y2][x2] = 1 - 1 / i;
}
}
结果:
正如您在结果中看到的那样,似乎光线在左下侧(红色 x)扩展得太多了。我该如何解决这个问题?
背景资料:
Strength:
The radius of how far the light reaches. This also
determines how bright the light will be at each tile of the array.
The Array "map" is a 2D float array. The engine I am using uses float
values for the alpha channel. The range is 0 (completely transparent)
to 1 (completely opaque).
解决方案(感谢 Gene):
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
}
}
您的算法存在地图索引整数截断问题。试试其他的。计算围绕中心的正方形中每个像素到中心的距离。从这个距离计算强度应该是多少。它将是这样的:
for (x = -R; x <= R; x++)
for (y = -R; y <= R; y++) {
double r = Math.sqrt(x * x + y * y);
double inv_rad = r <= R ? 1 / r : 0; // truncate outside radius R
map[yc + y][xc + x] = 1 - inv_rad;
}
这里xc和yc是整数中心坐标。 R 是围绕中心的框的一半大小。
当我尝试将其添加到我的项目时,我只得到 o.o
我输入的值是 500, 500,50
private float map[][] = new float[1000][1000];
public void test(int x, int y, float strength){
public void addLight(int x,int y,int strength ){
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
System.out.println(map[y + y2][x + x2]);
}
}
}
我正在 java 中制作一个基于图块的游戏,我想制作一张光照贴图。
我遇到了一些问题。我有光照贴图阵列,上面放置了影响阵列的灯光。灯以圆形发射。到目前为止看起来还不错,但这并不是我想要的。
到目前为止,这是我的代码:
for(float i = 0; i < strength + 1; i++){
for(double u = 0.0f; u < 360; u += 0.5){
double angle = u * Math.PI / 180;
int x2 = (int)(x + i * Math.cos(angle));
int y2 = (int)(y + i * Math.sin(angle));
if(map[y2][x2] > 1 - 1 / i)
map[y2][x2] = 1 - 1 / i;
}
}
结果:
正如您在结果中看到的那样,似乎光线在左下侧(红色 x)扩展得太多了。我该如何解决这个问题?
背景资料:
Strength:
The radius of how far the light reaches. This also determines how bright the light will be at each tile of the array.The Array "map" is a 2D float array. The engine I am using uses float values for the alpha channel. The range is 0 (completely transparent) to 1 (completely opaque).
解决方案(感谢 Gene):
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
}
}
您的算法存在地图索引整数截断问题。试试其他的。计算围绕中心的正方形中每个像素到中心的距离。从这个距离计算强度应该是多少。它将是这样的:
for (x = -R; x <= R; x++)
for (y = -R; y <= R; y++) {
double r = Math.sqrt(x * x + y * y);
double inv_rad = r <= R ? 1 / r : 0; // truncate outside radius R
map[yc + y][xc + x] = 1 - inv_rad;
}
这里xc和yc是整数中心坐标。 R 是围绕中心的框的一半大小。
当我尝试将其添加到我的项目时,我只得到 o.o 我输入的值是 500, 500,50
private float map[][] = new float[1000][1000];
public void test(int x, int y, float strength){
public void addLight(int x,int y,int strength ){
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
System.out.println(map[y + y2][x + x2]);
}
}
}