IPhone graphics (3)

Source: Internet
Author: User

Last time
We created a simple iPhone application that made a ball bounce around
The screen. This time we are going to extend the last example to use
Accelerometer to create a gravity influence on the ball. So as you tilt
The device the ball will roll in the direction of the tilt.

The first thing we need to do is add a way to influence the ball's motion. I 've addedInfluence
Method toVector2d
Class. AlsoGravityview
Class getsGravity
Variable that isVector2d
Class instance.

123456789101112131415161718192021222324252627282930
- (void)influence:(Vector2D*)influenceVector Max:(CGFloat)max;{    [endPoint addVector:influenceVector];    length = sqrt(endPoint.X * endPoint.X + endPoint.Y * endPoint.Y);    CGFloat newAngle = atan2(-endPoint.Y, endPoint.X) * ONEEIGHTYOVERPI;    if(newAngle < 0)    {        newAngle += 360;    }     if(abs(newAngle - influenceVector.angle) >= 90)    {        length *= 0.96;    }    else    {        length *= 1.03;    }     if(length > max)    {        length = max;    }    else if(length < 0.0)    {        length = 0.0;    }     [self setAngle:newAngle];}

This method is passed another vector and a max value. The passed inInfluencevector
Is added to the ball'sEndpoint
.
If the ball is currently moving away from the influence vector then it
Is slowed down slightly by multiplying the vector length by 0.96. If
Ball is currently moving toward the influence vector then it is sped up
Slightly by multiplying the vector length by 1.03. The maximum length
Of the vector is capped at the max value passed into the method.
Vector is not allowed to have a negative length.

I 've also added a method to the vector2d class to scale the length of the vector.

123456789101112
- (void)scaleLength:(CGFloat)factor;{    length *= factor;    if(length < 0.05)    {        length = 0.0;    }     double radians = angle * PIOVERONEEIGHTY;    endPoint.X = length * cos(radians); // could speed these up with a lookup table    endPoint.Y = -(length * sin(radians));}

This method simply multiplies the vector length by the scale factor.
When the length of the vector becomes very small it is set to zero.

In order to receive accelerometer events we make the graphicsview class implement the uiaccelerometerdelegate prototype.

1
@interface GraphicsView : UIView <UIAccelerometerDelegate>

In the graphicsview initwithframe method we need to set up the accelerometer.

12345
    accelX = 0.0;    accelY = 0.0;     [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];    [[UIAccelerometer sharedAccelerometer] setDelegate:self];

We are initializing the accelerometer x and y values to zero, setting
The update interval so that we receive updates 60 times per second and
Setting self as the delegate.

With the accelerometer set up the accelerometer method of our graphicsview object instance will be called 60 times per second.

1234567
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{    accelX = (acceleration.x * kAccelFilt) + (accelX * (1.0 - kAccelFilt));    accelY = (-acceleration.y * kAccelFilt) + (accelY * (1.0 - kAccelFilt));     [gravity setEndPointX:accelX*2 Y:accelY*2];}

We are using a filter to isolate the current device position. Each
Time the method is called we set the Gravity Vector's end point to two
Times the accelerometer x and y values. We don't need the Z axis
Accelerometer for this example. Note that we are using negative Y
Account for coordinate system differences.

The final piece of the puzzle is to call the influence method of our ball's vector in the timer tick method.

1234567891011
- (void)tick{    // Apply gravity influence    [ball.vector influence:gravity Max:8.0];     // Update the balls position    [ball move:self.bounds];     // Tell the view that it needs to re-draw itself    [self setNeedsDisplay];}

That's all there is to it. prett simple eh?

Here are the new source files:
Refreshing ing iPhone graphics Part 3 Source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.