## Archive for **August 2012**

## Paint By Evolution

At one point, I wanted to join the Ludum Dare. Unfortunately, the registration emails never came. (It’s possible that my name registrar was on the fritz at the time.) On top of that, my three-year-old developed strep, and I haven’t been feeling that great this weekend, so I wasn’t sure I would do anything. However, lying awake with a sick child yielded an idea.

Painting by evolution turned out to be less a game than a toy, and a bit less fun than I expected. There are four paintings on the screen, and when you click on one, it gets replaced by some combination of the other three. The hope was that eventually you would be able to prune the randomness into something worth saving.

The frustrating part is that you don’t get to select features to keep. Careful gardening can help keep a small block of color on at least a few of the screens, but it doesn’t grow very well. Frequent pruning is required to keep the paintings reasonably in sync; otherwise, any new painting ends up looking like static.

If I keep working on it, I could add a variable mutation rate, doubling as a more visible sign of the pause state, a way to save paintings to a file, probably a form of auto-save and reload, and possibly better backgrounds.

Given that it’s not an official Dare entry, I don’t feel too bad about using clip art for the picture frames.

## Bouncing Back

I need to retract the mathematics in my previous post. A few simulations revealed that large masses were getting bounced around a bit too much, and tiny masses were exceptionally steady, hardly budging as big balls pounded them from all sides. Clearly, something was wrong.

I was right to be skeptical of the cubic mass, but wrong to trust my second set of equations, for I had made the same wrong substitution. From m₁·Δv₁=-m₂·Δv₂ I had ended up substituting -Δv₁·m₂/m₁ for Δv₂ in the conservation of energy equation. One mistake in a single term, and the whole equation explodes.

Way back when I thought Java was a good idea, I encountered a program that would let you drag around terms in an equation, and almost started an open-source version. Then again, Maple would probably have helped me even more in this case. I was very lucky to have physics professors who accepted homework in the form of Maple printouts.

Anyway, the corrected and verified equations:

`dx = (a.x - b.x)`

`dy = (a.y - b.y)`

`factor = 2 * (dx*(b.v.x - a.v.x) + dy*(b.v.y - a.v.y)) / ((a.m + b.m) * (dx**2 + dy**2))`

`a.v.x += factor * b.m * dx`

`a.v.y += factor * b.m * dy`

`b.v.x -= factor * a.m * dx`

`b.v.y -= factor * a.m * dy`

This set also has some simplifications, particularly with the `dx`

and `dy`

calculations. Since they were already needed in two places, using them to replace the radius made the whole thing more correct for very little cost.

In fact, it now looks decent even when we let the balls slip inside each other, with one caveat: A negative `factor`

means the balls are already moving away from each other; they may already have bounced, but haven’t yet separated. Don’t adjust the velocities in that case, or things can get jittery. Granted, this rule of thumb lets really fast collisions pass by undetected, but that doesn’t look nearly as bad.

## Bouncing Math

It should normally be impossible for three-dimensional balls of different sizes to be forced into a two-dimensional plane together. Nevertheless, I’ve discovered a situation where I want to calculate their trajectories when they bounce off each other.

Fortunately, I get to assume perfect elasticity. A bit more realism would be nice, but letting the input energy cancel out completely turns a quadratic equation linear. (The other solution is where the balls miss each other completely.) Because it’s been long enough since my mechanics course, I incidentally proved conservation of momentum along the way, but came up with the following:

`factor = 2*((a.x-b.x)*((b.m**2)*b.v.x - (a.m**2)*a.v.x) + (a.y-b.y)*((b.m**2)*b.v.y - (a.m**2)*a.v.y))/((a.m**3 + b.m**3) * (a.r + b.r)**2)`

`a.v.x += factor * a.m * (a.x-b.x)`

`a.v.y += factor * a.m * (a.y-b.y)`

`b.v.x -= factor * b.m * (a.x-b.x)`

`b.v.y -= factor * b.m * (a.y-b.y)`

Yes, it’s messy. Messy enough that I distrusted it at first (mass cubed, really?), despite the units working out properly, until I derived it again through a different method. It’s much simpler in the coordinates of the impact force, but gets just as complicated on translation back to cartesian coordinates.

Forgive my combination of programming syntax with physics-length variable names, for I have been delving into both worlds at once. They should all be obvious from the context, except that `.r`

is for the radius of the ball; that bit is a shortcut for calculating the absolute distance between their centers, which itself is part of a proxy for the impact force angle. The whole thing only really applies at the precise moment of collision, assuming the balls are perfectly rigid. Fortunately, I’m defining the universe in which this happens, so I get to make the rules.

Now I have to decide whether to use this, or whether a vastly simpler calculation will produce results that look good enough.

Update: My math was wrong. Use the new equations instead.

## Toothpaste Woes

My children have a very messy relationship with toothpaste. For starters, they tend to drop the caps down the drain, because I haven’t bothered to re-install the plug that wasn’t quite seated properly when we moved in. At one point, my parents noticed; they gave us this fun fish cap, which promptly got clogged, taken off, and left on the floor.

Every six months, the dentist gives each child a small tube that they try to claim as their very own, until they all decide to try someone else’s and everyone forgets whose is whose. As the caps get left off, tubes get confiscated until there’s only one or two left.

The flip-top caps are even worse; back when they tried to close the lid, they ended up squishing excess toothpaste out the edges, which dried in place. Now there’s as much dried goop around the opening as cap itself, and the tube is empty enough that they need me to squish some out every night.

For some reason, I’m the expert in the family at squeezing the last few bits of toothpaste out of the tube. I can last almost a week on a tube that my spouse has deemed empty. It may or may not be related to the fact that I’m the only bottom-squeezer in the house.

On a semi-related note, the skeptic in the family has researched fluoridated water after hearing a few too many panicky poison warnings. It turns out that the only problems are in areas of naturally high fluoridation, none of which are in the U.S. Fluoridated water doesn’t seem to rebuild teeth from the inside as originally thought, but gives you more resistance to cavities by fluoridating your saliva. It’s also true that Europe has mostly decided against fluoridating their water, because you get just as much benefit from a combination of fluoride toothpaste and socialized dental care.

Sadly, we’re likely to have as many panicked citizens riled up against the latter as against fluoride in the water supply.

## Just Kiss The Girl

Today’s rant comes not from my spouse, but from another close relative who noticed a disturbing trend in romantic comedies. The girl starts rambling about how it will never work out, the boy kisses her, her eyes go wide, and she starts kissing back.

As an adult, it’s easy to see the context here. Perhaps her heart really wants him, but her mind thinks it’s a bad idea. Perhaps she’s afraid of commitment. Perhaps her previous relationships have gone really wrong. In each case, what the boy really means by kissing her quiet is reassurance that no matter what happens, it’ll be fun while it lasts, so relax and enjoy the ride.

The problem is that not all viewers are that smart. Certain teenage boys in particular can get the message that when a girl says no, you should kiss her until she says yes. That’s the kind of attitude that leads to date rape, potentially destroying lives.

The sad part is that my parents had pointed to certain chick flicks as models of how a gentleman should behave toward women. At this point, I can’t even tell you which ones, much less what kind of behavior they taught.

So be very, very careful what you let your children watch. The worst shows may well have no violence or profanity at all, but normal people doing normal things that translate poorly to everyday situations.