how to bound area in accelerometer?
<p>i am working on an application which is based on accelerometer.</p>

<p>i have a ball which moves on the basis of acclerometer.</p>

<p>i have tried to bound the area of ball in about 320 by 480.</p>

<p>but when the ball reaches one of the corner, the ball gets out of bounded area and gets disappear, and comes again from anywhere.</p>

<p>i have no idea what i am doing wrong.</p>

<p>here is the code i m using</p>

<pre><code>static inline BOOL bounce(float* val, float* delta, float min, float max) {
#define FLIP(val, c) (c - (val - c))
    if (*val &lt; min || *val &gt; max) {
        *delta = -(*delta * 0.00);
        float loc = *val &lt; min ? min : max;
        *val = FLIP(*val, loc);
        return YES;
    } 
    return NO;
}


-(void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration;
{

    #define CAP(val, max) (val &lt; -max ? -max : (val &gt; 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) &gt; kAccelerationThreshold || fabsf(acceleration.y) &gt; kAccelerationThreshold || fabsf(acceleration.z) &gt; kAccelerationThreshold)
    {
    }
    if (bounce(&amp;location.x, &amp;delta.x,20, self.view.bounds.size.width - 25 ) || bounce(&amp;location.y, &amp;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;
}
</code></pre>

<p>waiitng for reply
regards</p>
