Richard Ellingworth / Mbed 2 deprecated RobotRic

Dependencies:   25LCxxx_SPI CommonTypes Gameduino mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BulletVelocities.h Source File

BulletVelocities.h

00001 /*
00002  * SOURCE FILE : BulletVelocities.h
00003  *
00004  * Converts from set of joystick inputs to horizontal and vertical velocities.
00005  *
00006  */
00007 
00008 #ifndef BulletVelocitiesIncluded
00009   
00010   #define BulletVelocitiesIncluded
00011 
00012   #include "Int16Pair.h"
00013   
00014   class BulletVelocities {
00015   
00016   public :
00017 
00018     /***************/
00019     /* CONSTRUCTOR */
00020     /***************/
00021     // Pass velocity for a horizontally moving bullet in h.
00022     // Pass velocity for a vertically moving bullet in v.
00023     BulletVelocities( Int16 h, Int16 v ) {
00024       // Calculate velocities used on the diagonal.
00025       // This is really doing hd = h * cos( 45 degrees ) and
00026       // vd = v * sin( 45 degrees ).
00027       Int16 hd = (Int16)floor( (double)h * 0.707 + 0.5 );
00028       Int16 vd = (Int16)floor( (double)v * 0.707 + 0.5 );
00029       // Initialise the table of velocities.
00030       Int16Pair *ptr = pairs;
00031       // No joystick contacts closed. Index 0.
00032       ptr->X = 0;
00033       ptr->Y = 0;
00034       ptr++;
00035       // Joystick up. Index 1
00036       ptr->X = 0;
00037       ptr->Y = -v;
00038       ptr++;
00039       // Joystick down. Index 2.
00040       ptr->X = 0;
00041       ptr->Y = v;
00042       ptr++;
00043       // Joystick up and down (impossible). Index 3.
00044       ptr->X = 0;
00045       ptr->Y = v;
00046       ptr++;
00047       // Joystick left. Index 4.
00048       ptr->X = -h;
00049       ptr->Y = 0;
00050       ptr++;
00051       // Joystick left and up. Index 5.
00052       ptr->X = -hd;
00053       ptr->Y = -vd;
00054       ptr++;
00055       // Joystick left and down. Index 6.
00056       ptr->X = -hd;
00057       ptr->Y = vd;
00058       ptr++;
00059       // Joystick up, down and left (impossible). Index 7.
00060       ptr->X = 0;
00061       ptr->Y = v;
00062       ptr++;
00063       // Joystick right. Index 8.
00064       ptr->X = h;
00065       ptr->Y = 0;
00066       ptr++;
00067       // Joystick right and up. Index 9.
00068       ptr->X = hd;
00069       ptr->Y = -vd;
00070       ptr++;
00071       // Joystick right and down. Index 10.
00072       ptr->X = hd;
00073       ptr->Y = vd;
00074       ptr++;
00075       // Joystick up, down and right (impossible). Index 11.
00076       ptr->X = 0;
00077       ptr->Y = v;
00078       ptr++;
00079       // Joystick left and right (impossible). Index 12.
00080       ptr->X = 0;
00081       ptr->Y = v;
00082       ptr++;
00083       // Joystick up, left and right (impossible). Index 13.
00084       ptr->X = 0;
00085       ptr->Y = v;
00086       ptr++;
00087       // Joystick down, left and right (impossible) Index 14.
00088       ptr->X = 0;
00089       ptr->Y = v;
00090       ptr++;
00091       // Joystick up, down, left and right (impossible). Index 15.
00092       ptr->X = 0;
00093       ptr->Y = v;
00094       ptr++;
00095     }
00096 
00097     /*******************************************************/
00098     /* GET VELOCITIES FOR A COMBINATION OF JOYSTICK INPUTS */
00099     /*******************************************************/
00100     // Pass a map containing joystick inputs where a set bit indicates a closed contact.
00101     // Bits must be ordered as follows :
00102     // Bit 0 = Up.
00103     // Bit 1 = Down.
00104     // Bit 2 = Left.
00105     // Bit 3 = Right.
00106     // Remaining bits are ignored.
00107     const Int16Pair *GetVelocities( UInt8 joyMap ) const {
00108       return pairs + ( joyMap & 0x0F );
00109     }
00110     
00111   private :
00112 
00113     // Horizontal and vertical velocities for each combination of joystick inputs.    
00114     Int16Pair pairs[ 16 ];
00115     
00116   };
00117   
00118 #endif
00119 
00120 /* END of BulletVelocities.h */
00121