Smalltalk: Twirl Them Particles

Listing 1. Class Bottle.


class:				Bottle
superclass:			Object
instance variables:	shape
comment:
	A containment bottle for the particle system. It is assumed to be a unit box with origin in the center. Since the particle leader moves in the -z direction, the top of each bottom is at z = -0.5 and the bottom at z=+0.5."

instance methods

get/set
shape
	^shape
shape: aSymbol
	shape := aSymbol

querying
isInside: aPoint
	^self
		perform: (self messageFor: self shape)
		with: aPoint

isInsideSphere: aPoint
	^aPoint squaredLength <= 0.25="">isInsidePearl: aPoint
	"The pearl's head is in the -ve z direction."
	| radius |
	radius :=
		(aPoint z < -0.5)="">ifTrue: [0.0] ifFalse: [
		(aPoint z < 0.0)="">ifTrue: [(0.5 - aPoint z abs) abs] ifFalse: [
		(aPoint z < 0.25)="">ifTrue: [self interpolateFrom: 0.5
			to: 0.25 using: aPoint z inRange: 0.0 to: 0.25] ifFalse: [
		(aPoint z < 0.5)="">ifTrue: [self interpolateFrom: 0.25
			to: 0.0 using: aPoint z inRange: 0.25 to: 0.5] ifFalse: [
		0.0]]]].
		^(aPoint x squared + aPoint y squared) <= radius="">squared
   
isInsideTornado: aPoint
	"The tornado's head is in the -ve z direction."
	| radius |
	radius :=
		(aPoint z < -0.5)="">ifTrue: [0.0] ifFalse: [
		(aPoint z < 0.5)="">ifTrue: [self interpolateFrom: 0.5 to: 0.0
			using: aPoint z inRange: -0.5 to: 0.5] ifFalse: [
		0.0]].
	^(aPoint x squared + aPoint y squared) <= radius="">squared

querying support
allTypes
	^#(sphere pearl tornado)
allMessages
	^#(isInsideSphere:
		isInsidePearl:
		isInsideTornado:)
messageFor: type
	| which |
	which := self allTypes indexOf: type.
	^self allMessages at: which

interpolating
interpolateFrom: a to: b using: x inRange: x1 to: x2
	^self interpolateFrom: a to: b usingFraction: (x-x1)/(x2-x1)
interpolateFrom: a to: b using: x
	^self interpolateFrom: a to: b usingFraction: (x-a)/(b-a)
interpolateFrom: a to: b usingFraction: fraction
	^a + ((b - a) * fraction)

About the Author

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