Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 向SceneKit物理体应用力?_Swift_Scenekit_Game Physics - Fatal编程技术网

Swift 向SceneKit物理体应用力?

Swift 向SceneKit物理体应用力?,swift,scenekit,game-physics,Swift,Scenekit,Game Physics,我需要一些帮助来理解力是如何应用于SceneKit中的物理体的 我的SCN在此定义如下: let startsphere = SCNSphere(radius: CGFloat(0.2)) startsphere.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3) let startsphereNode = SCNNode(geometry: star

我需要一些帮助来理解力是如何应用于SceneKit中的物理体的

我的SCN在此定义如下:

    let startsphere = SCNSphere(radius: CGFloat(0.2))
    startsphere.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3)
    let startsphereNode = SCNNode(geometry: startsphere)
    startsphereNode.position = SCNVector3(0.0, 1.0, 0.0) // center of playfield, 1m height
    startsphereNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
    startsphereNode.physicsBody?.isAffectedByGravity = false
    startsphereNode.physicsBody?.friction = 0.0
    startsphereNode.physicsBody?.rollingFriction = 0.0
    startsphereNode.physicsBody?.damping = 0.0
    startsphereNode.physicsBody?.restitution = 1.0
该球体被封闭在一个由6个平面组成的盒子中,球体将从其中反弹。设置碰撞遮罩和,使球体从这些墙反弹,我可以观察到,当我移动球体时,会出现这种情况

现在我施加这样的力:

  startsphereNode.physicsBody?.applyForce(SCNVector3(50000,0,0), at: startsphereNode.position, asImpulse: false)
  // or like this
  startsphereNode.physicsBody?.velocity = SCNVector3(200,0,0)
我观察场景中球体的行为。当施加力(或设置速度)时,球体会立即跳出长方体的右侧,然后缓慢向后(向左)移动到其先前设置的位置(0,1,0)。我能看到它穿过墙

现在我的问题是:

  • 为什么它会向右跳(不移动)?我已经研究过冲量和速度的大小,但总是一样的

  • 为什么施力后它会回到原来的位置

  • 为什么它不能从右边的墙上弹下来?我看到它“穿过”墙

  • 我想实现的行为是,根据物理设置,在施加力后,球体在我的盒子内反弹


    有什么想法吗?谢谢。

    以防万一这可能会帮助其他人:

    问题是我的球体也有一个旋转的活动轨迹。在那一刻,我删除了所有的SCNActions,它开始按预期工作

    结果:不要同时使用模拟和物理模拟

    问候
    克里斯

    对你的问题有一些快速的回答

    为什么它会向右跳(不移动)?我到处玩过 具有冲量和速度大小,但始终相同

    您可能没有使用计时器。如果你把你的动作放在计时器里,按下按钮,或者操纵杆等等。。。您可以在具有计时器持续时间的循环中移动它们

    @objc func rapidMove(_ timer: Timer) {
           self.sceneView.box.runAction(SCNAction.move(by: SCNVector3(-0.1,0,0), duration: 0.001))
    
        }
    
    为什么一旦力被释放,它会回到原来的位置 申请

    您可能没有使用演示文稿节点来更新位置

    为什么它不能从右边的墙上弹下来?我看到它“射穿” 墙

    您必须设置碰撞

    需要设置至少两个碰撞类别

     struct CollisionCategory: OptionSet {
    let rawValue: Int
    
    static let sphere  = CollisionCategory(rawValue: 1 << 0) // 00...01
    static let box = CollisionCategory(rawValue: 1 << 1) // 00..10
      }
    
    
    
    startsphereNode.physicsBody?.categoryBitmask = CollisionCategory.sphere.rawValue
    startsphereNode.physicsBody?.contactTestBitMask = CollisionCategory.box.rawValue
    

    只需使用presentation:
    让realPosition=yourNode.presentation.worldPosition
    boxNode.physicsBody?.categoryBitmask = CollisionCategory.box.rawValue
    boxNode.physicsBody?.contactTestBitMask = CollisionCategory.sphere.rawValue