# Ray tracing the next week: understanding Instances

My question is not about the code, but it's about the approach used by the author Peter Shirley (and apparently common practice in ray tracers) of handling Instances. To understand the question it's necessary to be familiar with the book, because posting the code for every class involved is not feasible. Something I don't understand is how rays behave.

Let's consider the traslation. The idea is to offset the origin, and check if the new ray - which is now shot from the new origin with the same direction of the original ray - hits the object. If so, we shift the hitting point by an equal and opposite offset. Two questions:

1) Why is it not sufficient to shoot rays from the "standard" origin, and offset the hitting point without actually shifting the origin first? Example: the ray is shot, if it hits the object in point (x,y) I move the hitting point by 2 (x+2, y+2) and store it.

2) The hit function of the class translate is only called when the original ray hits the object? Because in that case, the translation would not result in the whole object, since it would only store those points that are first hit by the original ray, and for which the new ray hits the object again.

## 1 Answer

There are two ways to 'translate' your object. The first is by moving each point of your object by the desired translation. The second is by translating the origin of your coordinate system. In this case it's the latter. Basically it turns out to be the same, whether you translate your object by a vector $$\vec{t}$$ or whether you translate your origin by $$-\vec{t}$$.

For 2) note that you only care about the intersected points (since that is what you use to compute the pixel color), so you compute the offset only when you need it. So yes, you do translate the origin of the ray at each intersect, it's the same as translating the object though (at each ray intersect), since $$\vec{p}+\vec{t} - \vec{o} = \vec{p} - (\vec{o}-\vec{t})$$.

• Yes, that's my intuition. Note 1), when does this translation happen? I am shooting rays from the origin into each pixel, and at some point I 'move' my origin and shoot rays from the new origin. Does this happen only when I hit the class "translation"? Note 2) yes, but since I only offset when both original ray and new ray hit the object, only a small portion of the object will be moved. If the object goes from x=1 to x=4, and I move my origin from 0 to -2, the only portion of the object moved is between x=2 and x=4 (for x<2 the new ray does not hit the object) – maurocomi Feb 22 at 16:11
• @MauroComi I don't get what you mean by 'hit the class translation'. You don't offset only a part of the object, you offset the ray origin, which is the same as offsetting the object. Note that if you translate an object, rays that previously intersected it, will not anymore - it's the same thing. What you should take away from this is that coordinate system translation by $-\vec{t}$ and object translation by $\vec{t}$ are the same thing. If you formulate it in terms of matrices, you can consider even more general transformations. – lightxbulb Feb 22 at 16:42
• Thanks for our explanation. With "hit the class translation" I mean that I offset the origin inside the function translate::hit(), which means that this function is called when the original ray hits a Hitable object inside the class translate? – maurocomi Feb 22 at 17:36
• @MauroComi Yeah as far as I can see it's just an interface to apply the transform before passing the transformed ray to the primitive hit function. People use a similar thing in sdf raymarching to distort the domain (they apply a nonlinear map to the basis). – lightxbulb Feb 22 at 18:42