使用SceneKit,我知道如何使用SCNphysicsField创建线性重力。我的问题是,是否可以指定3D空间中特定点的方向(例如,指向球体的中心)。
这是我尝试过的radialGravity()
:
let gravityNode=SCNNode()
let gravity=SCNPhysicsField.radialGravity()
gravity.scope=SCNPhysicsFieldScope.insideExtent
gravity.strength=9.8
gravityNode.physicsField=gravity
gravityNode.position=SCNVector3Make(0, 0, 0)
homeScene.rootNode.addChildNode(gravityNode)
答案 0 :(得分:1)
使用SCNPhysicsField.radialGravity()
。您还需要将物理实体放在您想要受影响的每个对象上(可能还有类别位掩码)。
以下是可以在macOS Playground中运行的示例:
//: Playground - noun: a place where people can play import Cocoa import SceneKit import SpriteKit import PlaygroundSupport let scene = SCNScene() let gravityNode = SCNNode() let radialGravityField = SCNPhysicsField.radialGravity() gravityNode.physicsField = radialGravityField gravityNode.position = SCNVector3Make(0, 10, 0) radialGravityField.strength = 9.8 // comment out the next line to disable the radial gravity field scene.rootNode.addChildNode(gravityNode) // uncomment the next line to disable default gravity //scene.physicsWorld.gravity = SCNVector3Make(0, 0, 0) let boxGeometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0) boxGeometry.firstMaterial?.diffuse.contents = SKColor.yellow let shape = SCNPhysicsShape(geometry: boxGeometry, options: nil) let boxNode1 = SCNNode(geometry: boxGeometry) boxNode1.position = SCNVector3Make(0, 6, 0) scene.rootNode.addChildNode(boxNode1) let body1 = SCNPhysicsBody(type: .dynamic, shape: shape) boxNode1.physicsBody = body1 let boxNode2 = SCNNode(geometry: boxGeometry) boxNode2.position = SCNVector3Make(0, 14, 0) scene.rootNode.addChildNode(boxNode2) let body2 = SCNPhysicsBody(type: .dynamic, shape: shape) boxNode2.physicsBody = body2 let pyramidNode = SCNNode(geometry: SCNPyramid(width: 1, height: 1, length: 1)) pyramidNode.position = SCNVector3Make(2, 10, -2) pyramidNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: pyramidNode.geometry!)) scene.rootNode.addChildNode(pyramidNode) // put something down at ground level to trick the automatic camera let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.2)) sphereNode.position = SCNVector3Make(0, -1, 0) scene.rootNode.addChildNode(sphereNode) let floor = SCNFloor() let floorNode = SCNNode(geometry: floor) floorNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: floor, options: nil)) scene.rootNode.addChildNode(floorNode) let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 600, height: 400)) sceneView.allowsCameraControl = true sceneView.autoenablesDefaultLighting = true sceneView.backgroundColor = SKColor.lightGray sceneView.showsStatistics = true sceneView.debugOptions = [.showPhysicsFields, .showPhysicsShapes] PlaygroundPage.current.liveView = sceneView sceneView.scene = scene