Smalltalk: Twirl Them Particles

Listing 2. Class Particle.

class:				Particle
superclass:			Object 
instance variables:	position velocity

instance methods

position: aPoint3D
	position := aPoint3D
velocity: aPoint3D
	velocity := aPoint3D

reset: maximumSpeed
	position := Point3D zero.
	velocity := Point3D

		x: (Time randomFloatFrom: -1.0 to: 1.0)
		y: (Time randomFloatFrom: -1.0 to: 1.0)
		z: (Time randomFloatFrom: -1.0 to: 1.0).
	velocity := velocity normalized *
		(Time randomFloatFrom: 0.0 to: 1.0) *

tick: bottle
	"Move according to velocity but try to stay inside the bottle."
	self position: self position + self velocity.
	(bottle isInside: self position) ifFalse: [
		"Reverse directions."
		self velocity: self velocity negated.
		self position: self position + (self velocity * 2)].
	(bottle isInside: self position) ifFalse: [
		"Reversing failed: Bottle must be contracting."
		self position: self position * 0.9].

drawUsing: aViewScreen
	aViewScreen drawPointAt: position

About the Author

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