Smalltalk: Twirl Them Particles

Listing 4. Class ViewScreen.


class:				ViewScreen
superclass:			Object
instance variables:	matrixStack pane pen canvas leader

class methods

examples
example1
	"ViewScreen example1"
	^self new run: 500

instance methods

running
run: numberOfParticles
	| switchTicks cameraToScreen count typeCount |

	switchTicks := 200.
	pane := GraphPane openWindow: 'Particles'.
	canvas := Bitmap screenExtent: pane extent.
	pen := canvas pen.
	pen fill: Color yellow; direction: 0; foreColor: ClrRed.

	cameraToScreen:=  Transformation4D
		simplePerspectiveForExtent: pane extent. 
	cameraToScreen preTranslateBy: (
		Point3D x: 0.0 y: 0.0 z: -2.0).
	cameraToScreen preRotateByDegrees: (
		Point3D x: 0.0 y: -90.0 z: 10.0).

	matrixStack := OrderedCollection with: cameraToScreen.
	leader := Leader new
		maximumParticleSpeed: 0.01;
		numberOfParticles: numberOfParticles;
		reset.

	count := 0. typeCount := 1.
	[true] whileTrue: [
		count := count + 1.
		count > switchTicks ifTrue: [
			count := 0.
			typeCount := typeCount \\ 3 + 1.
			leader shape: (#(sphere pearl tornado) at: typeCount)].
		leader tick; drawOn: self. self flip]

stack manipulation
top
	^matrixStack last
push
	matrixStack addLast: self top
pop
	matrixStack removeLast
multiply: aTransformation
	matrixStack 
		at: matrixStack size
		put: (aTransformation * self top)
rotateBy: aPoint
	"Rotate by x around x-axis, by y around y-axis, etc."
	matrixStack
		at: matrixStack size
		put: (self top preRotateByDegrees: aPoint)
drawing
drawPointAt: position
	"Actually draw 4 pixels to make it more visible."
	| where |
	where := position * self top.
	Pen
		place: where x@where y;
		go: 2;
		place: where x@where y+1;
		go: 2

flip
	pane pen
		copyBitmap: canvas
		from: canvas boundingBox
		at: 0@0.
	canvas pen fill: Color yellow.

About the Author

Wilf LaLonde is director of Learning Dimensions, Inc., a company focused on 3D gaming development.