## [stella] bouncing ball

 Subject: [stella] bouncing ball From: Jeff Johnston Date: Sat, 6 Jul 2002 10:23:08 -0700 (MST)
I think I may have figure out an easy way to compute this without the
polar code (although that demo sure is cool :):

When a ball hits the paddle, it's hitting a round object, but we can
pretend that it is hitting a flat object.  The slope of the
flat line that the ball is hitting is going to be equal to the angle of
that point of the circle (angle 0-255).

My understanding of the result of a ball bouncing against a flat object is
that the ball's exit angle is reflected around the angle of the object it
hits.

Example:
Ball direction, 128 degrees (256 "degrees" per circle)
So the difference in angles is 144-128=16 degrees
144+16=160 degrees.
But the ball also needs to change direction "180" (128) degrees, so:
160+128=288=>32 degrees
The ball goes in at 128 degrees, comes back at 32 degrees.

To draw the ball traveling at a certain angle could be hard without the
polar code, except if we construct a primitive line algorithm (here is my
qbasic version for proof of concept).. none of the math involved is hard
for the 6502 to do:

SCREEN 13
FOR ANGLE=0 TO TO 255
GOSUB TRACE
NEXT ANGLE
DO:LOOP WHILE INKEY\$=""
SCREEN 0:WIDTH 80
END

TRACE:
DEVIATION=0:BALLX=100:BALLY=100
FOR A=1 TO 100 'in the real game there wouldn't be a set distance
'find the best angle for now (out of 0, 32, 64, 96, 128, 160, 192, 224)
BEST=ANGLE+DEVIATIO
BEST=INT(BEST/32) 'same as shifting right
'CINT would have been more accurate above, but that's harder
BEST=BEST*32 'same as shifting left
'update the deviation from our desired angle
DEVIATION=ANGLE+DEVIATION-BEST
'move ball
IF BEST>=32 AND BEST<=96 THEN BALLX=BALLX+1
IF BEST>=96 AND BEST<=160 THEN BALLY=BALLY-1
IF BEST>=160 AND BEST<=224 THEN BALLX=BALLX-1
IF BEST>=224 OR BEST<=32 THEN BALLY=BALLY+1
'draw ball
PSET (BALLX,BALLY)
NEXT A
RETURN

Now I'm going to put this in.. we'll see how it goes :)
calamari

----------------------------------------------------------------------------------------------
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/