簡體   English   中英

在scnnode上施加力而不會在碰撞時失去它

[英]apply force on scnnode without loosing it on collision

我正在嘗試在Scenekit上創建一些簡單的邏輯,但是到目前為止還沒有運氣。 我有一個球體,位於兩個平面之間。 球體為動態物理體,兩個平面包含靜態物理體。 我在球體上向其中一架飛機施加力。 在碰撞時,球體從平面向相反的方向反彈,但損失了很多力。 我該如何保持碰撞力。 這是通過我的更改在Game模板上的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)
}

scn文件

模擬器

設置物理實體的歸還屬性。

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

“此屬性模擬身體的“彈跳”。 恢復為1.0意味着在碰撞中身體不會損失任何能量,例如,落在平面上的球將彈回其跌落的高度。 恢復為0.0表示碰撞后身體不會反彈。 恢復大於1.0會使身體在碰撞中獲得能量。 默認恢復為0.5。”

此外,您可能希望減少.friction和.rollingFriction屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM