IOS physical Engine

Source: Internet
Author: User

IOS physical Engine

Currently, the well-known 2D physical engines include Box2d and Chipmunk, which are cross-platform. However, Apple also encapsulates a physical engine. UIDynamic is a new technology introduced from iOS 7 and belongs to the UIKit framework. This allows developers to achieve cool physical simulation without using physical formulas. It is often used in game development. This article is mainly about playing some of the lighter features, not to mention the advanced usage of the frameworks in those games. After all, it's not long before I get started. It's easy to use it on a common application interface.

If you did not see this article in Dong Boran's blog, click to view the original article.

The main steps are three steps. 1. Create a physical simulator. Set the simulation scope 2. Create the corresponding physical simulation behavior, add the physical simulation element 3. Add the physical simulation behavior to the simulator to start simulation.

 

Create a physical simulator using the lazy Loading Method

-(UIDynamicAnimator *) animator {if (! _ Animator) {// create a physical simulator _ animator = [[UIDynamicAnimator alloc] initWithReferenceView: self. view];} return _ animator ;}

 

Simulate gravity behavior UIGravityBehavior

One attribute of gravity behavior is the acceleration of gravity. The greater the setting, the faster the growth speed. The default value is 1.

gravity.magnitude = 100;

 

Add elements to tell the simulator which elements can perform gravity

    [gravity addItem:self.sxView];

 

Simple Demonstration:

// Create a gravity behavior UIGravityBehavior * gravity = [[UIGravityBehavior alloc] init]; // The larger the magnvel, the faster the growth rate gravity. magn1_= 100; [gravity addItem: self. sxView]; // Add it to the simulator to start simulation [self. animator addBehavior: gravity];

 

You can see that the animation is only dropped. (Note that this is not a constant speed animation. It simulates the falling of an object at the acceleration of gravity)

 

Simulate collision behavior UICollisionBehavior

An element must be added to the collision action to tell the physical simulator which elements can be collided. For example

[collision addItem:self.sxView];

The second is to set the collision boundary. By default, there is a parameter that uses the screen as the boundary.

collision.translatesReferenceBoundsIntoBoundary = YES;

 

Combining collision behavior with gravity Behavior

// 1. create a gravity behavior UIGravityBehavior * gravity = [[UIGravityBehavior alloc] init]; // The larger the magnvel, the faster the growth rate gravity. magn1_= 2; [gravity addItem: self. sxView]; // 2. create a collision behavior: UICollisionBehavior * collision = [[UICollisionBehavior alloc] init]; [collision addItem: self. sxView]; [collision addItem: self. bigBlock]; [collision addItem: self. smallBlock]; // set the collision boundary collision. translatesreferencebounds=boundary = YES; // 3. start simulation [self. animator addBehavior: gravity]; [self. animator addBehavior: collision];

 

(In the figure, the two controls go up because they both add collision behavior, but do not add gravity behavior)

If you think the screen as a boundary is not good, you can set an edge by yourself.

[collision addBoundaryWithIdentifier:@"line2" fromPoint:CGPointMake(self.view.frame.size.width, 0) toPoint:CGPointMake(self.view.frame.size.width, 400)];

 

It can also be a castle path. (Note that the path here is not displayed. If you want to see it, draw a graph with the same boundary as you set in the view)

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:    CGRectMake(0,150, self.view.frame.size.width, self.view.frame.size.width)];    [collision addBoundaryWithIdentifier:@"circle" forPath:path];

Effect

 

Simulate capture behavior UISnapBehavior

The capture behavior must be given a point at the time of creation.

UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.sxView snapToPoint:point];

The capture behavior has a shockproof coefficient attribute. The larger the setting, the smaller the amplitude.

snap.damping = 1;

Because it can only be moved once by default. If you want to move it multiple times, you can clear the previous simulator before simulation.

[self.animator removeAllBehaviors];

 

Combined with the demo, where to move the cursor

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. obtain the touch object UITouch * touch = [touches anyObject]; // 2. obtain the touch point CGPoint point = [touch locationInView: self. view]; // 3. create capture behavior UISnapBehavior * snap = [[UISnapBehavior alloc] initWithItem: self. sxView snapToPoint: point]; // shockproof coefficient. The larger the damping is, the smaller the amplitude is. snap. damping = 1; // 4. clear the previous one and start [self. animator removeAllBehaviors]; [self. animator addBehavior: snap];}

 

 

There are also some examples that we will not demonstrate here. The header files are easy to understand.

UIGravityBehavior: gravity behavior UICollisionBehavior: collision behavior UISnapBehavior: capture behavior uipus?havior: Push behavior UIAttachmentBehavior: attach behavior UIDynamicItemBehavior: all physical simulation behaviors of Dynamic Element Behavior inherit from UIDynamicBehavior and all UIDynamicBehavior can be combined and used independently, some complex effects can be achieved. If you don't see this article in Dong Boran's blog Park, click to view the original article.

For example, gravity can also be set, the collision can monitor the whole collision process, and the animation is attached to the iMessage text message chat interface (up and down) similar to iOS8. If you are interested, you can study it on your own, these methods can be used to occasionally make animations such as "Red Packets" and "golden eggs" in applications.

Welcome.

Related Article

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.