Added references to SDL_IPHONE_MAX_GFORCE ... that way this value can change without altering the demos behavior. More understandable too. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 00:40:47 +0000
branchgsoc2008_iphone
changeset 2424ecc18fbfdec3
parent 2423 74d25e48d54d
child 2425 491958a6c881
Added references to SDL_IPHONE_MAX_GFORCE ... that way this value can change without altering the demos behavior. More understandable too.
XCodeiPhoneOS/Demos/src/accelerometer.c
     1.1 --- a/XCodeiPhoneOS/Demos/src/accelerometer.c	Fri Aug 15 00:38:49 2008 +0000
     1.2 +++ b/XCodeiPhoneOS/Demos/src/accelerometer.c	Fri Aug 15 00:40:47 2008 +0000
     1.3 @@ -11,7 +11,12 @@
     1.4  #define MILLESECONDS_PER_FRAME 16	/* about 60 frames per second */
     1.5  #define DAMPING 0.5f;				/* after bouncing off a wall, damping coefficient determines final speed */
     1.6  #define FRICTION 0.0008f			/* coefficient of acceleration that opposes direction of motion */
     1.7 -#define GRAVITY_CONSTANT 0.02f		/* how sensitive the ship is to the accelerometer */
     1.8 +#define GRAVITY_CONSTANT 0.004f		/* how sensitive the ship is to the accelerometer */
     1.9 +
    1.10 +/*	If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
    1.11 +#ifndef SDL_IPHONE_MAX_GFORCE
    1.12 +#define SDL_IPHONE_MAX_GFORCE 5.0f
    1.13 +#endif
    1.14  
    1.15  static SDL_Joystick *accelerometer; /* used for controlling the ship */
    1.16  
    1.17 @@ -26,20 +31,25 @@
    1.18  
    1.19  void render(void) {
    1.20  		
    1.21 +	
    1.22  	/* get joystick (accelerometer) axis values and normalize them */
    1.23 -	float amax = (float)(0x7FFF); /* largest Sint16 number */
    1.24 -	float ax = SDL_JoystickGetAxis(accelerometer, 0) / amax; 
    1.25 -	float ay = -SDL_JoystickGetAxis(accelerometer, 1) / amax; 
    1.26 +	float ax = SDL_JoystickGetAxis(accelerometer, 0); 
    1.27 +	float ay = -SDL_JoystickGetAxis(accelerometer, 1); 
    1.28  		
    1.29  	/* ship screen constraints */
    1.30  	Uint32 minx = 0.0f;
    1.31  	Uint32 maxx = SCREEN_WIDTH - ship.rect.w;
    1.32  	Uint32 miny = 0.0f;
    1.33  	Uint32 maxy = SCREEN_HEIGHT - ship.rect.h;	
    1.34 -		
    1.35 -	/* update velocity from accelerometer */
    1.36 -	ship.vx += ax * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
    1.37 -	ship.vy += ay * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
    1.38 +
    1.39 +#define SINT16_MAX ((float)(0x7FFF))
    1.40 +
    1.41 +	/* update velocity from accelerometer
    1.42 +	   the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between 
    1.43 +	   SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
    1.44 +	*/
    1.45 +	ship.vx += ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
    1.46 +	ship.vy += ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
    1.47  	
    1.48  	float speed = sqrt(ship.vx * ship.vx + ship.vy * ship.vy);
    1.49