<p>I think there might be a misprinting in the book. I am getting this</p>

<p>$((A-C) + tB)\cdot((A-C) + tB) = R\cdot R$ </p>

<p><code>Let  A-C = Y</code></p>

<p>$(Y + tB)\cdot(Y + tB) = R\cdot R$</p>

<p>$Y\cdot Y + Y\cdot tB + tB\cdot Y + t^2B\cdot B = R\cdot R$</p>

<p>Substituting back </p>

<p>$(A-C)\cdot(A-C) + 2t * B\cdot(A-C) + t^2B\cdot B = R\cdot R$</p>

CC BY-SA 3.0
2017-10-29T14:20:49.803Z
Yeah i looked at the later code now after giving up and it was definitly a missprint. It works out with (A-C) = y as you said.
Siniyas
CC BY-SA 3.0
2017-10-29T07:03:29.237Z
2017-10-31T19:51:54.827Z
2017-10-31T19:51:54.827Z
gallickgunner
<p>So i am working through the book "Raytracing in one weekend".
At the point where i am at we want to check if a ray hits a sphere.
For that we have the equation:</p>

<pre><code>dot((A + t*B - C),(A + t*B - C)) = R*R
</code></pre>

<p>Where A + t*B is a point on the ray, with A the start, B the direction and t how far along we went on the ray.</p>

<p>To solve this the author transformed the equation to:</p>

<pre><code>t*t*dot(B,B) + 2*t*dot(A-C,A-C) + dot(C,C) - R*R = 0
</code></pre>

<p>Now i just can not understand how this transformation comes about. 
Simply multiplying this out seems to give a way different result.</p>

<p>Is there some kind of vector math i'm missing here?</p>

CC BY-SA 3.0
2017-11-14T18:31:31.283Z
I've written a detailed, step by step derivation of the math here: http://www.gabrielgambetta.com/computer-graphics-from-scratch/basic-ray-tracing.html#ray-meets-sphere Hope this helps :)
ggambetta
<raytracing><vectors>
Vector Math for Raytracer
Siniyas
# Vector Math for Raytracer

(Asked by: username_0 on 2017-10-29)

So i am working through the book "Raytracing in one weekend".
At the point where i am at we want to check if a ray hits a sphere.
For that we have the equation:

```
dot((A + t*B - C),(A + t*B - C)) = R*R

```

Where A + t\*B is a point on the ray, with A the start, B the direction and t how far along we went on the ray.

To solve this the author transformed the equation to:

```
t*t*dot(B,B) + 2*t*dot(A-C,A-C) + dot(C,C) - R*R = 0

```

Now i just can not understand how this transformation comes about.
Simply multiplying this out seems to give a way different result.

Is there some kind of vector math i'm missing here?

**Tags:** raytracing, vectors

> **Comments:**
> **username_1**: I've written a detailed, step by step derivation of the math here: http://www.gabrielgambetta.com/computer-graphics-from-scratch/basic-ray-tracing.html#ray-meets-sphere Hope this helps :)  

---

## 1 answers

### Answer by username_2 on 2017-10-29. Score: 3 | Accepted answer

I think there might be a misprinting in the book. I am getting this

$((A-C) + tB)\cdot((A-C) + tB) = R\cdot R$

`Let A-C = Y`

$(Y + tB)\cdot(Y + tB) = R\cdot R$

$Y\cdot Y + Y\cdot tB + tB\cdot Y + t^2B\cdot B = R\cdot R$

Substituting back

$(A-C)\cdot(A-C) + 2t \* B\cdot(A-C) + t^2B\cdot B = R\cdot R$

> **Comments:**
> **username_0**: Yeah i looked at the later code now after giving up and it was definitly a missprint. It works out with (A-C) = y as you said.  

---

