所以我正在尝试编写一种非常具体的方法来获得定向2D碰撞,这一个双重不断给我一个NaN值。它使用的值都不是NaN,因此它必须是这一行中的值,其中值为
double dX = Math.Sqrt(Math.Pow(collisionCorner.X - playerCorner.X, 2) +
Math.Pow((collisionCorner.X - (2 * playerCorner.Y) +
(thatOnePart * playerCorner.X)) / thatOnePart, 2));
ThatOnePart值对我来说只是一个快捷方式,这是我需要在这里使用很多的东西所以这就是为什么我做了它,如果它很重要我会在这里添加它:
double thatOnePart = ((newPlayerCorner.Y - playerCorner.Y) /
(newPlayerCorner.X - playerCorner.X));
更新
我把它全部分解为单独的操作以查看它出错的地方:
double dx1 = collisionCorner.X - playerCorner.X;
Console.WriteLine("dx1 operation 1: " + dx1);
dx1 = Math.Pow(dx1, 2);
Console.WriteLine("dx1 operation 2: " + dx1);
double dx2 = thatOnePart / playerCorner.X;
Console.WriteLine("dx2 operation 1: " + dx2);
dx2 -= playerCorner.Y * 2;
Console.WriteLine("dx2 operation 2: " + dx2);
dx2 += collisionCorner.X;
Console.WriteLine("dx2 operation 3: " + dx2);
dx2 = dx2 / thatOnePart;
Console.WriteLine("dx2 operation 4: " + dx2);
dx2 = Math.Pow(dx2, 2);
Console.WriteLine("dx2 operation 5: " + dx2);
double dX = dx1 + dx2;
Console.WriteLine("dX operation 1: " + dX);
dX = Math.Sqrt(dX);
Console.WriteLine("dX operation 2: " + dX);
控制台给了我这个:
dx1操作1:27,1995391845703 dx1操作2:739,814931852976 dx2操作1:8 dx2操作2:8 dx2操作3:8 dx2操作4:NaN dx2操作5:NaN dX操作1:NaN dX操作2:NaN
所以当它被theOnePart划分时会出错,但是在这个例子中thatOnePart的值是8,所以我仍然不知道这里出了什么问题
更新:
所以我在所有这些操作之前为所有使用的值添加了一条写字符,这样你就可以看到它们是什么,代码现在是:
Console.WriteLine("collisionCornerX = " + collisionCorner.X + " thatOnePart = " + thatOnePart + " playerCornerX = " + playerCorner.X + " playerCornerY = " + playerCorner.Y);
double dx1 = collisionCorner.X - playerCorner.X;
Console.WriteLine("dx1 operation 1: " + dx1);
dx1 = Math.Pow(dx1, 2);
Console.WriteLine("dx1 operation 2: " + dx1);
double dx2 = thatOnePart / playerCorner.X;
Console.WriteLine("dx2 operation 1: " + dx2);
dx2 -= playerCorner.Y * 2;
Console.WriteLine("dx2 operation 2: " + dx2);
dx2 += collisionCorner.X;
Console.WriteLine("dx2 operation 3: " + dx2);
dx2 = dx2 / thatOnePart;
Console.WriteLine("dx2 operation 4: " + dx2);
Console.WriteLine("thatOnePart: " + thatOnePart);
dx2 = Math.Pow(dx2, 2);
Console.WriteLine("dx2 operation 5: " + dx2);
double dX = dx1 + dx2;
Console.WriteLine("dX operation 1: " + dX);
dX = Math.Sqrt(dX);
Console.WriteLine("dX operation 2: " + dX);
并且控制台给了我:
collisionCornerX = 224
thatOnePart = 8
playerCornerX = 193,6004
playerCornerY = 246,0005
dx1 operation 1: 30,3995513916016
dx1 operation 2: 924,132724810625
dx2 operation 1: 8
dx2 operation 2: 8
dx2 operation 3: 8
dx2 operation 4: NaN
thatOnePart: 8
dx2 operation 5: NaN
dX operation 1: NaN
dX operation 2: NaN
更新:
我检查了每个参数的值,这是控制台给我的,它们在整个过程中都保持不变
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005
playerCornerY = 227,6004
dx1 operation 1: 29,5995483398438
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dx1 operation 2: 876,133261922747
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dx2 operation 1: -8
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dx2 operation 2: -8
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dx2 operation 3: -8
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dx2 operation 4: NaN
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
thatOnePart: -8
dx2 operation 5: NaN
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dX operation 1: NaN
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
dX operation 2: NaN
collisionCornerX = 224 thatOnePart = -8 playerCornerX = 194,4005 playerCornerY = 227,6004
答案 0 :(得分:3)
如果您咨询documentation of the Sqrt method,可以找到:
您可以在方法中输入负输入或NaN输入作为参数 Devision by 0将导致正无穷大:
NAN
查看documentation of the Pow method如果输入(x,y)
为{{1}},它将返回{{1}}:
x< 0但不是NegativeInfinity; y不是整数,NegativeInfinity或PositiveInfinity。
或
x = -1; y = NegativeInfinity或PositiveInfinity。
或
x或y = NaN。
所以其中一个案例就是它。