JavaScript: 简单减法 returns NaN
JavaScript: Simple subtraction returns NaN
我有一个问题,
我的碰撞检测功能有时会将实体位置设置为 NaN。
当我打开控制台(在 chrome 上)时,实体的位置和碰撞是有效数字,但有时它们会相互减去 returns 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
控制台截图:
所以我真的很困惑,不知道如何处理这个问题。
我需要查看更多你的代码,看看到底哪里出了问题,但 NaN 肯定是对 undefined
类型进行数学运算的结果,所以请仔细查看是否有 [=10] =]
我又重写了一次代码,但我再也没有得到 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;
}
}
}
但感谢您的帮助:)
我有一个问题, 我的碰撞检测功能有时会将实体位置设置为 NaN。 当我打开控制台(在 chrome 上)时,实体的位置和碰撞是有效数字,但有时它们会相互减去 returns 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
控制台截图:
所以我真的很困惑,不知道如何处理这个问题。
我需要查看更多你的代码,看看到底哪里出了问题,但 NaN 肯定是对 undefined
类型进行数学运算的结果,所以请仔细查看是否有 [=10] =]
我又重写了一次代码,但我再也没有得到 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;
}
}
}
但感谢您的帮助:)