I finally had time to write a blog. After a long time, I didn't study SpriteKit. Today I installed Xcode5 DP2, and I was surprised to find that, when creating a project, you can see the SpriteKit Game option. After selecting it, You can directly create a SpriteKit project. It's really thoughtful for Apple to think about. It saves a lot of the previous steps, if you are a developer familiar with SpriteKit, you can directly use Sprite Game to create a project. If you are a beginner, it is recommended to create an empty project and follow the official documentation, step by step, it will naturally give you a better understanding of SpriteKit. In fact, directly using SpriteKit Game to create a project only helps you automatically generate a small part of the code. It is not difficult to repeat it by yourself. At least you need to know how it starts and operates. Since iOS7 is a beta version, Xcode5 is also a DP version, and the API documentation remains unchanged, it is estimated that more interesting things will be available for SpriteKit. After so long, the Code is a little unfamiliar. Today I re-read SpriteKit's Simulate Physics, that is, the physical system, and suddenly found a lot of new things, I used to look at it in a rough way. Today, I have a lot of things. SpriteKit is really powerful. in 2d, it is as good as possible. Let's just talk about it. Next we will learn the physical system of SpriteKit from the ground up. To complete the physical system settings of a game, you need to do the following: 1. Associate the SKPhysicsBody with a game object with physical properties, each node of the game object has the physicsbody attribute. 2. It sets the attributes of physicsbody to make the game objects display better game effects. 3. It sets the physical system of the entire game world, for example, gravity. Note, scene also has physical attributes. 4. You can apply force on physicsbody. 5. Define collision processing between objects. 6. Optimize the entire physical system. In fact, I personally think it is interesting to think about collision, there are new things in it, at least I have never seen them in other game engines, or maybe I have little experience, but I personally think SpriteKit's collision detection is really awesome. Physics body (pb) has three types: Moving, static, and edge, pb of movement can accept all external physical influences. Static pb has no speed and is not affected by force and collision. It is good to distinguish these two kinds of pb, ordinary game objects are usually petabytes of movement, while walls, floors, boundaries of game scenarios, and so on, can be static petabytes of edge, which is actually edge. You can understand it in this way, the free and rule-free pb can be defined by the boundary. It is often used as the boundary of the game scenario. It is also useful that the game objects are shaped, spriteKit provides several shapes for us, but in fact, in real games, our game objects are usually irregular graphics. At this time, if you want to add a petabyte with a similar shape to your game object, you can consider using edge, but this is not a common method, using edge to shape a game requires a lot of computing and resource consumption, which will make your game smoother. Therefore, we do not advocate this practice very much. The following code uses edge as the scene Boundary
- < li>
- < li>
- < li>
- < li>
- < li>
- < li>
The code is not hard to understand. The main function is bodyWithEdgeLoopFromRect:. Let's just say that. It is not difficult to create pb based on the specified shape. The specific code will not be pasted out. We suggest you go to the SKPhysicsBody api to learn the entire physical system. Let's talk about the attributes of several petabytes. The attributes of mass, that is, quality, are all known in terms of physical or non-physical properties. If there is a mass, of course there is a density, these two attributes will change with the change of the other party. The only thing that remains unchanged is the shape of the object. The whole thing we know is that we have learned physics, and mass equals density to multiply by area size. Friction attribute, friction, is the roughness of the object surface, the object moving on it will be affected by its friction, the force is also the linearDamping resistance and angularDamping angle force, is the rotation force, this is free from the environment in your game world. It is generally air resistance or water resistance, which is determined by your environment. Restitution attributes can be understood as potential energy. During a collision, it determines the force of the collision, that is, the strength of the effect. Dynamic attribute. Generally, the default value of dynamic pb is yes. After setting it to no, It is static pb. AffectedByGravity attribute. yes indicates that gravity is affected, and no indicates that gravity is not affected. AllowsRotation attribute, which determines whether an object can be selected. If it is set to no, rotation is frozen. About these attributes, it should be noted that, after we create pb, the area has been set to fixed and unchangeable, mass and density, therefore, the changes between mass and density affect each other. You can set mass to determine density based on your needs, or set density to determine mass based on actual conditions, the two have different effects, which involve physical knowledge. In addition, many attributes change. Based on the actual physical properties, SpriteKit recalculates each attribute value at each frame. You can use some functions to modify some attributes during update, in addition, when switching to different environments in different scenarios, modify some attributes during pross-post scene as needed to achieve the desired effect. As mentioned above, scene also has a physical nature. It also has a physicsWorld attribute, that is, the physical system in the entire game world. Here we will talk about the speed. The normal speed is 1, for example, when it is 0.5, the execution speed of pb will be halved. When it is 2, it will be doubled. Then, you may need to exert some force on your pb, so that it can be moved, there are two forces force and impulse, force is a continuous force, each frame must be set, so that the force can continue, impulse is a real-time force, that is to say, the force effect is another immediate effect, but it will not continue. In view of the difference between the two, the official document only focuses on force. Oh, the force is actually adding a vector. We only need to provide a point to let the object move towards that point. The force can be divided into three types based on the coordinate point. One is the force that allows the object to move in a straight line, A force that only allows an object to rotate is also applied to an object. At this time, SpriteKit determines the effect of the Force added to this point based on the shape of the object. Finally speaking of collision, as mentioned in previous articles, cocos2d uses the rect of two objects for collision detection. Other things related to collision detection are difficult to implement, while unity provides a powerful physical system, and the collision inside is really nothing to say, that is, 2d games can still use its physical system, and SpriteKit collision is a bit toward unity, in fact, the entire physical system is a bit like this. Personally, physics body is the rigibody. Of course, unity is a 3d engine, and there are a lot of things about the physical system. In fact, after the node is associated with physics body, SpriteKit makes some calculations when two objects collide. This is one of the most powerful parts I think, that is, you can really think of them as the collision between two real-world objects. after the collision, how will the objects be accurately calculated. However, you cannot perform collision detection by relying on these features. If you want two objects to respond to the collision according to your thoughts, you need to set some attributes. It is also the attribute of pb. These three attributes are very important: categoryBitMask, which marks the class of objects that the object belongs to. The default value is 0 xffffffffcollisionBitMask, which indicates which objects can collide with it, the default value is 0 xffffffffcontactTestBitMask, which indicates which objects will have some impact upon collision with it, the default value is 0x00000000. These three values are all expressed by 32-bit hexadecimal numbers. That is, your game can have 32 objects of different types, which are actually less used, why are these values expressed in this way? This is what I think is new. During collision detection, these values are used for logic and calculation collision, it's not rough. To determine whether another object can collide with itself, perform logic and operation based on the value of collisionBitMask and the value of categoryBitMask of the other object. The result is not 0, that is, a collision can occur. When determining whether two collision objects have an impact, assume that they are A and B. First, the categoryBitMask of A and the contactTestBitMask of B perform logic and operations, and B performs the same operation, when neither of the two results is 0, the two can affect each other during the collision. Think about it carefully and digest it. This is something agreed upon and can be used directly, but if you want to study it in depth, go to the official api. After setting these values, we need some protocols to complete what we want to do. Now we have to use scene. SpriteKit provides us with a protocol called SKPhysicsContactDelegate, which is the only protocol it provides. It sets scene when initializing scene. physicsWorld. contactDelegate = self; In. h header file with the Protocol, it provides two collision detection functions, the-didBeginContact: And-didEndContact: the parameter is a SKPhysicsContact, which contains the information of two collision bodies, and collision information, we suggest you look at the SKPhysicsContact api to develop more handy, we can do what we want to do in these two functions. A collision detection. UsePreciseCollisionDetection: attributes that improve collision accuracy. As mentioned earlier, we will not talk about this. After talking about the collision, the next is the joint that connects multiple pb objects together. It can be understood as a skeleton. If you want your game to show more complex physical effects, you may need it. In a game, sometimes we need to control our game object, and we need to select this object all the time. When the object has pb, we can find it in many ways. The most common method is to look for an object through ray rays. First, we need to determine a ray. For example, in a plane, we can use (0, 0) as the starting point. The point of the finger clicking on the screen is the ending point. Generally, in 3d scenarios, the camera position is used as the starting point, and the clicked position is the ending point to determine a ray, then, you can use the bodyAlongRayStart: end: function to obtain the objects that the rays pass through. Note that the entire function can only obtain one object. If your rays pass through many objects, in this case, you can use a function to obtain all the objects on the ray. In your block, you can get information about each object, based on this information, you can determine what you want. In addition, you can obtain objects by name, as mentioned in the previous article. In addition, you can also find an object based on the vertex. bodyAtPoint: The function determines whether the vertex belongs to an object based on the vertex you provide. If yes, the system returns the object. There is also the bodyAtRect: function, which sets a rect in the scene. When an object enters the rect, bodyAtRect will return this object. Next, we will pay attention to the fact that the entire virtual physical system consumes a lot of resources, so try to reduce the physical computing of your game and make your game run smoothly, remember everything you set for your pb and implement your game logic or effects based on the nature of these settings, only by knowing what they are, can they play their role. For optimization, we have to thank you for your time for research and research. Each game engine has one or several sets of optimization solutions, spriteKit will certainly be able to know how your machine works, and then how your engine works on the machine, this will help you optimize your program. It's almost the same. Something about physical systems is very powerful, and some may need to be used before you know it. In addition, there is also a particle system. You can also find time to study it, write a special note on particle systems. Finally, I provide my own code. For a Demo of Simulate Physics, the Demo only implements some physical features, settings of gravity friction, collision processing, and force addition, click on the screen to generate an sprsprite. Then, you can change the gravity and friction to observe its performance in the scenario. When you touch the boundary, the system will respond, probably like this, if you want to view other attributes, you can directly modify them on the Demo. Oh, my Xcode version is DP2, and it is estimated that there is no major impact. That's all.