本文介绍了SpriteKit-使精灵克服重力(像气球一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有人知道我如何使我的SKSpriteNode克服重力吗?我曾想过要反转默认重力,但意识到我也需要使东西掉落!

Does anyone have any idea how I can make my SKSpriteNode defy gravity? I thought of inverting the default gravity but realise I also need things to fall too!

似乎应该很容易,但是通读文档我看不到我会怎么做!

It seems like it should be easy but reading through the documentation I can’t see how I would do it!

谢谢

推荐答案

更新:在iOS 8/OS X优胜美地(10.10)中,物理场为此类问题提供了一种优雅的解决方案.这是使用它们为场景添加浮力(针对特定节点集)的快速方法.

Update: In iOS 8 / OS X Yosemite (10.10), physics fields provide an elegant solution to these sorts of problems. Here's a quick take on using them to add buoyancy (for a specific set of nodes) to your scene.

  1. 使用SKFieldNode /linearGravityFieldWithVector:"rel =" nofollow noreferrer> linearGravityFieldWithVector 构造函数,提供与重力相反的向量,并将场节点添加到场景中.
  2. 设置 fieldBitMask 在气球上显示独特的内容.
  3. 设置 categoryBitMask 在场上与气球的fieldBitMask重叠但与其他物体的fieldBitMask不重叠.
  1. Create an SKFieldNode with the linearGravityFieldWithVector constructor, providing a vector that's the opposite of gravity, and add the field node to your scene.
  2. Set the fieldBitMask on your balloons to something unique.
  3. Set the categoryBitMask on the field to something that overlaps with the balloons' fieldBitMask, but that does not overlap with the fieldBitMask of any other bodies.

现在,气球将上升或保持稳定,但其他物体将掉落.调整字段的strength将使您调整气球的浮力是完美平衡重力(以便它们漂浮在适当位置,但在触摸时会受到干扰),还是略大于或小于重力(以便它们缓慢地上升或下降)

Now, the balloons will rise or hold steady, but other objects will fall. Tweaking the field's strength will let you tune whether the balloons' buoyancy is perfectly balancing gravity (so that they float in place, but are disturbed when touched), or slightly more or less than gravity (so that they slowly rise or fall).

默认情况下,一个字段是无限的,覆盖整个场景,但是您可以使用该字段的 region 属性可将其限制为场景的一部分. (如果要模拟水中的浮力,这很有用-一旦某个物体上升到水面上方的场顶上方,它就会掉回水中.)

By default, a field is infinite, covering the whole scene, but you can change that with the field's region property to limit it to a portion of the scene. (This is useful if you want to simulate buoyancy in water — once an object rises past the top of the field at the water's surface, it falls back in.)

此外,如果您希望按照 @TheisEgeberg的答案进行浮力调整,则可以使用 falloff 属性.

Also, if you want variable buoyancy as per @TheisEgeberg's answer, you can control its variation over distance with the falloff property.

在iOS 7/OS X Mavericks(10.9)中,或者如果您想更精确地控制在何时何地施加哪些力,则可以使用以下原始答案中的方法.

In iOS 7 / OS X Mavericks (10.9), or if you want more precise control over which forces apply where and when, you can use the approach from my original answer below.

如果您希望物体真的像气球一样漂浮-即要浮力,受重力影响但还要抵消它-您需要在每一帧上对其施加力(例如,在update:方法).

If you want an object to really float like a balloon — that is, to be buoyant, affected by gravity but also counteracting it — you'll need to apply a force to it on every frame (i.e. in your update: method).

当心缩放:重力是恒定的加速度,但是如果您施加力以抵消重力,则力与质量成正比.要制作一个完美平衡重力的矢量以供在applyForce:中使用,您需要:

Beware scaling: gravity is a constant acceleration, but if you're applying a force to counteract gravity, a force is proportional to mass. To make a vector that perfectly balances gravity for use in applyForce:, you'll need to:

  1. {-1,-1,-1}缩放重力矢量以指向相反的方向
  2. 按要向其施加力的物体的mass缩放(F = ma,其中重力或反重力为a).
  3. 150缩放—存在一个错误,其中SKPhysicsWorld.gravity属性的单位与applyForce:的单位不同. (如果关闭SKPhysicsWorld重力并改为使用SKFieldNode重力,则无需执行此操作.)
  1. scale the gravity vector by {-1,-1,-1} to point in the opposite direction
  2. scale by the mass of the body you're applying the force to (F = ma, where gravity or anti-gravity is a).
  3. scale by 150 — there's a bug where the SKPhysicsWorld.gravity property isn't in the same units as applyForce:. (If you turn SKPhysicsWorld gravity off and use SKFieldNode gravity instead, you don't need to do this.)

与关闭affectedByGravity并应用使气球升起的动作不同,此方法在其余的物理模拟中都很好用.利用与重力完全相等的平衡力,气球将漂浮在适当的位置-与其他物体碰撞后,气球将恢复平衡.如果平衡力大于重力,气球将上升,但其上升将受到其他物体的阻碍.

Unlike turning off affectedByGravity and applying an action to make the balloon rise, this approach works well with the rest of the physics sim. With a balancing force exactly equal to gravity, the balloon will float in place — after colliding with other things it'll return to equilibrium. If the balancing force is greater than gravity, the balloon will rise, but its rise will be hindered by other bodies in its way.

这篇关于SpriteKit-使精灵克服重力(像气球一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:56