我一直在尝试移动车身沿着车轮我已经创建了一个增强现实项目,将汽车模型放置在水平面上,汽车由四个按钮控制,即加速,转向,倒车和刹车汽车左,右是控制器转向,而加速和倒车
其实现在我可以把三维汽车模型在水平面上,但不知道如何旋转车轮一样向前和向后沿着车身。
下面是我一直在尝试放置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
}
}
任何帮助都非常感谢!!!
1条答案
按热度按时间klr1opcd1#
这是一项相当全面的任务。
您应该将汽车作为SCNPhysicsVehicle子类。
SCNPhysicsVehicle是一种物理行为,它将物理主体修改为像汽车、摩托车或其他轮式车辆一样的行为。
要构建车辆,请指定SCNPhysicsBody对象作为其底盘,并指定SCNPhysicsVehicleWheel对象数组作为其车轮。对于每个车轮,您可以定义物理特征(如悬挂和牵引力),并在场景中关联节点以提供车轮的大小和视觉表示。构建车辆后,您可以在加速、制动和转向方面对其进行控制。
常规:https://developer.apple.com/documentation/scenekit/scnphysicsbody
对于车轮:https://developer.apple.com/documentation/scenekit/scnphysicsvehiclewheel
示例:
1.需要使用静态几何体创建地面。
1.你得把车修好。
1.现在你可以操纵你的车了。
P.S.不要忘记在viewDidLoad()中添加以下内容:
P. P. S这只是一个大概的想法,你必须做一些研究,并找到一个工作的解决方案,以您的具体情况。
我希望它有帮助!