ios 如何快速地沿着车轮移动车身?

n9vozmp4  于 2022-11-26  发布在  iOS
关注(0)|答案(1)|浏览(153)

我一直在尝试移动车身沿着车轮我已经创建了一个增强现实项目,将汽车模型放置在水平面上,汽车由四个按钮控制,即加速,转向,倒车和刹车汽车左,右是控制器转向,而加速和倒车
其实现在我可以把三维汽车模型在水平面上,但不知道如何旋转车轮一样向前和向后沿着车身。
下面是我一直在尝试放置3D对象的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let touch = touches.first {

        // gives us the location of where we touched on the 2D screen.
        let touchLocation = touch.location(in: sceneView)

        // hitTest is performed to get the 3D coordinates corresponding to the 2D coordinates that we got from touching the screen.
        // That 3d coordinate will only be considered when it is on the existing plane that we detected.
        let results = sceneView.hitTest(touchLocation, types: .existingPlaneUsingExtent)

        // if we have got some results using the hitTest then do this.
        if let hitResult = results.first {

           let boxScene = SCNScene(named: "art.scnassets/porsche.scn")!

            if let boxNode = boxScene.rootNode.childNode(withName: "car", recursively: true) {
                print("box:::\(boxNode.childNodes)")

                boxNode.position = SCNVector3(x: hitResult.worldTransform.columns.3.x, y: hitResult.worldTransform.columns.3.y + 0.15, z: hitResult.worldTransform.columns.3.z)

                // finally the box is added to the scene.
                sceneView.scene.rootNode.addChildNode(boxNode)

            }

        }

    }
}

检测水平面功能代码:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    if anchor is ARPlaneAnchor {

        // anchors can be of many types, as we are just dealing with horizontal plane detection we need to downcast anchor to ARPlaneAnchor
        let planeAnchor = anchor as! ARPlaneAnchor

        // creating a plane geometry with the help of dimentions we got using plane anchor.
        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))

        // a node is basically a position.
        let planeNode = SCNNode()

        // setting the position of the plane geometry to the position we got using plane anchor.
        planeNode.position = SCNVector3(x: planeAnchor.center.x, y: 0, z: planeAnchor.center.z)

        // when a plane is created its created in xy plane instead of xz plane, so we need to rotate it along x axis.
        planeNode.transform = SCNMatrix4MakeRotation(-Float.pi/2, 1, 0, 0)

        //create a material object
        let gridMaterial = SCNMaterial()

        //setting the material as an image. A material can also be set to a color.
        gridMaterial.diffuse.contents = UIImage(named: "art.scnassets/grid.png")

        // assigning the material to the plane
        plane.materials = [gridMaterial]

        // assigning the position to the plane
        planeNode.geometry = plane

        //adding the plane node in our scene
        node.addChildNode(planeNode)


    }

    else {

        return
    }

}

任何帮助都非常感谢!!!

klr1opcd

klr1opcd1#

这是一项相当全面的任务。
您应该将汽车作为SCNPhysicsVehicle子类。
SCNPhysicsVehicle是一种物理行为,它将物理主体修改为像汽车、摩托车或其他轮式车辆一样的行为。
要构建车辆,请指定SCNPhysicsBody对象作为其底盘,并指定SCNPhysicsVehicleWheel对象数组作为其车轮。对于每个车轮,您可以定义物理特征(如悬挂和牵引力),并在场景中关联节点以提供车轮的大小和视觉表示。构建车辆后,您可以在加速、制动和转向方面对其进行控制。
常规:https://developer.apple.com/documentation/scenekit/scnphysicsbody
对于车轮:https://developer.apple.com/documentation/scenekit/scnphysicsvehiclewheel
示例:

var vehicle = SCNPhysicsVehicle()

1.需要使用静态几何体创建地面。

func createGround(planeAnchor: ARPlaneAnchor) -> SCNNode {
   let ground = SCNNode(geometry: SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(CGFloat(planeAnchor.extent.z))))
   ground.position = SCNVector3(planeAnchor.center.x,planeAnchor.center.y,planeAnchor.center.z)
   ground.eulerAngles = SCNVector3(90.degreesToRadians, 0, 0)
   let staticBody = SCNPhysicsBody.static() // it must be static
   ground.physicsBody = staticBody
   return ground
   }

1.你得把车修好。

func setupCar() {

let scene = SCNScene(named: "YourScene.scn")
let chassis = (scene?.rootNode.childNode(withName: "chassis", recursively: true))! // Your chassis
let frontLeftWheel = chassis.childNode(withName: "frontLeftWheel", recursively: true)!
let frontRightWheel = chassis.childNode(withName: "frontRightWheel", recursively: true)!
let rearLeftWheel = chassis.childNode(withName: "rearLeftWheel", recursively: true)!
let rearRightWheel = chassis.childNode(withName: "rearRightWheel", recursively: true)!

// physic behavior for wheels

let v_frontLeftWheel = SCNPhysicsVehicleWheel(node: frontLeftWheel)
let v_frontRightWheel = SCNPhysicsVehicleWheel(node: frontRightWheel)
let v_rearRightWheel = SCNPhysicsVehicleWheel(node: rearLeftWheel)
let v_rearLeftWheel = SCNPhysicsVehicleWheel(node: rearRightWheel)

chassis.position = SCNVector(0,0,0) // insert your desired position
let body = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: chassis, options: [SCNPhysicsShape.Option.keepAsCompound: true]))
body.mass = 1
chassis.physicsBody = body
self.vehicle = SCNPhysicsVehicle(chassisBody: chassis.physicsBody!, wheels: [v_rearRightWheel, v_rearLeftWheel, v_frontRightWheel, v_frontLeftWheel])
self.sceneView.scene.physicsWorld.addBehavior(self.vehicle)
self.sceneView.scene.rootNode.addChildNode(chassis)
}

1.现在你可以操纵你的车了。

func renderer(_ renderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: TimeInterval) {

      var engineForce: CGFloat = 5
      var brakingForce: CGFloat = 0

      // here you can manipulate your car steering angle
      self.vehicle.setSteeringAngle(0, forWheelAt: 2) 
      self.vehicle.setSteeringAngle(0, forWheelAt: 3)

      self.vehicle.applyEngineForce(engineForce, forWheelAt: 0)
      self.vehicle.applyEngineForce(engineForce, forWheelAt: 1)
      self.vehicle.applyBrakingForce(0, forWheelAt: 0)
      self.vehicle.applyBrakingForce(0, forWheelAt: 1)
      }

P.S.不要忘记在viewDidLoad()中添加以下内容:

self.sceneView.delegate = self
        self.sceneView.scene?.physicsWorld.contactDelegate = self

P. P. S这只是一个大概的想法,你必须做一些研究,并找到一个工作的解决方案,以您的具体情况。
我希望它有帮助!

相关问题