Exponential Spirals for Game Effects May 4th, 2009
Patrick Stein

In earlier posts, I mentioned finding polynomials, riffing off of damped harmonic motion, and then hitting on exponential spirals all trying to come up with a nice looking way to snap game tiles back into place when they are released. I want them to overshoot and then settle into place rather than snap directly into their spot.

The Basic Spiral

I am talking about a simple spiral of the form:

\theta(t) = Kt
r(t) = \alpha\left(Kt\right)^{n}
for some integer n.

That would be an equation for a spiral that starts at the origin and heads outward as t increases. For my application though, I want to end at the origin so I need to substitute 1-t in for t.

\theta(t) = K(1-t)
r(t) = \alpha\left(K(1-t)\right)^{n}

The math is also going to work out slightly better if I use n-2 in place of n in the above equations:

\theta(t) = K(1-t)
r(t) = \alpha\left(K(1-t)\right)^{n-2}

I don’t want the piece to spiral into place when released though. So, really, I am concerned with just the x coordinate from the above equations:

x(t) = r(t) \cos \theta(t) = \alpha K^{n-2} (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)

To normalize everything, I am going to let \alpha = K^{2-n}. And, since I want my interpolation value to go from zero to one instead of one to zero, I am again going to subtract this all from one:

x(t) = 1 -  (1-t)^{n-2} \cos \left(K\left(1-t\right)\right)

Read the rest of this entry ⇒

A Eureka Moment May 1st, 2009
Patrick Stein

I was pondering the Phony Physics again as I set to work on my iPhone app.

In the previous post, I twiddled the equations for damped spring motion until I found something visually pleasing. Last night, I went back to the drawing (a.k.a. white) board.

What if I used an exponential spiral (parameterized by arc length). Then, I could easily adjust the number of times it bounces back and forth. I could use a spiral like r(t) = \alpha K (1-t)^n and \theta(t) = K(1-t) where K is some multiple of 2\pi. Then, I could walk along the spiral getting to the origin when t = 1 going around the origin \frac{K}{2\pi} times in the process. If I follow the curve at a fixed rate, then I guarantee that my oscillations will pick up speed as I approach the origin.

Rather than have it spiral into the center, I am just using the x-coordinate of the spiral as my new t value to interpolate with. I like the effect for the most part. It overshoots a little bit far on the first oscillation. I may tweak it some more before it’s all over. For now though, I am sticking with it.

I will post some graphs soon.

Phony Physics (a.k.a. Fun with Interpolation) April 3rd, 2009
Patrick Stein

In a previous post, I mentioned looking for a polynomial for an application. I am working on an application that involves clicking or dragging tiles around. Once you release the tile, I want it to snap to where it’s supposed to be.

The easiest way to do this is to just warp it to where it’s supposed to go. This isn’t very visually pleasing. The next best thing is to set up a time-interval on which it moves into place. Then, linearly interpolate from where it is to where it’s going (here with t normalized to range between zero and one):

(1-t)x_0 + tx_1

That’s much better than just warping, but it doesn’t have any sort of fade-in or fade-out. It instantaneously has its maximum velocity and then instantaneously stops at the end.

Typically, then one uses a cubic spline to scale the t value before interpolating to get a speed up in the beginning and then slow down in the end.
Read the rest of this entry ⇒

Find the Polynomial You’ve Been Looking For April 2nd, 2009
Patrick Stein

Often, I want to have a polynomial that meets certain criteria. For most applications in the past, I figured it all out by hand.

Today, I was having trouble getting the conditions specified enough to get the sort of polynomial that I was expecting. After finding the coefficients for several sixth degree polynomials in a row, I figured I should instead be able to do something like this with the proper lisp:

(calculate-polynomial-subject-to
    (value :at 0 :equals 0)
    (derivative :at 0 :equals 0)
    (nth-derivative 2 :at 0 :equals 0)
    (value :at 1 :equals 1)
    (derivative :at 1 :equals 0)
    (nth-derivative 2 :at 1 :equals 0)
    (value :at 1/2 :equals 3/4))

That’s all done now (polynomials.lisp). So, for the record, the above is: 26x^3 - 63x^4 + 54x^5 - 16x^6. That is still not quite the polynomial I want for this application, but it’s close. A few minutes of Lisp saved me hours of whiteboard work.

Multiple Bank Robberies Is Not Surreal January 6th, 2009
Patrick Stein

On December 29th, there were five bank robberies in New York City. The NPR bit on it contained this tidbit:

Anahad O’Connor, metro and breaking-news reporter for The New York Times, says the news was a little surreal because last year there was fewer than one bank robbery a day in the city.

So, how surreal is it, actually? According to O’Connor’s article, there were 280 bank robberies in the first 362 days of 2007. So, there is an average of 280/362 bank robberies per day in NYC (at least for most of 2007).

Now, it would be hard to believe that bank robbers don’t have preferred days of the week. For example, outside of Hollywood films, it is probably much more likely that a bank gets robbed while there are tellers on duty than on days when the bank is closed. It is technically easier to get a person to give you money than to get a 10 inch steel door to give you money.

So, there are probably some days where the raw likelihood is higher than other days. But, for the sake of argument, let’s say that all days are equiprobable since this makes five robberies in one day as improbable as it can be with still an overall average of 280/362 robberies a day.

Similarly, any conspiracy to rob multiple banks would probably tend to cluster them for the same time period so as to thin out the police response. So, let’s assume that each robbery is entirely independent of the others.

Under these minimizing assumptions, the number of NYC bank robberies on a given day is a Poisson distributed random variable with \lambda = \frac{280}{362}. Thus, the probability of five robberies in a given day is a bit over 1 in 1000. [ \frac{\lambda^5 e^{-\lambda}}{5!} \approx 0.0010645 ] That’s a bit more than once every three years. That’s just not surreal.

And, it’s way less surreal than all of that. The same New York Times article says that bank robberies were up 54% at that point in 2008. There were 431 bank robberies in the first 362 days of 2008. Using 431 in place of 280 in the above calculations brings the probability up to almost 6 in 1000 (0.0059982). That’s twice a year, on average.

The moral: probability and gut instinct are not close allies.

Maybe 2008 having 431 bank robberies in the same time that 2007 had 280 wasn’t as improbable as it sounds either. If we assume the real average is 280 bank robberies in the first 362 days of a year, what are the odds of finding a year with 431 bank robberies in the first 362 days?

It turns out, it’s less than 1.5 in 10^{17}. This is roughly once every 10 million age of the universe’s. That might qualify as surreal.

Updates In Email

Email:

l