2015-12-29 55 views
0

在我的光线投射引擎中,我有这个令人烦恼的错误。 如果玩家靠近物体,则会产生巨大的透镜效应。 我已经看过这个问题,并尝试了他建议的修复。 这里是我当前实现的raycasting部分中的代码(它位于我的paint方法中)之后我也将文本绘制到屏幕上。Lensing在我的光线投射引擎

double distance; 
double lineHeight=-1; 
double angleOff;    
double angleCast; 
for(int i=0;i<900;i++){ 
    //i is the line across from the side. 
    angleOff=Math.atan(((i-450.0)/4000.0)/.1); 
    boolean hit=false; 
    board.setColor(GameFrame.m.getBackground()); 
    board.drawLine(i, 922, i, 0); 
    for(double rayDist=0;!hit;rayDist+=.01){ 
     try{ 
      char block=Map.map[(int) (Map.p.x+Math.sin(angleOff+Map.p.angle)*rayDist)][(int) (Map.p.y-Math.cos(angleOff+Map.p.angle)*rayDist)]; 
      if(block!='o'){ 
       lineHeight=800/(rayDist*Math.abs(Math.cos(angleOff+Map.p.angle))); 
       switch(block){ 
        case 'w': 
         board.setColor(Color.BLACK); 
         break; 
        case 'z': 
         board.setColor(Color.red); 
         break; 
        case 'g': 
         board.setColor(Color.green); 
         break; 
        case 'a': 
         board.setColor(Color.DARK_GRAY); 
         break; 
       } 
       hit=true; 
      } 
     }catch(Exception e){ 
      hit=true; 
      lineHeight=800/(rayDist*Math.abs(Math.cos(angleOff+Map.p.angle))); 
      board.setColor(Color.black); 
     } 
    } 

    board.drawLine(i,(int) (461-(lineHeight/2)), i,(int) (461+(lineHeight/2))); 
} 

请注意,Map.p.angle是玩家物体弧度的角度(朝上)。 另请注意,地图存储为char[][],其中'w'是墙壁,'z'是僵尸,'g'是枪电源,'a'是弹药。 如果有人能让我知道是什么导致这种镜头,我会非常感激。 这里有一些截图 功能正常 See how currently it looks (passably) 3d 错误 Note the huge fisheye lensing effect 另一幅影像 enter image description here

如果有人能告诉我需要应用我会很满意什么修正。

回答

0

发现此问题。 这是我的这条线 lineHeight = 800 /(rayDist Math.abs(Math.cos(angleOff + Map.p.angle))); 它应该是 lineHeight = 800 /(rayDist Math.abs(Math.cos(angleOff))); 这使我的代码在接近0的角度下工作良好,但角度越大失真越多