2017-04-02 24 views
-2

我有一個問題, 我的碰撞檢測函數有時會將實體的位置設置爲NaN。 當我打開控制檯(在鉻上)實體和碰撞的位置是有效的數字,但相互減去它們有時會返回NaN。Javaskript:簡單的減法函數返回NaN

updateCollision = function(entity,rect) { 
var a = entity.x - rect.x; // a = NaN , entity.x = 3117.2646499953607 , rect.x = 3296.976967651385 
var b = entity.y - rect.y; // b = NaN , entity.y = 3024.105915848102 , rect.y = 3144.4270586199345 

if(isNaN(a)) // isNaN(a) = true 
{ 
    console.log("not again >:("); // but console doesn't log 
} 

//the code continues but its not important 

截圖控制檯:

(screenshot of console)

所以我真的迷茫了,不知道如何處理這個問題做。

+0

也許你打電話updateCollision嚴重 - 你怎麼樣console.log實體和矩形,並確保每個x/y屬性實際上是數字 –

+0

你應該記錄更有用的東西,如'a'和'b'的值。 – Xufox

+1

請更新問題以包含問題的[mcve]。您的屏幕截圖顯示了Chrome的開發工具,並在代碼暫停時突出顯示了某些內容,但值得注意的是,**沒有顯示您在哪些語句中暫停**(將以藍色突出顯示)。因此,儘管這些變量可能具有這些值,但不管它是什麼語句,它都顯示不在該代碼的截圖中,並且在該函數的開始處計算「a」和「b」時是不對的,所以我們沒有辦法幫助你弄清楚發生了什麼事。 –

回答

0

我已經rewriten好好看看代碼一個更多的時間,我不明白的NaN值了

固定功能:

updateCollision = function(entity,rect) { 
var a = entity.x - rect.x; 
var b = entity.y - rect.y; 

var unrotatedCircleY = Math.sin((-rect.angle)/180*Math.PI)*a + Math.cos((-rect.angle)/180*Math.PI)*b +rect.y; 
var unrotatedCircleX = Math.cos((-rect.angle)/180*Math.PI)*a - Math.sin((-rect.angle)/180*Math.PI)*b +rect.x; 
var r = entity.collRad/2; 

var closestX, closestY, aX, aY; 


if (unrotatedCircleX < rect.x - rect.width/2) 
{ 
    closestX = rect.x - rect.width/2; 
    aX = closestX; 
} 
else if (unrotatedCircleX > rect.x + rect.width/2) 
{ 
    closestX = rect.x + rect.width/2; 
    aX = closestX; 
} 
else 
{ 
    closestX = unrotatedCircleX; 
    aX=rect.x; 
} 

if (unrotatedCircleY < rect.y - rect.height/2) 
{ 
    closestY = rect.y - rect.height/2; 
    aY = closestY; 
} 
else if (unrotatedCircleY > rect.y + rect.height/2) 
{ 
    closestY = rect.y + rect.height/2; 
    aY = closestY; 
} 
else 
{ 
    closestY = unrotatedCircleY; 
    aY = rect.y; 
} 


var collision = false; 

var distance = getDistance(unrotatedCircleX , unrotatedCircleY, closestX, closestY); 
if (distance < r) 
collision = true; 
else 
collision = false; 

if(collision && entity.type == "bullet") 
{ 
    entity.hp = 0; 
} 
else if(collision) 
{ 
    if(entity.type == "bullet") 
    { 
     DeleteEntity(entity); 
     return; 
    } 
    if(rect.collType == "solid") 
    { 

     var positionAngle = Math.atan2(-closestY + unrotatedCircleY , -closestX + unrotatedCircleX); 

     var y_vel = Math.sin((-rect.angle)/180*Math.PI)*entity.x_vel + Math.cos((-rect.angle)/180*Math.PI)*entity.y_vel; 
     var x_vel = Math.cos((-rect.angle)/180*Math.PI)*entity.x_vel - Math.sin((-rect.angle)/180*Math.PI)*entity.y_vel; 

     y_vel *= 0.9; 
     x_vel *= 0.9; 



     unrotatedCircleX = closestX + Math.cos(positionAngle)*(r); 
     unrotatedCircleY = closestY + Math.sin(positionAngle)*(r); 

     a = unrotatedCircleX - rect.x; 
     b = unrotatedCircleY - rect.y; 

     entity.y = Math.sin((rect.angle)/180*Math.PI)*a + Math.cos((rect.angle)/180*Math.PI)*b + rect.y; 
     entity.x = Math.cos((rect.angle)/180*Math.PI)*a - Math.sin((rect.angle)/180*Math.PI)*b + rect.x; 

     entity.y_vel = Math.sin((rect.angle)/180*Math.PI)*x_vel + Math.cos((rect.angle)/180*Math.PI)*y_vel; 
     entity.x_vel = Math.cos((rect.angle)/180*Math.PI)*x_vel - Math.sin((rect.angle)/180*Math.PI)*y_vel; 
    } 
    if(rect.collType == "trigger") 
    { 
     if(level["commandList"][rect.data].command == "tp") 
     { 
      entity.x = level["commandList"][rect.data].x; 
      entity.y = level["commandList"][rect.data].y; 
     } 
     if(level["commandList"][rect.data].command == "loadLevel") 
     { 
      levelToLoad = level["commandList"][rect.data].x; 
     } 
    } 
} 

但感謝您的幫助:)

-2

我需要看到你的代碼的更多,看什麼其實是錯誤的,但肯定楠上undefined類型的數學運算的結果,所以,如果事情是undefined

+0

我不知道爲什麼我看不到你的截圖,但是,'undefined'可能是一個問題,當你傳遞一個對象對於一個函數,如果你爲它的一個屬性賦值(未定義),整個對象都會受到影響(因爲它是通過引用的方式),當我使用turbulenz遊戲引擎碰撞檢測的時候,我遇到了這個問題,那就是一些基納'未定義'。所以將你的函數改爲函數(entity_x,entity_y,rect_x,rect_y),看它是否有效。 – StetHD