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

问题描述 投票:1回答:1

我试图在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

ios swift scenekit
1个回答
0
投票

设置物理体的恢复属性。

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

“这个属性模拟了身体的”弹性“。 1.0的恢复意味着身体在碰撞中不会失去能量 - 例如,落在平坦表面上的球会弹回到它从下落的高度。恢复0.0意味着身体在碰撞后不会反弹。恢复大于1.0会导致身体在碰撞中获得能量。默认恢复原状是0.5。“

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

© www.soinside.com 2019 - 2024. All rights reserved.