游戏编码中如何使用二次公式?
how is the quadratic formula used in game coding?
大家好,我是来自韩国的游戏程序员。
就在今天,我发现了一些使用 QUADRATIC 公式计算某些东西的代码。这是代码
hduVector3Dd p = startPoint;
hduVector3Dd v = endPoint - startPoint;
// Solve the intersection implicitly using the quadratic formula.
double a = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
double b = 2 * (p[0]*v[0] + p[1]*v[1] + p[2]*v[2]);
double c = p[0]*p[0] + p[1]*p[1] + p[2]*p[2] - m_radius * m_radius;
double disc = b*b - 4*a*c;
// The scale factor that must be applied to v so that p + nv is
// on the sphere.
double n;
if(disc == 0.0)
{
n = (-b)/(2*a);
}
else if(disc > 0.0)
{
double posN = (-b + sqrt(disc))/(2*a);
double negN = (-b - sqrt(disc))/(2*a);
n = posN < negN ? posN : negN;
}
else
{
return false;
}
// n greater than one means that the ray defined by the two points
// intersects the sphere, but beyond the end point of the segment.
// n less than zero means that the intersection is 'behind' the
// start point.
if(n > 1.0 || n < 0.0)
{
return false;
}
这是检查球体形状的函数的一部分。
我不明白为什么要使用 QUADRATIC FORMULA 来计算一些东西。
任何想法,将被preciated
我真的很想知道、理解并在未来的代码中重用它^^
好像是在做线球碰撞检查。
虽然我不太确定在哪里使用它(我可能是愚蠢的 xD)
大家好,我是来自韩国的游戏程序员。 就在今天,我发现了一些使用 QUADRATIC 公式计算某些东西的代码。这是代码
hduVector3Dd p = startPoint;
hduVector3Dd v = endPoint - startPoint;
// Solve the intersection implicitly using the quadratic formula.
double a = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
double b = 2 * (p[0]*v[0] + p[1]*v[1] + p[2]*v[2]);
double c = p[0]*p[0] + p[1]*p[1] + p[2]*p[2] - m_radius * m_radius;
double disc = b*b - 4*a*c;
// The scale factor that must be applied to v so that p + nv is
// on the sphere.
double n;
if(disc == 0.0)
{
n = (-b)/(2*a);
}
else if(disc > 0.0)
{
double posN = (-b + sqrt(disc))/(2*a);
double negN = (-b - sqrt(disc))/(2*a);
n = posN < negN ? posN : negN;
}
else
{
return false;
}
// n greater than one means that the ray defined by the two points
// intersects the sphere, but beyond the end point of the segment.
// n less than zero means that the intersection is 'behind' the
// start point.
if(n > 1.0 || n < 0.0)
{
return false;
}
这是检查球体形状的函数的一部分。 我不明白为什么要使用 QUADRATIC FORMULA 来计算一些东西。
任何想法,将被preciated 我真的很想知道、理解并在未来的代码中重用它^^
好像是在做线球碰撞检查。
虽然我不太确定在哪里使用它(我可能是愚蠢的 xD)