Swift 向SceneKit物理体应用力?
我需要一些帮助来理解力是如何应用于SceneKit中的物理体的 我的SCN在此定义如下:Swift 向SceneKit物理体应用力?,swift,scenekit,game-physics,Swift,Scenekit,Game Physics,我需要一些帮助来理解力是如何应用于SceneKit中的物理体的 我的SCN在此定义如下: let startsphere = SCNSphere(radius: CGFloat(0.2)) startsphere.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3) let startsphereNode = SCNNode(geometry: star
let startsphere = SCNSphere(radius: CGFloat(0.2))
startsphere.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3)
let startsphereNode = SCNNode(geometry: startsphere)
startsphereNode.position = SCNVector3(0.0, 1.0, 0.0) // center of playfield, 1m height
startsphereNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
startsphereNode.physicsBody?.isAffectedByGravity = false
startsphereNode.physicsBody?.friction = 0.0
startsphereNode.physicsBody?.rollingFriction = 0.0
startsphereNode.physicsBody?.damping = 0.0
startsphereNode.physicsBody?.restitution = 1.0
该球体被封闭在一个由6个平面组成的盒子中,球体将从其中反弹。设置碰撞遮罩和,使球体从这些墙反弹,我可以观察到,当我移动球体时,会出现这种情况
现在我施加这样的力:
startsphereNode.physicsBody?.applyForce(SCNVector3(50000,0,0), at: startsphereNode.position, asImpulse: false)
// or like this
startsphereNode.physicsBody?.velocity = SCNVector3(200,0,0)
我观察场景中球体的行为。当施加力(或设置速度)时,球体会立即跳出长方体的右侧,然后缓慢向后(向左)移动到其先前设置的位置(0,1,0)。我能看到它穿过墙
现在我的问题是:
有什么想法吗?谢谢。以防万一这可能会帮助其他人: 问题是我的球体也有一个旋转的活动轨迹。在那一刻,我删除了所有的SCNActions,它开始按预期工作 结果:不要同时使用模拟和物理模拟 问候
克里斯对你的问题有一些快速的回答 为什么它会向右跳(不移动)?我到处玩过 具有冲量和速度大小,但始终相同 您可能没有使用计时器。如果你把你的动作放在计时器里,按下按钮,或者操纵杆等等。。。您可以在具有计时器持续时间的循环中移动它们
@objc func rapidMove(_ timer: Timer) {
self.sceneView.box.runAction(SCNAction.move(by: SCNVector3(-0.1,0,0), duration: 0.001))
}
为什么一旦力被释放,它会回到原来的位置
申请
您可能没有使用演示文稿节点来更新位置
为什么它不能从右边的墙上弹下来?我看到它“射穿”
墙
您必须设置碰撞
需要设置至少两个碰撞类别
struct CollisionCategory: OptionSet {
let rawValue: Int
static let sphere = CollisionCategory(rawValue: 1 << 0) // 00...01
static let box = CollisionCategory(rawValue: 1 << 1) // 00..10
}
startsphereNode.physicsBody?.categoryBitmask = CollisionCategory.sphere.rawValue
startsphereNode.physicsBody?.contactTestBitMask = CollisionCategory.box.rawValue
只需使用presentation:
让realPosition=yourNode.presentation.worldPosition
boxNode.physicsBody?.categoryBitmask = CollisionCategory.box.rawValue
boxNode.physicsBody?.contactTestBitMask = CollisionCategory.sphere.rawValue