i am working on an application which is based on accelerometer.
i have a ball which moves on the basis of acclerometer.
i have tried to bound the area of ball in about 320 by 480.
but when the ball reaches one of the corner, the ball gets out of bounded area and gets dis开发者_如何学运维appear, and comes again from anywhere.
i have no idea what i am doing wrong.
here is the code i m using
static inline BOOL bounce(float* val, float* delta, float min, float max) {
#define FLIP(val, c) (c - (val - c))
if (*val < min || *val > max) {
*delta = -(*delta * 0.00);
float loc = *val < min ? min : max;
*val = FLIP(*val, loc);
return YES;
}
return NO;
}
-(void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration;
{
#define CAP(val, max) (val < -max ? -max : (val > max ? max : val))
CGPoint delta = CGPointMake(CAP(self.delta.x + acceleration.x, 1.5),CAP(self.delta.y - acceleration.y, 1.5));
CGPoint location = CGPointMake(self.location.x + delta.x, self.location.y + delta.y);
if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
{
}
if (bounce(&location.x, &delta.x,20, self.view.bounds.size.width - 25 ) || bounce(&location.y, &delta.y,20, self.view.bounds.size.height - 20))
{//25, 20
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
self.delta = delta;
self.location = location;
self.ballView.center = self.location;
}
waiitng for reply regards
Solved.
taken 2 int values and bounded x between 0 and 320 and y in between 0 and 480.
thanks anyways
精彩评论