2015-12-29 53 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的角度下工作良好,但角度越大失真越多