// Spherical Spacewar -- Spacewar on the surface of a sphere // Projected Mercator, Winkel Tripel, sinusoidal, // two hemispheres flat, or the full sphere flat and two-valued, // or various other projections. // Momentum conserved in shooting and collisions. // Player creates mass of shots at the time of shooting, out of thin air. // Shot hits increase the mass and radius of the hit player. // Player mass is directly proportional to player radius. // Player shape is a spherical shell cap (the zone of a spherical cap), // or a filled circle in rectilinear mode. // Program has rectilinear mode (not on a sphere). // Rectilinear mode is wrapping flat space or walled flat space. // Program has option for a physics read-out of energy and momentum // at bottom of screen. // Al J , 6-15-06 to 8-15-06... Commands Help and Status Screen [key]: effect or status z x c a s d: player 2 turn left, forward, turn right, left, back, right. 1 2 3 4 5 6 (on numpad): player 1 movement keys, like player 2. p or P: select type of projection: rectangular. t or T: trails or erasure of previous player or shot positions, 3 1. 5 or %: decrease or increase main loop delay, 1ms. o or O: draw player shapes projected like the sphere, or not projected: 1. ` or -: clear the screen. ~: automatic screen clearing, 0. 7: reset player 2. 8: reset player 1. =: reset board (players to starting coordinates and a few other resets). ': curvilinear trajectory math method, 0, centripetal step. l or L: spin mechanics in player-player and player-shot collisions, 1 1. u: playfield friction on saucer travel, 1. I: wall-player friction spin, 1. (spacebar): pause. U: enable more realistic playfield surface friction, 0. (escape): quit program or exit this screen. ?: enter or exit this screen. /: show help on more commands. lon,lat 1.571, -1.047 look angle 2.36 travel angle 2.36 vel 0.0000 lon,lat -1.571, 1.047 look angle 5.54 travel angle 5.54 vel 0.0000 plr linKE +0.00000 ((())) totalKE orbital L xyz +0.00000 +0.0000 +0.0000 +0.0000 Commands Help and Status Screen 2 [key]: effect or status 9: player 1 fire. detonate oldest shot if 3 shots are already out. 8: detonate player 1 shot nearest to player 1. e: player 2 fire. detonate oldest shot if 3 shots are already out. w: detonate player 2 shot nearest to player 2. k or K: toggle player-player and player-shot collisions, 1 1. ; or :: animated transition from one projection to another. {: rectilinear mode, rectangle space, not a sphere, 1. }: mode-independent selection of spherical or rectilinear space: rectilinear. 0: black hole (with two interpretations of gravity in spherical space), 0. 6: no grid lines except around the border of the projection, 1. g or G: modify grid color. grid color is 60. ^: grid refresh delay, 105/18 s. v or V: player 2 colors, 47 111. b or B: player 1 colors, 79 63. h or H: restore or save player coordinates. ]: try to adjust magnitude of movement steps in balance with program speed, 0. [: kinetic energy and momentum read-out at bottom of screen, 1. \: frames-per-second read-out. ? or /: enter or exit this screen. plr linKE plyr Px plyr Py total Lz00 +0.00000 +0.00000 +0.00000 +0.0000 (space) totalKE totalPx totalPy +0.00000 +0.00000 +0.00000 ****Notes, draft This program, Spherical Spacewar, was born of a curiosity to see what Spacewar, a class of video games with ships that turn and thrust in a two-dimensional wrapping rectangle space, would be like on the surface of a sphere. Spherical shell space Spherical cap zone integrals The shape of the player saucer (when not in rectilinear mode) is a spherical shell cap or the surface cap of a sphere, also known as the zone of a spherical cap. Moments of inertia for spin about an axis normal to the sphere of radius r at the surface center of the cap of sphere central angle radius r1*r m*r^2*( cos(r1)^3/3.0 - cos(r1) + 2.0/3.0 )/( 1-cos(r1) ) for rotation along the sphere surface about the center of the sphere m*r^2*( 4.0/3.0 - cos(r1) - cos(r1)^3/3.0 )/( 2.0*( 1-cos(r1) ) ) Center of mass distance from center of sphere of radius r r/2.0 * (1+cos(r1)) Surface friction torque against spin If the cap is not travelling, surface friction against spin is u*g*m*r*( r1 - 0.5*sin(2.0*r1) )/( 2.0*( 1-cos(r1) ) ) . Numerical integration is used for the complication of friction against simultaneous spin and travel. The velocity unit vector for friction at each area element cannot be analytically integrated over two-dimensions. Numerical integration over ~100 points gives a good approximation without too much of an impact on program execution speed. Avoiding the integral, one simple approximation for dividing the force of friction between friction against spin and friction against travel is to apportion the total friction by average spin velocity (2/3 w r) or travel velocity (v) over their sum (v + 2/3 w r); this loosely fits the results of the integral, applying friction mostly against the dominant velocity. (2/3 w r) is the average spin velocity on a flat disc of radius r. On a spherical cap zone of central angle radius r1*r on a sphere of radius r, the average spin velocity is (r1 - 1/2 sin(2 r1))/(2 - 2 cos r1) * w r1 r. (Show derivations; render equations in TeX.) Great circle trajectories Methods centripetal step parametric great circle solution great circle course run solution great circle trajectory change of bearing math methods atan2(v dot lat-hat,v dot lon-hat) Clairaut's formula -- breaks at apex lat great circle course angle function -- shows error over small moves Elastic collisions In flat space In spherical shell space Spin mechanics in collisions The force transverse to the collision line in player-player and player-wall collisions is a Coulomb force of friction, with a user-selectable coefficient of friction of 0.0 or 0.1. The normal force for the force of friction is from the exchange of momentum in the collision line. Orbital angular momentum In the physics read-out (in spherical mode), the angular momentum vector shown is for orbital momentum, and not total, where orbital refers to the motion of an object around the sphere, and the object's spin around its own center of mass is not included. Total angular momentum is not shown because in this simulation it is not conserved; the spherical shell environment redirects the spin of the objects traveling within it to be always normal to the surface, without consequence. Unlimited massless fuel As of July 26, 2006, the program does not track fuel usage for player thrusts. Each straight thrust adds a fixed velocity increment in the direction the player is looking (or reverse, left, or right of that for the other straight thrust keys) to the player's travel velocity. It would not be a difficult change to the program to track fuel mass possession and usage. If the unspent fuel mass was assumed to be stored evenly distributed over the player saucer, or all at the center, or all at the rim, this change to the program would not require new integral calculus for moments of inertia. It does however raise questions of refueling possibilities, such as continuous refueling from ambient space, or running out and needing to reset. There would also be the question of whether a larger saucer would spend more fuel per thrust command, or spend the same fuel for a smaller change of velocity. Having the program track fuel use for turning thrusts raises the questions of allowing the player to turn-thrust up to a high rate of spin and allowing the player for possible fuel economy or combat value to disable post-turn-command automatic counter-thrusting to zero spin. As of July 26, 2006, a turning thrust command immediately spins the player to a constant turning rate, and maintains that rate against play-field surface friction if any while the key is held. A more recent turn command supersedes the older one, and if when the key for the more recent one is released a prior key is still down its turn command resumes. After a collision which alters the player's spin by more than 1%, currently active turn commands for that player are suspended, so the effect of the collision on spin may be observed. Momentum conservation Discrete time-stepping unit dt non-unit dt use of several smaller dt steps when a player velocity is high non-infinitesimal dt error back-out of pre-collision movement step object penetration forward moves after collision bounce after back-out of pre-collision penetration Barrel-less shot firing As of July 26, 2006, shots fire from the surface center of a player saucer and do not experience the effect of traveling through a barrel such as there would be if the player were turning. Playfield friction against player spin and travel U: enable more realistic playfield surface friction, 1. In the program, 'U' enables the numerical integration of surface friction, for spherical or rectilinear mode. Otherwise friction against travel velocity and spin velocity are each treated as if the other velocity were zero. Mass and radius units old pixel units and sphere central angle units Black hole option The black hole exerts and experiences a Newtonian force of gravity between itself and the player saucers and any shots in play. In rectilinear mode, it can be seen to cause stable elliptical orbits. In spherical mode, it offers two selectable interpretations of Newtonian gravity: one uses the shortest distance along the sphere surface as the inverse square distance, in the direction of the starting course angle from one object to the other, and the other interpretation uses the gravity force equation in R3 and truncates the force component normal to the sphere at each object. The black hole is displayed with inward animation of pixels between two concentric radii. The outer is at the escape horizon for six times the player thrust command velocity increment. The inner is at a fanciful Shwarzchild radius using a wavespeed of 14.6 times the player thrust command velocity increment (the program does not use modern relativity, and there is no universal speed limit, other than the maximum floating point value.) Any player or shot whose center passes within the black hole inner radius is removed from play. There is no tidal phase locking effect, or other tidal effects, because objects are radially symmetric and not deformable. Although the Black Hole does absorb the spin angular momentum of absorbed players, the alteration of the Schwarzchild radius by rotation is not modeled. Questions A discussion of entertainment software and attention to science and, as here, physical simulation Projections Sinusoidal Winkel Tripel Rectangular Mercator Two hemispheres flat Two hemispheres flat, one from under Flat Perspective Azimuthal equidistant Alber's Equal-Area Conic and linear combinations of those Player draw methods There are three methods of drawing the player saucers selectable within the program. Method zero projects the position of a saucer but does not use the projection function for drawing the saucer's extended shape, which may be useful for program performance on slower machines. Method one (default) sweeps over the saucer in radius and angle, projecting each point. Method two draws only the border of the saucer, as a many-sided polygon with each vertex projected. The in-game coloration of the player saucer uses darker shades in proportion to the square of radial distance from the surface center of the saucer. Also, the entire saucer is shaded darker when it is known to be projected upside-down (that is, when it is seen from underneath as opposed to the conventional from above) at its surface center. For the linear combinations of two projections available with ';' or ':' projections morphing, the program may not know that part of the projection is upside-down, as properly testing for that was deemed an unwarranted computational cost. In any case, when the player saucer is projected upside down, left and right turning are apparently reversed. Player erase methods There are four methods of erasing the previous position of a player saucer selectable within the program. Method zero does not erase the previous position of a player saucer. Method one redraws the saucer in black. Method two redraws in black the trailing edge of the saucer, based on its velocity. Method three (default) draws a black many-sided polygon around the outside of the saucer. Program execution timing 5 or %: decrease or increase main loop delay, 1ms. \: frames-per-second read-out. The framerate display will never show a reading less than 18 frames per second. It is 18 times the number of main loop executions between one machine clock tick and the next, a period of about 1/18.2 seconds, with the clock read on each pass through the loop. CLOCKS_PER_SEC = 18.2 ]: try to adjust magnitude of movement steps in balance with program speed, 0. When ']' moves-timing is not enabled, and it is not by default, player turn and thrust rates will be of increased or decreased magnitude relative to the simulation as main loop execution speed decreases or increases. Player input Straight thrust key commands repeat twice per clock tick. Turn command angular velocity is 2PI/29.333/clock tick. Possible future game program options Statistics report of shots and hits on self and hits on opponent and destructions by black hole, per player. Expenditure of fuel for thrusts, and fuel mass. Computer-controlled player 3. A bar on the field to kick around, indestructible except by a black hole. Asteroids: circles / spherical cap zones, possibly of irregular outline, that can be decomposed by shots into multiple smaller asteroids, which vanish from the simulation when transitioning to below some threshold size. Billiards: balls that can roll and slip on the surface; for compatibility with the existing shapes, they would be on a sphere (or a plane, in rectilinear mode) one billiard ball radius below the main surface. Ultra-black-holes, so powerful that they don't exert a gravitational force but can serve as billiard pockets. Source code The program source code was written to be compiled with Borland Turbo C++ 3.0. Other than the double-slash until-end-of-line comment form, no C++ extensions to the C language were used. The program is written to use the 320 by 200, 8 bits per pixel VGA graphics mode. Keyboard input is through a custom interrupt keyboard handler, so that player control keys may be read in parallel, as much as the keyboard hardware allows. The C convention of all-lowercase variable and function identifiers and all-uppercase preprocessor defines was followed. Unrequested program termination may be the result of an inverse trig. domain error or division by zero. As of 7/31/06 these are mostly checked for and avoided. Machine performance The program was developed on a computer with a 1.8 GHz AMD Athlon 64 microprocessor, to be run under DOS with VGA graphics. There is room in the source code to make the program run better on slower machines. Some calculations are executed redundantly. In some cases, removing redundant calculations would impair the clear readability of equations in the source code. In other cases, there are quantities which are recalculated in different places, and the costs of recalculation could be avoided through the use of more memory. Windows performance When running the program under Windows, performance can be choppy at first. Holding down a key (such as 'shift' or 'enter') for a few seconds will exercise the program's interrupt keyboard input handler, and on this installation clears up choppy performance for good, unless the program is set to player draw method 0 and main loop delay 0ms, which set the program to run very fast, and inconsistent speed under Windows will be persistent. ... ****Acknowledgments... Spacewar http://en.wikipedia.org/wiki/Spacewar Alex Russell x86 DOS asm/C interrupt keyboard handler http://www3.telus.net/alexander_russell/course/introduction.htm Aviation Formulary, great circle navigation formulas http://williams.best.vwh.net/avform.htm#Crs http://www.physics.oregonstate.edu/paradigms/Publications/GreatCircles.html Wikipedia, various math and physics http://en.wikipedia.org/wiki/ Eric Weisstein's World of Physics http://scienceworld.wolfram.com/physics/ Mathworld, various math http://mathworld.wolfram.com/SphericalCoordinates.html The Math and Physics of Billiards http://archive.ncsa.uiuc.edu/Classes/MATH198/townsend/math.html **** This document is hosted at https://cometary-dash.neocities.org/sphrspwa/sphrspwa.txt