场景套件车轮与底盘断开连接

时间:2018-11-06 01:56:56

标签: ios scenekit

我正在尝试制作一个简单的车辆演示。如果我创建的底盘和车轮最初漂浮在地板上方,那么车轮会立即掉落到地板上,而底盘只是漂浮在地板上方。

如果我建立场景以使轮子开始停在地板上,则它们会停留在那儿,但底盘会向上浮动(!),直到它在轮子上方的空中盘旋。它缓慢上浮,动画流畅流畅。

您如何建议我解决此问题?

//
//  Experiment1.swift
//  SKTrailerTow
//
//  Created by Philip Delaquess on 11/3/18.
//  Copyright © 2018 Philip Delaquess. All rights reserved.
//

import SceneKit

class Experiment1 : SCNScene {

    var chasis : SCNNode!

    override init () {
        super.init()

        addLights()
        addCamera()
        addFloor()
        addTractor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addLights () {
        let omni = SCNNode()
        omni.position = SCNVector3Make(0, 5, 0)
        omni.light = SCNLight()
        omni.light!.type = .omni
        self.rootNode.addChildNode(omni)
    }

    private func addCamera () {
        let camera = SCNNode()
        camera.position = SCNVector3Make(2.3, 1.4, 2.4)
        camera.camera = SCNCamera()
        self.rootNode.addChildNode(camera)

        let origin = SCNNode()
        self.rootNode.addChildNode(origin)
        let lookAt = SCNLookAtConstraint(target: origin)
        camera.constraints = [lookAt]
    }

    private func addFloor () {
        let floor = SCNNode()
        floor.position = SCNVector3Make(0, -0.1, 0)
        floor.geometry = SCNFloor()

        let body = SCNPhysicsBody.static()
        floor.physicsBody = body

        self.rootNode.addChildNode(floor)
    }

    private func addTractor () {
        chasis = SCNNode()
        chasis.position = SCNVector3Make(0, 0, 0)
        chasis.geometry = SCNBox(width: 0.5, height: 0.1, length: 1.0, chamferRadius: 0.0)
        chasis.geometry!.firstMaterial!.diffuse.contents = UIColor.yellow
        chasis.geometry!.firstMaterial!.specular.contents = UIColor.white

        let body = SCNPhysicsBody.dynamic()

        body.allowsResting = false
        body.mass = 80
        body.restitution = 0.1
        body.friction = 0.5
        body.rollingFriction = 0
        chasis.physicsBody = body
        self.rootNode.addChildNode(chasis)

        let flWheel = addWheel(toNode: chasis, atPosition: SCNVector3Make(0.25, 0, 0.5))
        let frWheel = addWheel(toNode: chasis, atPosition: SCNVector3Make(-0.25, 0, 0.5))
        let blWheel = addWheel(toNode: chasis, atPosition: SCNVector3Make(0.25, 0, -0.5))
        let brWheel = addWheel(toNode: chasis, atPosition: SCNVector3Make(-0.25, 0, -0.5))

        let vehicle = SCNPhysicsVehicle(chassisBody: body, wheels: [flWheel, frWheel, blWheel, brWheel])
        self.physicsWorld.addBehavior(vehicle)

    }

    private func addWheel (toNode node: SCNNode, atPosition pos: SCNVector3) -> SCNPhysicsVehicleWheel {
        let wheelNode = SCNNode()
        node.addChildNode(wheelNode)
        wheelNode.position = pos
        wheelNode.geometry = SCNSphere(radius: 0.1)
        wheelNode.geometry!.firstMaterial!.diffuse.contents = UIColor.red

        let wheel = SCNPhysicsVehicleWheel(node: wheelNode)
        let connPos = SCNVector3Make(pos.x * 1.5, pos.y, pos.z)
        wheel.connectionPosition = connPos

        return wheel
    }
}

1 个答案:

答案 0 :(得分:0)

自己弄清楚。显然我的车轮太小了。半径0.1无效。当我将所有数字文字乘以10时,似乎一切正常!