preface

In the last article, we understood the background of the game “Snails can’t Sleep” and what we were trying to accomplish. In order to make a good transition to actual game development, let’s take advantage of some of the physics simulation capabilities of UIKit framework itself and finish some interesting demos to help us understand some of the concepts that are often encountered in game development.

UIKit physics simulation

self.animator = UIDynamicAnimator(referenceView: view)
let redView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
redView.backgroundColor = .red
view.addSubview(redView)
let gravity = UIGravityBehavior(items: [redView])
self.animator?.addBehavior(gravity)
Copy the code

let collision = UICollisionBehavior(items: [redView])
collision.translatesReferenceBoundsIntoBoundary = true
self.animator?.addBehavior(collision)
Copy the code

By default, the collision boundary is the same as the container view boundary. If we want to indent the collision boundary to the container boundary, we can do this:

collision.setTranslatesReferenceBoundsIntoBoundary(with: UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50))
Copy the code

let anchor = CGPoint(x: self.view.bounds.width / 2, y: 100)
let attachment = UIAttachmentBehavior(item: redView, attachedToAnchor: anchor)
self.animator?.addBehavior(attachment)
Copy the code

Terms related to physics engines

I have been struggling with this part of the game for a while, and I think it is still necessary to explain it to you. Back when I first got into the physics engine, I was in a strange field and needed to fill in a lot of basic concepts. In order to get it right, I would like to share with you a few terms that I will often come into contact with in the subsequent game development process.

vector

This is supposed to be high school math. In games, vectors are often used to describe position and speed. Because the content of the vector is too much, one by one, I feel like I can open a tutorial class, so I only list a part of the key content tips that may be used in the subsequent game, and you can expand the details yourself when you have time.

Length of vector

The moving vector

Add vectors

Rotating vector

Vector scaling

The dot product

Thanks to Swift, all types can be “extended” to add additional methods to implement a set of vector computations. The Swift custom operator is powerful because it saves a lot of code duplication, but there are some problems. Unless our custom operators can be understood by someone else reading the code (I can’t guarantee that), defining obscure symbols as operators makes the code less readable.

The world

The “physics engine” cannot be used directly in a game, it has to use an attachment to make it work. The “world” is the environment in which the physics engine works, the “universe” that houses all the objects in the game, and if an object is outside the world, it cannot be managed by the physics engine and nothing can affect it.

The quality of

General concepts. The more massive an object is, the more weight it has, and the faster it will fall.

speed

General concepts. In the SpriteKit physics world, speed is made up of two parts: horizontal speed and vertical speed, known as “x speed” and “Y speed.”

object

It is the object itself in the physical world. A body that can be acted upon by external forces and can collide with other bodies has mass and speed. Physics is divided into “dynamic objects” and “static objects”, “static objects” means that they are not acted on by external forces and do not move.

force

A force applied causes an object to move. In this game, when we launch a ball, a force is applied to the ball, and when we release the ball, it gains an acceleration and moves away from its initial position. Gravity and force are not the same thing. Gravity is another force that affects all objects in the current gravitational environment. How much force must be applied to make the object move depends on the mass of the object itself. Under the same force, apply the force to the heavier object and the lighter object, and the lighter object will move relatively fast.

The frictional force

General concepts. When one body comes into contact with another, the friction between them causes the speed of motion to decrease. In the real world, friction between objects is transferred by creating heat, but in SpriteKit, kinetic energy can only be dissipated out of thin air. To fully simulate heat transfer, we need to write a whole new logic. We can determine the coefficient of friction of one object, and the lower the coefficient of friction, the smoother the surface, and the less kinetic energy they lose when they come into contact with other objects.

The collision body

The collider defines the shape of the object, and the texture of the object itself does not determine the actual size of the rigid body. For example, when calculating a person’s waist circumference, it must be measured close to the body. What we measure is the waist circumference of the “rigid body” itself, not the waist circumference with clothes on. SpriteKit provides squares, rectangles, circles, and polygons by default, and you can customize the Path Settings for different colliders.

Edge collider

The edge collider is a subset of colliders. It is composed of one or more thin lines, and the edge collision body is mostly used to build “walls” and other obstacles. An object with an edge collider never moves; it is a “static object”.

The collision

When two bodies/rigid bodies collide, we call it a collision. A collision is a behavior, and a collider is an object. When a collision occurs, we can get information about both sides of the colliding body from the “collision”, such as what objects are colliding and the location of the collision.

The connection

A connection is a relationship between two objects. The most common is the PIN connection, where one object is able to rotate but cannot leave another object at a relative point. The other is the Spring connection, where an object can move away from another object, but when it moves within a certain distance of the other object, it bounces back, as demonstrated in the last demo above.

conclusion

In this article, we do some small physics experiments through some physics simulation methods in UIKit, and describe related concepts for the basic support of subsequent game development.

In the next article, we’ll introduce various SpriteKit concepts and put them into practice.

  • Game explanation;
  • Familiar with 2D programming (ING);
  • Rigid body collision and detection;
  • Ball launch and block elimination;
  • Game logic improved.

GitHub address: github.com/windstormey…