【发布时间】:2013-09-10 13:03:18
【问题描述】:
我一直在尝试在 java 中制作一个动态灯光系统,而不使用库。但是,出于某种原因,我似乎无法获得光线以有效地运行。它闪烁并滞后一吨。我这样做之前对游戏中的光照引擎一无所知,所以我愿意接受建议。这是我目前的更新方法:
public void updateLight( ArrayList<Block> blocks )
{
//reset light
light.reset();
//add the x and y of this light
light.addPoint( x, y );
//precision for loops
int ires = 1;
int jres = 2;
for( int i = 0; i < width; i += ires )
{
//get radians of current angle
float rdir = (float)Math.toRadians( dir + i - width/2 );
//set up pixel vars
int px, py;
for( int j = 0; j < length; j += jres )
{
//get position of pixel
px = (int)ZZmath.getVectorX( x, rdir, j );
py = (int)ZZmath.getVectorY( y, rdir, j );
//if point gets found
boolean foundpoint = false;
for( int n = 0; n < blocks.size(); n ++ )
{
//check if block is solid
//also check that collision is possible really quickly for efficiency
if( blocks.get( n ).solid )
{
//get info on block
int bx = blocks.get( n ).x;
int by = blocks.get( n ).y;
//quick trim
if( Math.abs( bx - px ) <= 32 && Math.abs( by - py ) <= 32 )
{
int bw = blocks.get( n ).w;
int bh = blocks.get( n ).h;
if( ZZmath.pointInBounds( px, py, bx, by, bw, bh ) )
{
//add point to polygon
light.addPoint( px, py );
//found point
foundpoint = true;
}
}
}
}
//if a point is found, break
if( foundpoint )
{
break;
}
//if at end of loop, add point
//loose definition of "end" to prevent flickers
if( j >= length - jres*2 )
{
light.addPoint( px, py );
}
}
}
}
这会修改显示灯光的多边形。我稍后会改变它。有什么方法可以让这个运行更好吗?此外,不,没有图书馆。我对他们没有任何意见,只是现在不想使用。
【问题讨论】:
-
你能描述一下你的过程吗?
-
灯光对象有宽度、长度和方向(方向)。光照更新方法遍历宽度,然后是长度,并根据 dir 定位一个像素。然后,它会检查所有块(我游戏中的另一个类)是否有坚固的块(不要让光线通过)。然后它会进行快速启发式“是否可能”检查,然后进行碰撞检查。如果它发生碰撞,那么它会向用于渲染的多边形添加一个点。如果它从来没有,然后达到最大长度,它会在那里增加一个点。然后,为了渲染,我只是在屏幕上拍打多边形。
-
这还不清楚。我们没有看到物体光的代码,但看起来它有一个点的集合,每个点都有一个宽度、长度和方向。如果它有效,我会写一个答案。
标签: java light raycasting