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.

Popular Prag Prog topics Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
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
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Latest in PragProg

View all threads ❯