<s>[INST] Ray Sphere Intersection I'm reading a book on raytracing and there's a part where the author is working out the equation for ray-sphere intersection. There's a part I don't understand that I'm hoping I can get help with.
The equation starts with:
$(\mathbf{p} - \mathbf{c}) \bullet (\mathbf{p} - \mathbf{c}) - r^2 = 0$
Bold values are vectors, and the dot indicates the dot product.
He then substitutes the ray in for p, making the equation:
$(\mathbf{o} + t \mathbf{d} - \mathbf{c}) \bullet (\mathbf{o} + t \mathbf{d} - \mathbf{c}) - r^2 = 0$
Next is the part I couldn't follow. He expands the equation to:
$(\mathbf{d} \bullet \mathbf{d})t^2 + [2(\mathbf{o} - \mathbf{c}) \bullet \mathbf{d}]t + (\mathbf{o} - \mathbf{c}) \bullet (\mathbf{o} - \mathbf{c}) - r^2 = 0$
I know it's kind of a stupid question, but can someone break it down how he got from equation 2 to equation 3 by expanding? I can't figure out all the intermediate steps on my own.
 [/INST] It's a matter of rearranging the terms inside and applying FOIL:
$$(\mathbf{o} + t \mathbf{d} - \mathbf{c}) \cdot (\mathbf{o} + t \mathbf{d} - \mathbf{c}) \\ = [t \mathbf{d} + (\mathbf{o} - \mathbf{c})] \cdot [t \mathbf{d} + (\mathbf{o} - \mathbf{c})] \\ = t^2 \mathbf{d} \cdot \mathbf{d} + 2t (\mathbf{o} - \mathbf{c}) \cdot \mathbf{d} + (\mathbf{o} - \mathbf{c}) \cdot (\mathbf{o} - \mathbf{c}).$$
Going from the first line to the second, I used the fact that addition and subtraction with vectors are each commutative:  $\mathbf{a} + \mathbf{b} = \mathbf{b} + \mathbf{a}.$
Going from the second line to the third, I used the fact that the dot product of two vectors is commutative:  $\mathbf{a} \cdot \mathbf{b} = \mathbf{b} \cdot \mathbf{a}.$
Also "FOIL" for the dot product of the vector sums:
$$(\mathbf{a} + \mathbf{b}) \cdot (\mathbf{c} + \mathbf{d}) = \mathbf{a} \cdot \mathbf{c} + \mathbf{a} \cdot \mathbf{d} + \mathbf{b} \cdot \mathbf{c} + \mathbf{b} \cdot \mathbf{d}.$$
</s>