首页 > 解决方案 > 射线和球体的交点 - 求解一个二次方程

问题描述

我不知道如何求解我的二次方程来检查射线和球体之间的交点。球体应如下所示:

最终球体

        // r = radius = sphere.radius;
        // m = center = sphere.center;

        float t;

        // ray intersection uses quadratic equation
        float a, b, c, d;

        // NOT SURE ABOUT THIS PART HERE !
        a = rayVx * rayVx + rayVy * rayVy + rayVz * rayVz;
        b = 2.0f * (rayEx * rayVx + rayEy * rayVy + rayEz * rayVz - rayVx * sphere.center[0] - rayVy * sphere.center[1] - rayVz * sphere.center[2]);
        c = rayEx * rayEx - 2 * rayEx * sphere.center[0] + sphere.center[0] * sphere.center[0] + rayEy * rayEy - 2 * rayEy * sphere.center[1] + sphere.center[1] * sphere.center[1] + rayEz * rayEz - 2 * rayEz * sphere.center[2] + sphere.center[2] * sphere.center[2] - sphere.radius * sphere.radius;

        // positive discriminant determines intersection
        //d = -42;
        d = (float) Math.pow(b, 2) - (4 * a * c);
        // no intersection point? => next object
        if (d <= 0)
            continue;

        // from here: intersection takes place!

        }
    }

    // no intersection point found => return with no result
    if (minObjectsIndex == -1)
        return null;

    // intermediate version
    Random rd = new Random();
    return new Color(rd.nextFloat(), rd.nextFloat(), rd.nextFloat());

    }

    public static void main(String[] args) {
    Raytracer00 rt = new Raytracer00();

    rt.doRayTrace();
    }
}

我现在一无所知。我真的不明白如何求解a,b,c的二次方程。如果有人想查看完整的项目,我在workupload上上传了整个项目。

我来自 iOS 世界,之前从未在 Java 中做过任何事情(遗憾的是)。如果有人可以帮助我,我将不胜感激!提前致谢。

标签: javaeclipseraytracing

解决方案


推荐阅读