JavaScript:简单减法返回NaN
我有个问题, 我的碰撞检测功能有时会将实体位置设置为NaN。 当我打开控制台(在chrome上)时,实体的位置和碰撞是有效的数字,但将它们相互减去有时会返回NaNJavaScript:简单减法返回NaN,javascript,nan,Javascript,Nan,我有个问题, 我的碰撞检测功能有时会将实体位置设置为NaN。 当我打开控制台(在chrome上)时,实体的位置和碰撞是有效的数字,但将它们相互减去有时会返回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;
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肯定是对未定义的
类型进行数学运算的结果,因此,请仔细查看是否有未定义的内容
我已再次重写代码,并且不再获取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;
}
}
}
updateCollision=函数(实体,矩形){
var a=entity.x-rect.x;
var b=实体y-直接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;
变量closestX,closestY,aX,aY;
if(未旋转圆环矩形x+矩形宽度/2)
{
closestX=rect.x+rect.width/2;
aX=最接近的x;
}
其他的
{
closestX=未旋转的圈;
aX=矩形x;
}
if(未旋转圆<矩形y-矩形高度/2)
{
closestY=rect.y—rect.height/2;
aY=紧密的;
}
else if(未旋转圆圈>矩形y+矩形高度/2)
{
closestY=rect.y+rect.height/2;
aY=紧密的;
}
其他的
{
closestY=未旋转的圆圈;
aY=矩形y;
}
var冲突=假;
var distance=getDistance(未旋转的圆圈、未旋转的圆圈、closestX、closestY);
if(距离
但是谢谢你的帮助:)也许你把updateCollision说得很糟糕-你的console.log entity和rect怎么样,并确保每个的x/y属性都是实际的数字你应该记录一些更有用的东西,比如
a
和b
的值。请更新问题以包括问题的答案。你的屏幕截图显示了rome的开发工具,您可以在代码暂停时突出显示内容,但明显没有显示您暂停的语句(将以蓝色突出显示)因此,尽管这些变量很可能有那些值,不管是什么语句,但它显然不在代码的屏幕截图中,也不在函数的开头,在那里计算a
和b
,所以我们无法帮你弄清楚到底发生了什么。-你的更新冲突代码在正确的情况下运行良好输入我不知道为什么我看不到你的屏幕截图,但是,未定义
可能是个问题,当你把一个对象传递给一个函数时,如果你给它的一个属性赋值(未定义),整个对象都会受到影响(因为它是通过引用实现的),我在使用turbulenz游戏引擎碰撞检测时也遇到了这个问题,这是一些未定义的基纳,
。所以将函数更改为函数(entity_x,entity_y,rect_x,rect_y),看看它是否有效。