Physicals

  The physics simulation in Sprite Kit is performed by adding physics bodies to scenes.

Type of Physics Body

  An SKPhysicsBody object defines the shape and simulation parameters for a physics body in the system.  

  There are three kinds of physics bodies:

  • 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.

  • 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.

  

Physics Shape

 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.
  • 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.

Configuring the Physical Properties of a Physics Body

  The SKPhysicsBody class defines properties that determine how the physics body is simulated.

  • The mass property determines how forces affect the body, as well as how much momentum the body has when it is involved in a collision.
  • The friction property determines the roughness of the body’s surface. It is used to calculate the frictional force that a body applies to other bodies moving along its surface.
  • The linearDamping and angularDamping properties are used to calculate friction on the body as it moves through the world. For example, this might be used to simulate air or water friction.
  • The restitution property determines how much energy a body maintains during a collision—its bounciness.

Other properties are used to determine how the simulation is performed on the body itself:

  • The dynamic property determines whether the body is simulated by the physics subsystem.
  • The affectedByGravity property determines whether the simulation exerts a gravitational force on the body. For more information on the physics world, see “Configuring the Physics World.”
  • The allowsRotation property determines whether forces can impart angular velocity on the body.

Mass Determines a Body’s Resistance to Acceleration

  You should set the mass on every volume-based body in your scene so that it properly reacts to forces applied to it.

  A physics body’s mass, area, and density properties are all interrelated. When you first create a body, the body’s area is calculated, and never changes afterwards. The other two properties change values at the same time, based on the following formula:

  mass = density x area

When you configure a physics body, you have two options:

  • Set the mass property of the body. The density property is then automatically recalculated. This approach is most useful when you want to precisely control each body’s mass.
  • Set the density property of the body. The mass property is then automatically recalculated. This approach is most useful when you have a collection of similar bodies created with different sizes. For example, if your physics bodies were used to simulate asteroids, you might give all asteroids the same density, and then set an appropriate bounding polygon for each. Each body automatically computes an appropriate mass based on its size on the screen.

Physics World

  All physics bodies in a scene are part of the physics world, which is represented in Sprite Kit by an SKPhysicsWorld object attached to the scene. The physics world defines two important characteristics of the simulation:

  • The gravity property applies an acceleration to volume-based bodies in the simulation. Static volumes and physics bodies that have set the affectedByGravity property to NO are unaffected.
  • The speed property determines the rate at which the simulation runs.

Making Physics Bodies Move

  you can control a physics body’s velocity directly, by setting its velocity and angularVelocity properties.

  The default collection of forces that apply to a body include:

  • The gravitational force applied by the physics world
  • The damping forces applied by the body’s own properties
  • A frictional force based on contact with another body in the system

  You can also apply your own forces and impulses to physics bodies.

  • A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects
  • An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.

  For example, assume for a moment you are making a space-based game where a rocket ship can fire missiles. When the ship fires a missile, the missile should have a starting velocity of the ship plus an additional vector in the direction of the launch. Listing 8-3 shows one implementation for calculating the launch velocity.

  

  Forces and impulses are modeling the same concept—adjusting a body’s velocity.

  You can apply a force to a body in one of three ways:

  • A linear force that only affects the body’s linear velocity.

  • An angular force that only affects the body’s angular velocity.

  • A force applied to a point on the body. The physics simulation calculates separate changes to the body’s angular and linear velocity, based on the shape of the object and the point where the force was applied.

Working with Collisions and Contacts

  Sprite Kit uses two kinds of interactions between physics bodies:

  • A contact is used when you need to know that two bodies are touching each other. In most cases, you use contacts when you need to make gameplay changes when a collision occurs.
  • A collision is used to prevent two objects from interpenetrating each other. When one body strikes another body, Sprite Kit automatically computes the results of the collision and applies impulse to the bodies in the collision.

  

  

  It isn’t necessary for these interactions to be symmetrical, because Sprite Kit only calls your delegate once per frame for each contact. Either body can specify that it is interested in the contact. Because a missile already requests a contact message when it strikes a ship, the ship does not need to ask for the same contact message.

  When Sprite Kit performs collision detection, it first determines the locations of all of the physics bodies in the scene. Then it determines whether collisions or contacts occurred. This computational method is fast, but can sometimes result in missed collisions. A small body might move so fast that it completely passes through another physics body without ever having a frame of animation where the two touch each other.

  If you have physics bodies that must collide, you can hint to Sprite Kit to use a more precise collision model to check for interactions. This model is more expensive, so it should be used sparingly. When either body uses precise collisions, multiple movement positions are contacted and tested to ensure that all contacts are detected.

  

Physicals的更多相关文章

  1. cocos基础教程(13)使用Physicals代替Box2D和chipmunk

    1.   概述 游戏中模拟真实的世界是个比较麻烦的事情,通常这种事情都是交给物理引擎来做.首屈一指的是Box2D了,它几乎能模拟所有的物理效果.而chipmunk则是个更轻量的引擎,能够满足简单的物理 ...

  2. Unity Physicals Rigidbody with multiple colliders

    Rigidbody with multiple colliders adding colliders changes the center of mass and rotation behaviour ...

  3. 让子弹飞Demo版

    让子弹飞是我非常喜欢的一款游戏.今天的目标就是利用cocos2dx 3.0 和box2d 打造一款这样的类型游戏的Demo版.本来cocos2dx 3.0 已经封装了physicals模块,可是我在使 ...

  4. Cocos2d-x 3.0 简捷的物理引擎

    Cocos2d-x 3.0 开发(九)使用Physicals取代Box2D和chipmunk http://www.cocos2d-x.org/docs/manual/framework/native ...

随机推荐

  1. android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用

    你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢? 你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢? 如果,你想,那你是不是很像知道,我们应 ...

  2. 如何直接在github网站上更新你fork的repo?

    玩过github的人一定会在你自己的账号上fork了一些github开源项目.这些开源项目往往更新比较活跃,你今天fork用到你自己的项目中去了,过几个星期这个fork的origin可能有一些bugf ...

  3. Mybatis返回插入的主键

    在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能 情景一:针对自增主键的表,在插入时不 ...

  4. win7防火墙开启ping

    默认情况下,Windows7出于安全考虑是不允许外部主机对其进行Ping测试的. 但在局域网环境中,Ping是测试网络情况的常用手段,如何允许 Windows7的ping测试回显呢? 打开: 控制面板 ...

  5. res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x ...

  6. UVa 1587 Box

    题意:给出6个矩形的长和宽,问是否能够构成一个长方体 先假设一个例子 2 3 3 4 2 3 3 4 4 2 4 2 排序后 2 3 2 3 3 4 3 4 4 2 4 2 如果要构成一个长方体的话, ...

  7. HDU 5344 MZL's xor (水题)

    题意:给一个序列A,设序列B的中的元素有(Ai+Aj)(1≤i,j≤n),那么求B中所有元素的异或之和.而序列A是这样来的:A1=0,Ai=(Ai−1∗m+z) mod l. 思路:相同的元素异或结果 ...

  8. 聊聊Oracle 11g的Snapshot Standby Database(上)

    Oracle 11g是Data Guard的重要里程碑版本.在11g中,Active DataGuard.Advanced Compression等特性大大丰富了Data Guard的功能和在实践领域 ...

  9. Levenshtein Distance (编辑距离) 算法详解

    编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...

  10. MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)

    MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...