math - Ray tracing ray-disk intersection -
so i'm trying write code sees if ray intersects flat circular disk , hoping checked out here. disk centered on negative z axis normal vector should (0,0, -1).
the way i'm doing first calculate ray-plane intersection , determining if intersection point within "scope" of disk.
in code getting numbers seem off , not sure if problem in method or if possibly somewhere else. if there wrong code appreciate feedback! =)
here code:
float d = z_intercept; //this disk intersects z-axis. can + or -. ray->d = normalize(ray->d); point p(0, 0, d); //this center point of disk point p0(0, 1, d); point p1(1, 0, d); vector n = normalize(cross(p0-p, p1-p));//calculate normal float diameter = disk_diameter; //constant value float t = (-d-dot(p-ray->o, n))/dot(ray->d, n); //calculate plane intersection point intersection = ray->o + t*ray->d; return (distance(p, intersection) <= diameter/2.0f); //see if within disk //this code calculate distance float realisticcamera::distance(point p, point i) { return sqrt((p.x-i.x)*(p.x-i.x) + (p.y-i.y)*(p.y-i.y) + (p.z-i.z)*(p.z-i.z)); }
"my disk centered on negative z axis normal vector should (0,0, -1)."
this fact simplifies calculations.
degenerated case: ray->d.z = 0 -> if ray->o.z = d ray lies in disk plane, check 2dd, else ray parallel , there no intersection
common case: t = (d - ray->o.z) / ray->d.z
if t has positive value, find x , y t, , check x^2+y^2 <= disk_radius^2
Comments
Post a Comment