I’m currently learning raycasting and I was able to render several objects in terminal using functions like this (vec3 is just a struct with xyz variables) :
float hit_sphere(vec3 center, float radius, vec3 ray) {
float a = pow(magnitude(ray), 2.0);
float h = dot(ray, center);
float c = pow(magnitude(center), 2.0) - radius*radius;
float discriminant = h*h - a*c;
if (discriminant < 0.0f) return -1.0f;
else {
return (h - sqrt(discriminant)) / a;
}
}
I tried to find such a function for a torus, but all I found were some books with fancy equations that I don’t really understand.
So it would be great if you could explain to me how to make such a function for a torus.