Magnetic Morphs

Four years pass since coding Magnetic Ribbon. All of a sudden I am wondering if my ribbon code will work in Squeak. page

The numerical stuff does except for transpose which is now spelt more consistently transposed. The graphics is another story. I start looking for a polygon fill and find it in class Canvas.

Actually it is in the refinement BallonCanvas. Balloon is Squeak's advanced graphics package. This problem isn't worth an advanced solution so I start looking for Plan B. Plan B is Morphic.

Morphs are shapes that are easily manipulated with assorted mouse clicks. Once I learned these, especially Alt-Left-Click, the Morphic secret weapon, I could play with and learn a few dozen of the hundred morphs already present. I used AtomMorph as a guiding example.

Like it, my new morph would refine ElipseMorph so that rendering would be taken care of. I used plain old ellipses as the magnets that my new morph would respond to each time Morphic asked it to step. Here is the step code.

step | magnets acceleration delta | super step. mags := owner submorphs select: [:each | each class == EllipseMorph]. acceleration := magnets inject: 0@0 into: [:sum :each | delta := each position - position. sum + (delta / (delta r squared + 2))]. acceleration := velocity * -0.0001 + acceleration. velocity := velocity + (acceleration * 10) . position := position + velocity. self position: position rounded

I added a few ellipses to my desktop and then one of my new active variation. (I called it a SwingMorph because that's what the dangling arm of the toy did.) It moved. In fact it moved too much. If it swung directly over a magnet it would pick up a kick from delta r approaching zero and go flying off the screen.

I added a few pixels of ever-present spacing, much like that in the toy itself. (The magnets never touched.) The morph still accumulated energy. I added a little bit of velocity dependent resistance (like air resistance) to make it stable, though still chaotic, in the long term.

I used floating point for all my numbers. These feed deep into the Morphic system which is used to working with integers. All worked fine except for one thing: the refresh region bounding box calculation was sometimes off by one. This meant that my morph would leave little trails of incorrectly refreshed pixel debris. Yuck. I decided to keep my own precision position and send a rounded copy of that into Morphic. This cleaned up the display but interfered with manual placement of the morph. Hmmm. I'd better back this last change out and fix Morphic.

I kind of missed the debris trail's arcs and swoops. Looking a little further into Morphic I discovered that every morph could draw. See Turtle Graphics .

I told my morph to lower its pen. Bingo. A beautiful drawing of the trajectory. I let it run all night and wrote it up in the morning. expand

Here is the source for the whole morph.

pages/magnetic-morphs