# PDP-1 Spacewar! program internals

Sat Nov 8 07:47:26 CST 2008

```Jim Leonard wrote:
>Al Kossow wrote:
>> Russell:  I suspect most of them figured out the "basic trick", but I'm not
>sure.
>>
>> Kossow: What "basic trick" were you thinking of?
>>
>> Russell: That you could do everything based on the spaceship unit vector.
>
>I'm afraid I don't understand what he's getting at.  Can someone explain
>the "basic trick" in a bit more detail?

I'll take a stab.  I just wrote a "spacewar" the other day as an, ahem,
unit test of a new lcd display driver and event (keypad) driver.  It helped
me find some bugs! :-)

The ship moves in 2d space with an orientation of 0-359 degrees.  In
order to "move" the ship, typically done once per game loop, you need to
add a delta-x and delta-y to the current x,y location.  The
delta-x,delta-y represents the ship's velocity, it's moving at, say
2-pixels-per-frame (like 20 mph would be to a car).

To calculate the velocity you need to add acceleration to the delta.  Often
this is "thrust".  The thrust has to be effected by the direction of the nozzle,
which is generally fixed as the opposite of the ship's nose.

I just used sin & cos because I have a computer (PXA320) which is many
many orders of magnitude faster.  But I suspect instead they had a table
of "unit vectors" based on the direction.  Essentially it's a sin & cos
table indexed by 0-359 which produces fractional numbers from -1 to 1.

On a pdp-1 I would guess they would use fixed point math, but I've never
looked at the source code.  The last time I wrote one was in 1980 on
a pdp-11 using fortran.  I think I used sin & cos there also but they could
easily have been implemented using a lookup table for all I know.  Or maybe
I used a lookup table.  I don't remember.

(wish I'd saved that source code; apparently a few years after I left the
dept head had to confiscate all the floppies with the game on it as there was some
sort of rampant competition going on between all the grad students and nothing
was getting done :-)

Anyway, if you know the direction of the item you can figure out the
"unit vector" (unit meaning it's normalized to 1) and then you can scale
that anyway you like to get the proper motion.

as always, apologies if I'm off base.