comparison XCodeiPhoneOS/Demos/src/accelerometer.c @ 2424:ecc18fbfdec3 gsoc2008_iphone

Added references to SDL_IPHONE_MAX_GFORCE ... that way this value can change without altering the demos behavior. More understandable too.
author Holmes Futrell <hfutrell@umail.ucsb.edu>
date Fri, 15 Aug 2008 00:40:47 +0000
parents 7fbcfc1574dc
children
comparison
equal deleted inserted replaced
2423:74d25e48d54d 2424:ecc18fbfdec3
9 #include "common.h" 9 #include "common.h"
10 10
11 #define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */ 11 #define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
12 #define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */ 12 #define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
13 #define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */ 13 #define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */
14 #define GRAVITY_CONSTANT 0.02f /* how sensitive the ship is to the accelerometer */ 14 #define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
15
16 /* If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
17 #ifndef SDL_IPHONE_MAX_GFORCE
18 #define SDL_IPHONE_MAX_GFORCE 5.0f
19 #endif
15 20
16 static SDL_Joystick *accelerometer; /* used for controlling the ship */ 21 static SDL_Joystick *accelerometer; /* used for controlling the ship */
17 22
18 static struct { 23 static struct {
19 float x, y; /* position of ship */ 24 float x, y; /* position of ship */
24 static SDL_TextureID shipID=0; /* texture for spaceship */ 29 static SDL_TextureID shipID=0; /* texture for spaceship */
25 static SDL_TextureID spaceID=0; /* texture for space (background */ 30 static SDL_TextureID spaceID=0; /* texture for space (background */
26 31
27 void render(void) { 32 void render(void) {
28 33
34
29 /* get joystick (accelerometer) axis values and normalize them */ 35 /* get joystick (accelerometer) axis values and normalize them */
30 float amax = (float)(0x7FFF); /* largest Sint16 number */ 36 float ax = SDL_JoystickGetAxis(accelerometer, 0);
31 float ax = SDL_JoystickGetAxis(accelerometer, 0) / amax; 37 float ay = -SDL_JoystickGetAxis(accelerometer, 1);
32 float ay = -SDL_JoystickGetAxis(accelerometer, 1) / amax;
33 38
34 /* ship screen constraints */ 39 /* ship screen constraints */
35 Uint32 minx = 0.0f; 40 Uint32 minx = 0.0f;
36 Uint32 maxx = SCREEN_WIDTH - ship.rect.w; 41 Uint32 maxx = SCREEN_WIDTH - ship.rect.w;
37 Uint32 miny = 0.0f; 42 Uint32 miny = 0.0f;
38 Uint32 maxy = SCREEN_HEIGHT - ship.rect.h; 43 Uint32 maxy = SCREEN_HEIGHT - ship.rect.h;
39 44
40 /* update velocity from accelerometer */ 45 #define SINT16_MAX ((float)(0x7FFF))
41 ship.vx += ax * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME; 46
42 ship.vy += ay * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME; 47 /* update velocity from accelerometer
48 the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
49 SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
50 */
51 ship.vx += ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
52 ship.vy += ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
43 53
44 float speed = sqrt(ship.vx * ship.vx + ship.vy * ship.vy); 54 float speed = sqrt(ship.vx * ship.vx + ship.vy * ship.vy);
45 55
46 if (speed > 0) { 56 if (speed > 0) {
47 /* compensate for friction */ 57 /* compensate for friction */