本文介绍了意外的物理体现在SpriteKit场景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpriteKit实现一个质量弹簧系统(许多小型物理机构与SKPhysicsJointSpring实例连接在一起)。一些粒子在穿过场景中心时会被钩住。

I'm implementing a mass-spring system (many small physics bodies joined together with SKPhysicsJointSpring instances) with SpriteKit. Some of the particles would get snagged while traversing the center of the scene.

在场景中间似乎有一个小的静态物体而我不是知道为什么会这样。

There seems to be a small, static body in the middle of the scene and I don't know why it's there.

这里有一个简单的方法可以看到我在说什么:

Here's an easy way to see what I'm talking about:


  1. 在XCode 8中,使用游戏模板创建一个全新的项目。

  2. GameViewController.viewDidLoad(),添加 view.showsPhysics = true

  1. In XCode 8, create a brand new project with the "Game" template.
  2. In GameViewController.viewDidLoad(), add view.showsPhysics = true

如果运行项目,你应该在中间看到一个小点,这是错误的身体:

If you run the project, you should see a little dot in the middle, which is the errant body:

任何人都知道如何摆脱它?

Anyone know how to get rid of it?

编辑:我试图手动创建场景对象:

I tried to manually create the scene object:

GameViewController.viewDidLoad()中,我将其替换为:

// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
    view.presentScene(scene)
}

with this:

with this:

let scene = GameScene(size: view.frame.size)
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
view.presentScene(scene)

但是没有解决它。

推荐答案

无论如何,我决定回答是因为我想分享很多信息而不适合评论。遗憾的是,我的回答并没有回答这个问题,但是它提供了一些关于这个身份不明显的有用信息,显然能够飞行(物理身体)对象:)

Anyways, I decided to make an answer because comments are not suitable due to lot of info I want to share. Also my answer, sadly, doesn't answer the question but it gives some useful info about this unidentified, obviously capable of flying (physics body) object :)

所以这个代码是如何抓住它(并修改它):

So this is the code how to grab it (and modify it???):

 //you can use self.frame here...I just copied Alessandro's code
 self.physicsWorld.enumerateBodies(in:(label?.frame)!) { body, stop in

    if let node = body.node {

        print("Type of this node: \(type(of:node))")
        print("Frame of this node: \(node.frame))")

    }else{

       print("This body's node property is nil")

        body.affectedByGravity = true
        body.isDynamic = true

        body.applyImpulse(CGVector(dx: 0.003, dy: 0.0003))

    }
     print("Area covered by this node physicsBody: \(body.area)")
}

所以如果你在其他地方设置一个断点声明,你可以完全扫描这个物理体并得到它的所有信息,比如它的节点属性设置为nil 或者它的 isDynamic property设置为 false 。但是你可以改变它,就像在我的代码中一样,将 isDynamics 设置为true。这使它可以移动。因此,如果你对它施加一些力量,它就会移动。

So if you put a break point inside of that else statement, you can scan this physics body completely and get all the info about it, like that its node property is set to nil or that its isDynamic property is set to false. But you can change that, and like in my code, set for example isDynamics to true. This makes it moveable. So if you apply some forces to it, it will move.

仍然,就像我在评论中说的那样,我不知道它为什么存在,它是什么表示或它的目的是什么。

Still, like I said in comments, I don't have an idea why it is there and what it represents or what is its purpose.

此外,对于那些想知道一个物理主体是否有可能没有与之关联的节点的人( body.node 等于 nil )但在 showsPhysics 时屏幕上仍然可见设置为 true ,有一个合理的解释。物理世界与节点树分开。所以我们可以从节点树中删除一个精灵,但这并不意味着它的物理主体会被立即删除。物理引擎可能还没有完成模拟......所以你可能想知道,这会怎么样?

Also, for those who are wondering how it is possible that one physics body doesn't have a node associated with it ( body.node equals nil) but is still visible on screen when showsPhysics is set to true, there is a reasonable explanation. Physics world is separated from the node tree. So we can remove a sprite from a node tree, but that doesn't mean that its physics body will be removed instantly. It may happen that physics engine haven't finished simulation... So you probably wonder, how this might happen?

假设您有三个 SKSpriteNode 同时相交的对象(比如A联系人B和A联系人C at at同一时间)。 SpriteKit 一次只能处理一个联系人。并说你在与B联系时从父母那里删除A.然后,A和C之间也有联系,所以 didBegin:(_ contact)将被叫两次。如果您在第一个 didBegin(_ contact)调用中从其父项中删除A,则在下一个 didBegin(_ contact) call, bodyA.node 将为nil( bodyA 是精灵A的物理体),但它的物理体在发动机完成所需的任务之前,它将保持可见这是因为节点树和物理世界是分开的。

Let say you have three SKSpriteNode objects intersecting at the same time (say A contacts B and A contacts C at the same time). SpriteKit can process only one contact at time. And say that you are removing A from a parent when it is contacting with B. Then, there is a contact between A and C also, so didBegin:(_ contact) will be called twice. And if you remove A from its parent in first didBegin(_ contact) call, in the next didBegin(_ contact) call, bodyA.node will be nil (bodyA is a physics body of sprite A), but its physics body will remain visible until engine finishes what needed. This is because node tree and a physics world are separated.

这篇关于意外的物理体现在SpriteKit场景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:12