Tuesday, January 24, 2012

Morphic and physics

Here's a nice serie of screencasts recorded by HwaJong Oh @daliot that shows how to create physical interaction between Morphic objects.
The series starts with a demo of the final application and then how to build it step by step.
  1. Introduction to MorphoPhysics
  2. Make a ball
  3. A better ball
  4. Add a rubber band
  5. Rubber band with force


Introduction to MorphoPhysics:


Gofer it
  squeaksource: 'DaliotsPlayground';
  package: 'ConfigurationOfDaliotsPlayground';
  load.

(Smalltalk at: #ConfigurationOfDaliotsPlayground) 
    project lastVersion load: 'MorphoPhysics'.




Make a ball:


Codes shown in workspaces:
"Before creating the BallMorph class"
b1 := EllipseMorph new.
b1 openInWorld.
b1 extent: 40@40.
b1 color: Color green.
b1 borderWidth: 0.
b1 center: (158@225).
"..."
b1 delete.

"After creating the BallMorph class"
b1 := BallMorph new.
b1 openInWorld.
b1 reset.
b1 startStepping.
b1 velocity: 1@0.
b1 velocity: 0@1.
b1 velocity: 0@ -1.
b1 velocity: 1@ -1.
b1 velocity: 20@0.
b1 velocity: -20@0.
b1 acceleratedBy: -20@0.
b1 mass: 40.
b1 applyForceBy: -1000@0.

b2 := BallMorph new.
b2 openInWorld.
b2 mass: 80.
b1 applyForceBy: -1000@0.
b2 applyForceBy: -1000@0.

Load final code of this video:
Gofer it
  squeaksource: 'Pharocasts';
  version: 'DemoMorphoPhysics-hjo.1'; 
  load.




A better ball:


Load final code of this video:
Gofer it
 squeaksource: 'Pharocasts';
 version: 'DemoMorphoPhysics-hjo.2';
 load.




Add a rubber band:


Codes shown in workspaces:
"before creating the RubberBandMorph class"
r := PolygonMorph new.
r openInWorld.
r makeOpen.
r beStraightSegments.
r setVertices: {80@180. 180@200}.
r borderColor: Color black.
r setVertices: {b1 center. b2 center}.
r delete.

"after creating the RubberBandMorph class"
r := RubberBandMorph new.
r openInWorld.
r ball1: b1.
r ball1.
r ball2: b2.
r ball2.
r connectTwoBalls.
r length.
r vectorFrom1To2
Transcript open.
r thickness.

Load final code of this video:
Gofer it
 squeaksource: 'Pharocasts';
 version: 'DemoMorphoPhysics-hjo.3';
 load.




Rubber band with force:


Codes shown in workspaces:
r stress.
r applyForceOppositeDirection: 100@0.
r unitVectorFrom1To2.
r applyForceOppositeDirection: (r stress * r unitVectorFrom1To2).
r applyForce.
r delete.

b1 delete.
b2 delete.

"A little more complex example"
b1 := BallMorph new.
b1 openInWorld.
b1 mass: 80.

b2 := BallMorph new.
b2 openInWorld.
b2 mass: 50.

b3 := BallMorph new.
b3 openInWorld.
b3 mass: 200.

r12 := RubberBandMorph new.
r12 ball1: b1.
r12 ball2: b2.
r12 openInWorld

r23 := RubberBandMorph new.
r23 ball1: b2.
r23 ball2: b3.
r23 openInWorld

r31 := RubberBandMorph new.
r31 ball1: b3.
r31 ball2: b1.
r31 openInWorld.
Load final code of this video:
Gofer it
 squeaksource: 'Pharocasts';
 version: 'DemoMorphoPhysics-hjo.4';
 load.

3 comments:

  1. Thanks HwaJong. That's an excellent introduction to Morphic programming!

    ReplyDelete
  2. This feels like the first time I've really understood physics. Thanks! :).

    ReplyDelete
  3. This was wonderful. Keep up the great work! :-)

    ReplyDelete