public Hit Hit(Ray ray) { var oc = ray.origin - center; float a = Vector3.Dot(ray.direction, ray.direction); float b = Vector3.Dot(oc, ray.direction); float c = Vector3.Dot(oc, oc) - radius * radius; float dis = b * b - a * c; if (dis > 0) { float e = (float)Math.Sqrt(dis); float t = (-b - e) / a; if (t > 0.007f) { var point = ray.Point(t); return(new Hit(t, point, (point - center).Unit())); } t = (-b + e) / a; if (t > 0.007f) { var point = ray.Point(t); return(new Hit(t, point, (point - center).Unit())); } return(default);
public bool Hit(ref Ray ray, ref Hit hit) { var oc = ray.origin - center; float a = Vector3.Dot(ray.direction, ray.direction); float b = Vector3.Dot(oc, ray.direction); float c = Vector3.Dot(oc, oc) - radius * radius; float dis = b * b - a * c; if (dis > 0) { float e = MathF.Sqrt(dis); float t = (-b - e) / a; if (t > 0.007f) { var point = ray.Point(t); var unit = (point - center).Unit(); hit = new Hit(t, ref point, ref unit); return(true); } t = (-b + e) / a; if (t > 0.007f) { var point = ray.Point(t); var unit = (point - center).Unit(); hit = new Hit(t, ref point, ref unit); return(true); } } return(false); }
public Hit?Hit(Ray ray) { var oc = ray.origin.Sub(this.center); float a = ray.direction.Dot(ray.direction); float b = oc.Dot(ray.direction); float c = oc.Dot(oc) - this.radius * this.radius; float dis = b * b - a * c; if (dis > 0) { float e = (float)Math.Sqrt(dis); float t = (-b - e) / a; if (t > 0.007f) { var hit = new Hit(); hit.dist = t; hit.point = ray.Point(t); hit.normal = hit.point.Sub(this.center).Unit(); return(hit); } t = (-b + e) / a; if (t > 0.007f) { var hit = new Hit(); hit.dist = t; hit.point = ray.Point(t); hit.normal = hit.point.Sub(this.center).Unit(); return(hit); } return(null); } return(null); }
public Hit Hit (Ray ray) { var oc = ray.origin.Sub(this.center); float a = ray.direction.Dot(ray.direction); float b = oc.Dot(ray.direction); float c = oc.Dot(oc) - this.radius * this.radius; float dis = b * b - a * c; if (dis > 0) { float e = (float) Math.Sqrt(dis); float t = (-b - e) / a; if (t > 0.007f) { var hit = new Hit(); hit.dist = t; hit.point = ray.Point(t); hit.normal = hit.point.Sub(this.center).Unit(); return hit; } t = (-b + e) / a; if (t > 0.007f) { var hit = new Hit(); hit.dist = t; hit.point = ray.Point(t); hit.normal = hit.point.Sub(this.center).Unit(); return hit; } return null; } return null; }