hgkjshegfskef

hgkjshegfskef

The Ray Tracer Challenge: cannot pass scaling test (page 69-70)

The test is as follows:

Scenario: Intersecting a scaled sphere with a ray
Given r ← ray(point(0, 0, -5), vector(0, 0, 1))
And s ← sphere()
When set_transform(s, scaling(2, 2, 2))
And xs ← intersect(s, r)
Then xs.count = 2
And xs[0].t = 3
And xs[1].t = 7

However, I get 1.5 and 3.5 respectively.

Looks like I have some misunderstanding about how to do the calculations, because both on paper and in the code I arrive at the same conclusion. So, I will write out my intermediate calculations.

We start by creating a scaling matrix, which is the transformation attached to the sphere:

2 0 0 0
0 2 0 0
0 0 2 0
0 0 0 1

Then we invert it (since we need to transform the ray by the inverse of sphere’s transformation):

0.5   0   0  0
0   0.5   0  0     = F
0     0 0.5  0
0     0   0  1

Then we multiply original ray’s point component with it and get:

    0       0
F * 0  =    0
   -5    -2.5

Then we multiply original ray’s vector component with it and get:

    0       0
F * 0  =    0
    1     0.5

So, our new Ray is { Point(0,0,-2.5), Vector(0,0,0.5) }, which is a point that lies on Z axis, looking towards the sphere.

To demonstrate that the algorithm for finding intersections between ray and sphere is correct (without writing it out here), we calculate it backwards: we know that the sphere is radius 1, centered at (0,0,0). My intersections are: 1.5, 3.5, which are distances from ray origin (-2.5) to points on the sphere. -2.5 + 1.5 = -1 and -2.5 + 3.5 = 1, 1 - (-1) = 2, which is the sphere diameter, so it makes sense? Or does it?

Marked As Solved

jamis

jamis

Author of Mazes for Programmers and 1 other title

Hey, I’m glad you found a solution! However, I feel like there may be a deeper issue here. The approach described in the book (transforming the ray by the inverse of the sphere’s transformation matrix, and then running the ray/sphere intersection as described) really should have “just worked”. The fact that you got t values that were half what was expected suggests that at some point maybe you were using the original ray origin/direction somewhere, or perhaps using the wrong transformation matrix?

Ultimately, as long as you’re getting the right answer now, it’s all good. I just worry that you may be doing more work than is necessary to obtain those t values, which may come back to bite you later in the book when render times grow longer and longer and every computation counts.

I’d be happy to take a look at your code if its available somewhere, if that would be helpful.

Also Liked

hgkjshegfskef

hgkjshegfskef

Helo Jamis, thank you for the reply and for the idea for the solution. The problem was with the implementation of quadratic equation solver, in particular, I was normalizing the direction vector of the transformed ray after converting it into object space. Lessons learned: don’t try to be too smart and go to wikipedia to implement line-sphere intersection according to their formula, just implement pseudocode from the book.

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
AstonJ
Or looking forward to? :nerd_face:
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: