const float kGravity = 0.2f; -(void) update:(ccTime)delta { velocity.y -= kGravity; self.position = CGPointMake(position_.x + velocity.x, position_.y + velocity.y); }
it given the information about how to develop the iphone application and games.......
Monday, January 31, 2011
Applying gravity without a physics engine
Tuesday, January 4, 2011
Objective-C Math
Objective-C is the object oriented version of C. C contains a library of standard Mathematical functions.
Objective-C does support C-based mathematical functions. All C are written for long variable types. Variables are declared long. Functions must be declared long float or double and any contacts must be written in floating point forms. eg. 1 should be written as 1.0.
int i; // long int
double x; // long float
fabs() – Find the absolute value or unsigned value in parentheses
results = fabs(x); // fabs(-1.5) = 1.5
ceil() – Find the ceiling integer; integer just above the value in parentheses; similar to rounding up
i = ceil(x); // ceil(2.5) = 3
floor() – find the integer that is below the floating point value in parentheses
i = floor(x); // floor(2.2) = 2
exp() – find the expoential value
pow() – raise a number to the power
result = pow(x,y); // raise x to the power of y
result = power(4,2); // raise 4 to the square = 16
sqrt() – find the square root of a number
i = sqrt(x); // sqrt(9) = 3
Objective-C does support C-based mathematical functions. All C are written for long variable types. Variables are declared long. Functions must be declared long float or double and any contacts must be written in floating point forms. eg. 1 should be written as 1.0.
int i; // long int
double x; // long float
fabs() – Find the absolute value or unsigned value in parentheses
results = fabs(x); // fabs(-1.5) = 1.5
ceil() – Find the ceiling integer; integer just above the value in parentheses; similar to rounding up
i = ceil(x); // ceil(2.5) = 3
floor() – find the integer that is below the floating point value in parentheses
i = floor(x); // floor(2.2) = 2
exp() – find the expoential value
pow() – raise a number to the power
result = pow(x,y); // raise x to the power of y
result = power(4,2); // raise 4 to the square = 16
sqrt() – find the square root of a number
i = sqrt(x); // sqrt(9) = 3
Monday, January 3, 2011
Detecting a Slow Shake Vs. Fast Shake
Personally I have tried out the - (void)motionEnded...method and found the response time to be rather slow.
What I am trying to do is detect a fast shake vs. a slow shake and write a message. The project compiles, and if you shake below the 1.5 acceleration threshold I setup the labels says "Slow" as it is suppose to. Speed up the shake and it shows "Fast." THE PROBLEM is if I slow the shake back down the label doesn't change. What I have is as follows:
- (void)accelerometer : (UIAccelerometer *)accelerometer
didAccelerate : (UIAcceleration *)acceleration {
static NSInteger shakeCount = 0;
static NSDate *shakeStart;
NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval: 2.0f sinceDate:shakeStart];
if ([now compare:checkDate]== NSOrderedDescending || shakeStart == nil) {
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
[now release];
[checkDate release];
// Shake is too fast
if (acceleration.y > AccelerationThreshold) {
shakeCount++;
if (shakeCount > 4) {
myLabel.text = [NSString stringWithFormat:@"To Fast %i", shakeCount];
NSLog(@"To Fast");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
// shakeCheckComplete = YES;
}
}
// Shake is slow
if (acceleration.y < AccelerationThreshold && acceleration.y > 0.5) {
shakeCount++;
if (shakeCount > 5) {
myLabel.text = [NSString stringWithFormat:@"Slow %i", shakeCount];
NSLog(@"Slow");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}
}
What I am trying to do is detect a fast shake vs. a slow shake and write a message. The project compiles, and if you shake below the 1.5 acceleration threshold I setup the labels says "Slow" as it is suppose to. Speed up the shake and it shows "Fast." THE PROBLEM is if I slow the shake back down the label doesn't change. What I have is as follows:
- (void)accelerometer : (UIAccelerometer *)accelerometer
didAccelerate : (UIAcceleration *)acceleration {
static NSInteger shakeCount = 0;
static NSDate *shakeStart;
NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval: 2.0f sinceDate:shakeStart];
if ([now compare:checkDate]== NSOrderedDescending || shakeStart == nil) {
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
[now release];
[checkDate release];
// Shake is too fast
if (acceleration.y > AccelerationThreshold) {
shakeCount++;
if (shakeCount > 4) {
myLabel.text = [NSString stringWithFormat:@"To Fast %i", shakeCount];
NSLog(@"To Fast");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
// shakeCheckComplete = YES;
}
}
// Shake is slow
if (acceleration.y < AccelerationThreshold && acceleration.y > 0.5) {
shakeCount++;
if (shakeCount > 5) {
myLabel.text = [NSString stringWithFormat:@"Slow %i", shakeCount];
NSLog(@"Slow");
shakeCount = 0;
shakeStart == nil;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}
}
How do I detect when someone shakes an iPhone?
static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
double
deltaX = fabs(last.x - current.x),
deltaY = fabs(last.y - current.y),
deltaZ = fabs(last.z - current.z);
return
(deltaX > threshold && deltaY > threshold) ||
(deltaX > threshold && deltaZ > threshold) ||
(deltaY > threshold && deltaZ > threshold);
}
@interface L0AppDelegate : NSObject <UIApplicationDelegate> {
BOOL histeresisExcited;
UIAcceleration* lastAcceleration;
}
@property(retain) UIAcceleration* lastAcceleration;
@end
@implementation L0AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[UIAccelerometer sharedAccelerometer].delegate = self;
}
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if (self.lastAcceleration) {
if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
histeresisExcited = YES;
/* SHAKE DETECTED. DO HERE WHAT YOU WANT. */
} else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
histeresisExcited = NO;
}
}
self.lastAcceleration = acceleration;
}
// and proper @synthesize and -dealloc boilerplate code
@end
The histeresis prevents the shake event from triggering multiple times until the user stops the shake.
Subscribe to:
Posts (Atom)