PLANET
------
by Peter Cameron
from ZX Computing, October/November 1983
An object lesson in geometry with this games program for the ZX Spectrum
written for us by Peter Cameron of Oxford.
This program began as an experiment in drawing three- dimensional shapes from
different vantage points — a branch of geometry once of practical importance
but now (along with much other geometric manipulations) on the decline in
educational fashion. My aim was to compute and display successive views of an
object as seen by a moving observer. The final result also involves a little
Newtonian mechanics and graph theory.
In a fix?
Let us take a fixed reference point, O, in space as the origin, and locate
any point relative to O by three co-ordinates (x,y,z) representing its
distance, East, North (the two-dimensional map reference) and in the vertical
direction. The screen should be made to display a photograph of an object
taken by an observer at co-ordinates (l,m,n). (To simplify matters, we assume
that the vertical direction in space is taken to be at the top of the screen.)
Some algebraic manipulation showed that if O is plotted at the centre of the
screen (pixel 128,88) then the point with co-ordinates (1,0,0) should be
plotted at 128+x1, 88+y1; co-ordinates (0,1,0) at 128+x2, 88+y2; and
coordinates (0,0,1) at 128+x3, 88+y3. It should be noted that the
displacements, x1,...y3, are given by the formulae in lines 140 to 160 of the
program. These formulae include provision for the fact that the object
appears smaller when viewed from further away.
With this information, an arbitary point can be plotted — the point with
co-ordinates (u,v,w) would appear at pixel, 128+u*x1 +v*x2+w*x3,
88+u*yl+v*y2+w*y3.
Luck Of the draw
I decided to draw an octahedron. This figure has an interesting symmetrical
shape which is extremely easy to draw. The corners, or vertices, are at the
points (1,0,0), (0,1,0), (0,0,1) and their negatives. Line 260 of the program
does the actual drawing.
You will see that the effect of line 260 is to draw the figure "without
taking the pencil from the paper" — that is, only using the PLOT instruction
once. That this is possible for the octahedron was another reason for my
choice of this solid. Such a figure is called Eulerian, after the
mathematician Leonhard Euler, who convinced the citizens of Konigsberg that
it was impossible to walk around their city crossing each of its seven
bridges exactly once. Euler showed that, in order to draw a figure without
lifting your pencil. it is necessary and sufficient that each vertex lies on
an even number of edges (in which case, you can start anywhere but you must
finish at the start point), or exactly two don’t lie on the edges (when you
must start at one of the edges and finish on another). It isn’t necessary to
demonstrate this — a routine for drawing an octahedron can be found by trial
and error. (Try it yourself with a pencil and paper before making a study of
line 260.)
The mechanics of the program
The position co-ordinates of the observer are (l,m,n) and the velocity is
(p,q,r). This means that, in the time interval between one observation and
another, l, m and n will change by amounts proportional to p, q and r
respectively. This is implemented in line 400.
At first I kept the velocity fixed. However, if the observer is piloting a
spaceship orbiting a planet, the velocity will also change. According to the
laws of gravity discovered by Isaac Newton, the change of velocity will be in
a direction towards the planet; that is, proportional to (—l,—m,—n)/SQR b,
where b is computed in line 140. The proportionality factor is the inverse
square of the distance from the planet; ie. 1/b.
At this point, I realised that there was a "game" element possible within my
program, so I added controls, which change the velocity either towards or
away from the planet. Unfortunately, the ship's engines are not very reliable
and the actual change is somewhat variable but that is what games are all
about, aren’t they? The velocity change (due to the combined effect of
gravity and engines) is implemented in line 410 and 420.
What’s the score?
The object of the game is to take survey photographs of the planet; these
should be as large as possible so you’ll have to get in real close. The score
is determined by the size of the largest image to appear on the screen, with
added bonuses for a very close approach. However, have I told you about the
risk of crashing?
Since the controls only work slowly, there is a more drastic way of avoiding
an imminent crash — a jump through hyperspace. Newton’s equations didn’t
exactly cover this possibility (and science fiction writers are still arguing
about it), so I took it upon myself to assume that re-entry from hyperspace
is random, both in its position and velocity. Hyperspace jumps are expensive
but spectactular (especially if you re-materialise inside a solid body!).
All change
This program is written for the 16K Spectrum. However, with a few changes it
could be RUN on the 48K machine. The changes are as follows:
Line 10 - Replace 31743 with 64511.
Line 60 - Replace 31744 with 64512.
Line 80 - Replace 124 (the third entry in the DATA list) with 252.
Line 550 - Replace 31744 with 64512.
Line 555 - Replace 31756 with 64524.
Line 600 - Replace 31768 with 64536.
Should you feel ambitious once you have studied the program, why not try and
define your own shapes and manipulate them in three dimensions.