How a Scene Processes Frames of Animation

  In the traditional view system, the contents of a view are rendered once and then rendered again only when the model’s contents change. This model works very well for views, because in practice most view content is static. Sprite Kit, on the other hand, is designed explicitly for dynamic content. Sprite Kit continuously updates the scene contents and renders it to ensure that animation is smooth and accurate.

  The process of animating and rendering the scene is tied to the scene object (SKScene).

  

Simulating Physics

  To use physics in your game, you need to:

  • Attach physics bodies to nodes in the node tree. See “All Physics is Simulated on Physics Bodies.”
  • Configure the physical properties of the physics bodies. See “Configuring the Physical Properties of a Physics Body.”
  • Define global characteristics of the scene’s physics simulation, such as gravity. See “Configuring the Physics World.”
  • Where necessary to support your gameplay, set the velocity of physics bodies in the scene or apply forces or impulses to them. See “Making Physics Bodies Move.”
  • Define how the physics bodies in the scene interact when they come in contact with each other. See “Working with Collisions and Contacts.”
  • Optimize your phys

All Physics is Simulated on Physics Bodies

  An SKPhysicsBody object defines the shape and simulation parameters for a physics body in the system. When the scene simulates physics, it performs the calculations for all physics bodies connected to the scene tree. So, you create an SKPhysicsBody object, configure its properties, and then assign it to a node’s physicsBody property.

There are three kinds of physics bodies:

  • A dynamic volume simulates a physical object with volume and mass that can be affected by forces and collisions in the system. Use dynamic volumes to represent items in the scene that need to move around and collide with each other.
  • A static volume is similar to a dynamic volume, but its velocity is ignored and it is unaffected by forces or collisions. However, because it still has volume, other objects can bounce off it or interact with it. Use static volumes to represent items that take up space in the scene, but that should not be moved by the simulation. For example, you might use static volumes to represent the walls of a maze.While it is useful to think of static and dynamic volumes as distinct entities, in practice these are two different modes you can apply to any volume-based physics body. This can be useful because you can selectively enable or disable effects for a body.
  • An edge is a static volume-less body. Edges are never moved by the simulation and their mass doesn’t matter. Edges are used to represent negative space within a scene (such as a hollow spot inside another entity) or an uncrossable, invisibly thin boundary. For example, edges are frequently used to represent the boundaries of your scene.The main difference between a edge and a volume is that an edge permits movement inside its own boundaries, while a volume is considered a solid object. If edges are moved through other means, they only interact with volumes, not with other edges.

  

Use a Physics Shape That Matches the Graphical Representation

  when choosing a shape for your physics body, do not be overly precise. More complex shapes require more work to be properly simulated. For volume-based bodies, use the following guidelines:

  • A circle is the most efficient shape.
  • A path-based polygon is the least efficient shape, and the computational work scales with the complexity of the polygon.

An edge-based body is more expensive to compute than a volume-based body. This is because the bodies it interacts with can potentially be on either side of an open edge or on the inside or outside of a closed shape. Use these guidelines:

  • Lines and rectangles are the most efficient edge-based bodies.
  • Edge loops and edge chains are the most expensive edge-based bodies, and the computational work scales with the complexity of the path

Creating an Edge Loop Around the Scene

  Listing 8-1 shows code that is used frequently in games that do not need to scroll the content. In this case, the game wants physics bodies that hit the borders of the scene to bounce back into the gameplay area.

  

Creating a Circular Volume for a Sprite

  Listing 8-2 shows the code that creates the physics body for a spherical or circular object. Because the physics body is attached to a sprite object, it usually needs volume. In this case, the sprite image is assumed to closely approximate a circle centered on the anchor point, so the radius of the circle is calculated and used to create the physics body.

  

  

最新文章

  1. Oracle的tnsnames.ora配置(PLSQL Developer)
  2. 一个简单的Webservice的demo(中)_前端页面调用
  3. JVM之运行时常量池(Runtime Constant Pool)
  4. C#的继承
  5. 找出只含有2,3,5因子构成的数的第N个
  6. 获取Assets目录下的图片并显示
  7. ruby编程语言-学习笔记4(第4章 表达式和操作符)
  8. 在kafka上对topic新增partition
  9. CodeForces#378--A, B , D--暴力出奇迹....
  10. 老李推荐: 第14章2节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer架构概述
  11. Java 枚举7常见种用法(转)
  12. PHP MYSQL 搜索周边坐标,并计算两个点之间的距离
  13. 挖一挖不常用到而又很实用的重载-Trim
  14. Linux显示隐藏文件
  15. 2017-2018-1 1623 bug终结者 冲刺005
  16. 学习ASP.NET Core Razor 编程系列十一——把新字段更新到数据库
  17. vue--动画收缩
  18. react-native 安装的时候遇到的问题
  19. logback-MDC日志唯一标识
  20. 居中分栏flex完美的解决方案

热门文章

  1. datagridview的某些属性以及增删改查
  2. [Swift系列]001-入门准备
  3. php实现一致性哈希算法
  4. 在页面中使用js
  5. BZOJ 4004 装备购买
  6. java金额的加减乘除
  7. mac vim shell配置
  8. 【转】Eclipse快捷键 10个最有用的快捷键----不错
  9. android部分手机onclick事件触发2次
  10. Javascript中的Keycode值列表