home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xblockbuster / part03 / deflection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  2.9 KB  |  120 lines

  1. /*
  2.  * File:       deflection.c
  3.  * Author:     Eric Van Gestel
  4.  *
  5.  * For:                xblockbuster
  6.  *
  7.  * No changes required for X11 support - MSW
  8.  *
  9.  * Implementation:
  10.  *     Simple horizontal and vertical deflection only changes the quadrant,
  11.  *     reversing motion along the y and x axes respectively.
  12.  *     Deflection from the convex pallet is a function of excentricity,
  13.  *     as well as of incoming angle (given a sliding of the edge effect).
  14.  */
  15.  
  16. #include "xblockbuster.h"
  17.  
  18. void
  19. brick_deflection( hit, ball )
  20.     register int    hit;    /* enumeration { HORIZONTAL, VERTICAL } */
  21.     register struct Ball *ball;
  22. {
  23.     if ( hit == HORIZONTAL ) {
  24.         switch ( ball->quadrant ) {
  25.         case NE:
  26.             ball->quadrant = SE;
  27.             break;
  28.         case NW:
  29.             ball->quadrant = SW;
  30.             break;
  31.         case SW:
  32.             ball->quadrant = NW;
  33.             break;
  34.         case SE:
  35.             ball->quadrant = NE;
  36.         }
  37.         ball->y_speed = -( ball->y_speed );
  38.     } else {        /* VERTICAL */
  39.         switch ( ball->quadrant ) {
  40.         case NE:
  41.             ball->quadrant = NW;
  42.             break;
  43.         case NW:
  44.             ball->quadrant = NE;
  45.             break;
  46.         case SW:
  47.             ball->quadrant = SE;
  48.             break;
  49.         case SE:
  50.             ball->quadrant = SW;
  51.         };
  52.         ball->x_speed = -( ball->x_speed );
  53.     }
  54. }
  55.  
  56. void
  57. pallet_deflection( ball )
  58.     register struct Ball *ball;
  59. {
  60.     double          excentricity, true_angle;
  61.  
  62.     switch ( ball->quadrant ) {
  63.     case SW:
  64.         excentricity = ball->x - pallet_x;
  65.         break;
  66.     case SE:
  67.         excentricity = pallet_x - ball->x;
  68.         break;
  69.     default:
  70.         return;        /* bouncing up from below */
  71.     };
  72.     ball->angle -= 2 * atan2( excentricity, pallet_height );
  73.  
  74.     if ( ball->angle > -M_PI_4 )
  75.         ball->quadrant = ( ball->quadrant == SW ) ? NW
  76.              /* SE */ : NE;
  77.     else {            /* rebound */
  78.         ball->angle = -M_PI_2 - ball->angle;
  79.         ball->quadrant = ( ball->quadrant == SW ) ? NE
  80.              /* SE */ : NW;
  81.     };
  82.  
  83.     /*
  84.      * avoid downward slide offs as well as infinite horizontal bounce
  85.      * loops
  86.      */
  87.     if ( ball->angle > NEAR_HORIZONTAL )
  88.         ball->angle = NEAR_HORIZONTAL;
  89.  
  90.     true_angle = ( ball->quadrant == NE ) ? M_PI_4 - ball->angle
  91.          /* NW */ : M_PI_3_4 + ball->angle;
  92.     ball->x_speed = ball->speed * cos( true_angle );
  93.     ball->y_speed = ball->speed * -sin( true_angle );
  94. }
  95.  
  96. #ifdef DEBUG_PDEFL
  97. main(  )
  98. {
  99.     pallet_xI = 0;
  100.     pallet_x = 0;
  101.     printf( "\npallet_length & pallet_height:  " );
  102.     scanf( "%d%d", &pallet_lengthI, &pallet_heightI );
  103.     pallet_length = ( double ) pallet_lengthI;
  104.     pallet_height = ( double ) pallet_heightI;
  105.     if ( pallet_lengthI > 0 && pallet_heightI > pallet_lengthI )
  106.         for ( ;; ) {
  107.             printf( "\nquadrant, in angle & excentricity:  " );
  108.             scanf( "%d%lf%lf", &ball1.quadrant, &ball1.angle, &ball1.x );
  109.             if ( ball1.quadrant < 1 || ball1.quadrant > 4 ||
  110.                  ball1.angle < -45 || ball1.angle > 45 ||
  111.             ball1.x < -pallet_length || ball1.x > pallet_length )
  112.                 break;
  113.             ball1.angle = ( ball1.angle / 45 ) * M_PI_4;
  114.             pallet_deflection( &ball1 );
  115.             ball1.angle = ( ball1.angle * 45 ) / M_PI_4;
  116.             printf( "==> quadrant: %d  angle: %f\n", ball1.quadrant, ball1.angle );
  117.         };
  118. }
  119. #endif DEBUG_PDEFL
  120.