forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch21.txt
139 lines (104 loc) · 3.96 KB
/
ch21.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
chapter: Accelerometers
==================
#define PI 3.14159
float find2dAngle(void){
//LOCAL VARIABLES
float alpha,
double ax, ay;
//POLL ACCELEROMETER FOR ACCELERATIONS, API SPECIFIC
ax = getXacceleration();
ay = getYacceleration();
//FIND ANGLE
alpha = atan2(ay,ax);
if (alpha >= 0){
alpha = alpha * (180/PI);
else {
alpha = alpha * (-180/PI) + 180;
}
return alpha;
}
====================================
- (void)viewDidLoad
{
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = kPollingRate;
[super viewDidLoad];
}
====================================
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration{
[(SpriteView *)self.view setAcceleration:acceleration];
[(SpriteView *)self.view draw];
}
====================================
-(id)initWithCoder:(NSCoder *)coder {
if((self = [super initWithCoder:coder])){
self.sprite = [UIImage imageNamed:@"sprite.png"];
self.currentPos = CGPointMake((self.bounds.size.width / 2.0f) +
(sprite.size.width / 2.0f), (self.bounds.size.height /2.0f)+(sprite.size.height /2.0f));
xVelocity = 0.0f;
yVelcoity = 0.0f;
convertX = self.bounds.size.width / kWorldWidth;
convertY = self.bounds.size.height / kWorldHeight;
}
return self;
}
====================================
- (void)setCurrentPos:(CGPoint)newPos {
prevPos = currentPos;
currentPos = newPos;
if(currentPos.x <0){
currentPos.x = 0;
xVelocity = 0.0f;
}
if(currentPos.y <0){
currentPos.y = 0;
yVelcoity = 0.0f;
}
if(currentPos.x > self.bounds.size.width - sprite.size.width){
currentPos.x = self.bounds.size.width - sprite.size.width;
xVelocity = 0.0f;
}
if(currentPos.y > self.bounds.size.height - sprite.size.height){
currentPos.y = self.bounds.size.height - sprite.size.height;
yVelocity = 0.0f;
}
CGRect curSpriteRect = CGRectMake(currentPos.x, currentPos.y,
currentPos.x+sprite.size.width, currentPos.y+sprite.size.height);
CGRect prevSpriteRect = CGRectMake(prevPos.x, prevPos.y,
prevPos.x+sprite.size.width, currentPos.y+sprite.size.height);
[self setNeedsDisplayInRect:CGRectUnion(curSpriteRect, prevSpriteRect)];
}
====================================
- (void)draw {
static NSDate *lastUpdateTime;
if (lastUpdateTime != nil) {
NSTimeInterval secondsSinceUpdate = -([lastUpdateTime
timeIntervalSinceNow]); //calculates interval in seconds from last update
//Calculate displacement
CGFloat deltaX = xVelocity * secondsSinceUpdate +
((acceleration.x*g*secondsSinceUpdate*secondsSinceUpdate)/2); // METERS
CGFloat deltaY = yVelocity * secondsSinceUpdate +
((acceleration.y*g*secondsSinceUpdate*secondsSinceUpdate)/2); // METERS
//Converts from meters to pixels based on defined World size
deltaX = deltaX * convertX;
deltaY = deltaY * convertY;
//Calculate new velocity at new current position
xVelocity = xVelocity + acceleration.x * g * secondsSinceUpdate; //assumes
acceleration was constant over last update interval
yVelocity = yVelocity - (acceleration.y * g * secondsSinceUpdate); //assumes
acceleration was constant over last update interval
//Mutate currentPos which will update screen
self.currentPos = CGPointMake(self.currentPos.x + deltaX,
self.currentPos.y + deltaY);
}
[lastUpdateTime release];
lastUpdateTime = [[NSDate alloc] init];
}
====================================
CGFloat deltaX = xVelocity * secondsSinceUpdate +
((acceleration.x*g*secondsSinceUpdate*secondsSinceUpdate)/2); // METERS
====================================
xVelocity = xVelocity + acceleration.x * g * secondsSinceUpdate;
==================