Post by vartsia on May 22, 2019 19:20:34 GMT
Hi there!
Could anyone help me with this problem?
All the tests are passed until the moment I implementing this ray transform part at the beginning of this intersect function.
It's on the page 70 in the pdf version of the book.
Here is my implementation:
After the implementation my discriminant value goes negative and that breaks all the rest.
Any idea what I'm doing wrong?
I'm curious of what values for the discriminant you get after implementing the ray transformation at the beginning of the function?
I have no idea why it goes under zero or where should I try to find the error.
Implementing the ray transform also breaks all the earlier tests where the intersect ( or in my code the intersectRGSphere) function is used.
The whole project could be found here
Could anyone help me with this problem?
All the tests are passed until the moment I implementing this ray transform part at the beginning of this intersect function.
It's on the page 70 in the pdf version of the book.
function intersect(sphere, ray)
ray2 ← transform(ray, inverse(sphere.transform))
# ...
end function
Here is my implementation:
public func intersectRGSphere(_ sphere: RGSphere, ray: RGRay) -> [RGIntersection] {
var t: [RGIntersection] = []
let tRay = transformRGRay(ray, matrix: sphere.transform.inverse)
/*
The vector form the sphere's center, to the ray origin
# Remember: the sphere is centered at the world origin
*/
let shpere_to_ray = tRay.origin - newRGPoint(x: 0, y: 0, z: 0)
let a = simd_dot(tRay.direction, tRay.direction)
let b = Float(2.0) * simd_dot(tRay.direction, shpere_to_ray)
let c = simd_dot(shpere_to_ray, shpere_to_ray) - Float(1.0)
let discriminant = (b * b) - Float(4.0) * a * c
if discriminant < Float(0.0) {
return t
}
let t1 = (-b - sqrtf(discriminant)) / (Float(2.0) * a)
let t2 = (-b + sqrtf(discriminant)) / (Float(2.0) * a)
t.append(RGIntersection(t: t1, object: sphere))
t.append(RGIntersection(t: t2, object: sphere))
return t.sorted(by: {
$0.t < $1.t
} )
}
After the implementation my discriminant value goes negative and that breaks all the rest.
Any idea what I'm doing wrong?
I'm curious of what values for the discriminant you get after implementing the ray transformation at the beginning of the function?
I have no idea why it goes under zero or where should I try to find the error.
Implementing the ray transform also breaks all the earlier tests where the intersect ( or in my code the intersectRGSphere) function is used.
The whole project could be found here