[stella] Polar to Cartesian Co-Ordinate Converter

Subject: [stella] Polar to Cartesian Co-Ordinate Converter
From: "Roger Williams" <mer02@xxxxxxxxxxxxx>
Date: Sun, 21 Oct 2001 16:09:14 -0700
This is the proof of concept demo for my polar to cartesian
co-ordinate converter, which does the operation in 93 to
114 machine cycles.  The converter can reliably be called
upon to miss exactly one WSYNC if used during the screen
draw, if it is called before cycle 30 of a scanline.  Even
so it is not as highly optimized as some Stella code and
I'm sure it can be improved even more.  But it is very
usable as presented here.
This has been tested in Z26, Stella, and on a real 2600.
The demo is actually pretty fun to play with and utterly
unlike anything I have ever seen Stella do.  The two
player sprites are set up off-screen (a la Combat, but
with the random positioning algorithm) and do various
circular things under your control.  The X and Y
co-ordinate axes are displayed for reference, using the
playfield and ball respectively.
The left controller controls both players.  Hold the fire
button down to control P1.  Left-Right adjusts theta or
theta velocity; up-down adjusts radius.  Radius control
wraps from 128 to 0 but stops decreasing at 0.

The right controller moves the origin so you can examine
the behavior of the algorithm at extreme ranges.
The player difficulty switches control the mode...
P1 easy=direct theta control; hard=orbital velocity
P0 easy=both orbit origin; hard=P1 orbits P0
The converter is so fast because it uses exclusively
8-bit values.  Theta (the angle) runs 0-255 around the
circle, giving a resolution of about 1.4 degrees.  R can
go out to about 160, and X and Y to +127/-128 signed.
Better angular resolution could be had with the same
algorithm up to 1/1024 circle at the cost of some extra
cycles in the conversion process and a longer trig table.
Three tables are used totaling a bit under 700 bytes.

The secret to this algorithm is logarithms (now there's
a word I bet you never thought you'd read in this list).
The source is fully documented with theory and I have
placed it in the public domain.  Let me know if you
would like the BASIC program to generate the log, exp,
and trig tables for different maximum values.  Close-in
accuracy can be improved by sacrificing maximum distance,
and vice-versa.  The calculations aren't exact but are
quite good for game type display work.
(BTW you can also use the log and exp tables for general
purpose scaling multiplication and division.  They are
surprisingly accurate, with few errors greater than 1 out
to results of 45 or so, and errors that increase gradually
as the outer limits are reached.)
I think I'm going to open a beer and watch this thing
whirl around awhile before I get to work on the cartesian
to polar mirror algorithm.  It won't execute quite as
fast but it uses only one additional table.  The concept
is proven in BASIC but converting it to 6502 .asm is
always fun...
--Roger Williams

Attachment: p2cdemo.asm
Description: Binary data

Attachment: p2cdemo.bin
Description: Binary data

Current Thread