Enjoy.

## Monday, December 17, 2007

### Mathematical Excursion #1

It's been a while since I've posted anything at all, let alone something worthwhile, so I thought I'd do something a little different for once. Here's a little glimpse into some of the more technical things I've been having to use in my personal projects as of late- calculating where to move points in 3d so that they're not intersecting another object.

We'll start with the simplest case- a sphere! Below you'll find a simple diagram of what we want to accomplish. Given a sphere of radius r, and a point that lies within it's volume, we're trying to find the new position, at the perimeter of the sphere. Going into a bit more detail, the next diagram displays all of the necessary components we need to consider in our calculation. Don't be too put off by this- I'm merely trying to illustrate that we're looking at similar triangles. All we need to know is the position of the center of the sphere, its radius, and the position of the point in question (relative to the center of the sphere). With this we can determine the distance from the point to the center of the sphere.

d = sqrt((xo - xp)^2 + (yo - yp)^2 + (zo - zp)^2)

If this distance is less than the radius of the sphere, then we know for a fact that the point lies within its volume. Now we merely have to find a single component of the corresponding similar triangle above to be able to determine some ratio between all of the similar components (read that a couple times until it makes sense). The only component we are guaranteed to have is d'- the distance from the current point to where it ought to go. This is straight forward.

d' = r - d

Now we can solve for the remaining xp', yp', and zp' components, knowing that the ratio d/d' = xp/xp' = yp/yp' = zp/zp'.

xp' = d' * xp/d
yp' = d' * yp/d
zp' = d' * zp/d

Pretty simple huh? All it took were a couple simple algebra equations and now you're moving the point in the vector (xp',yp',zp').