ios - 在 scnnode 上施加力而不在碰撞时松开它

标签 ios swift scenekit

我正在尝试在 scenekit 上创建一些简单的逻辑,但到目前为止还没有成功。 我有一个球体,在两个平面之间。球体作为动态物理体,两个平面包含静态物理体。 我在球体上向其中一个平面施加力。碰撞时,球体从平面弹回相反的方向,但会损失很多力。我怎样才能让它保持碰撞力。这是在游戏模板上的 xCode 中生成的 viewDidLoadCode,我做了一些更改:

override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene(named: "art.scnassets/ship.scn")!

    // create and add a camera to the scene
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)

    // place the camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    // create and add a light to the scene
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = .omni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // create and add an ambient light to the scene
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = .ambient
    ambientLightNode.light!.color = UIColor.darkGray
    scene.rootNode.addChildNode(ambientLightNode)

    // retrieve the ship node
    let ship = scene.rootNode.childNode(withName: "sphere", recursively: true)!




    // retrieve the SCNView
    let scnView = self.view as! SCNView

    // set the scene to the view
    scnView.scene = scene

    ship.physicsBody?.applyForce(SCNVector3(x: 100,y: 0, z: 0), asImpulse: false)


    // allows the user to manipulate the camera
    scnView.allowsCameraControl = true

    // show statistics such as fps and timing information
    scnView.showsStatistics = true

    // configure the view
    scnView.backgroundColor = UIColor.black

    // add a tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)
}

the scn file

simulator

最佳答案

设置物理体的恢复属性。

https://developer.apple.com/documentation/scenekit/scnphysicsbody/1514740-restitution

“此属性模拟 body 的“弹性”。 restitution 为 1.0 意味着 body 在碰撞中没有损失能量——例如,一个球掉到平坦的表面上会弹回它掉落的高度。恢复值为 0.0 意味着 body 在碰撞后不会反弹。大于 1.0 的恢复会导致 body 在碰撞中获得能量。默认恢复为 0.5。”

此外,您可能希望减少 .friction 和 .rollingFriction 属性。

关于ios - 在 scnnode 上施加力而不在碰撞时松开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48034291/

相关文章:

html - iOS Safari 上主页上的文字颜色不同

swift - searchBar 函数但是 TableView fatal error

swift - 如何使用全局结构进行单元测试?

swift - 光线不随相机移动 - SceneKit

ios - 导航栏中的按钮图像被拉伸(stretch)

javascript - iOS 上的 Safari 在页面底部加载 JavaScript 之前等待 10-30 秒

objective-c - SceneKit 检测给定区域的触摸

ios - 参数标签 '(MDLObject:)' 与 Xcode 10 中的任何可用重载不匹配

ios - 通过 XCode 中的 dateFromString String Date 和 NSDate 不匹配

swift - 在 Swift 中返回不同类类型的多态方法?