将简单的物理应用于SceneKit XCODE SWIFT中的.scn对象 [英] Applying simple Physics to a .scn object in SceneKit XCODE SWIFT

查看:76
本文介绍了将简单的物理应用于SceneKit XCODE SWIFT中的.scn对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,下面有一个我创建的普通球体,用于测试游戏场景/世界中是否具有物理场.因此,我只是将球放在场景/世界中,这是完美的.它受重力影响.因此,然后我尝试对.scn文件执行完全相同的操作.我给它的物理性质与跌落到重力的测试球相同.但那个人不动.重力设置为-9.8以模拟常规重力代码:

Hey Below I have a normal sphere that I created just to test if my in game scene/world has physics. So I simply put the ball in the scene/world and it is perfect. It is affected by the gravity. So then I try to do the exact same thing to the .scn file. I give it physics the same as the test sphere that falls down do to gravity. but the man doesn't move. The gravity is set to -9.8 to simulate regular gravity Code:

  //----Test-Circle-here--------------------

    var sphere1: SCNNode!
    let sphereGeometry = SCNSphere(radius: 10.5)
    let sphereMaterial = SCNMaterial()
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    sphereMaterial.diffuse.contents = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]
    sphere1 = SCNNode(geometry: sphereGeometry)
    sphere1.position = SCNVector3(x: 1.0, y: 0.05, z: 0.05)

    //----Giving it a physics---------

    sphere1.physicsBody?.affectedByGravity = true
    sphere1.physicsBody?.friction = 0
    sphere1.physicsBody?.restitution = 1 //bounceness of the object
    sphere1.physicsBody?.angularDamping = 1 // rotationess
    sphere1.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    scnView.scene!.rootNode.addChildNode(sphere1)

在男人的下方,无论如何都保持原状

Below the man Stays in same- spot no matter what some how

     class Character {

let node = SCNNode()
init() {

    let GuyScene = SCNScene(named: "art.scnassets/FoxMan2.scn")
    let characterTopLevelNode: SCNNode = GuyScene!.rootNode.childNodeWithName("Guy", recursively: true)!
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    characterTopLevelNode.position = SCNVector3(x: 10.0, y: 0.0, z: 0.0)

    //----Giveing it a physics---------

    characterTopLevelNode.physicsBody?.affectedByGravity = true
    characterTopLevelNode.physicsBody?.friction = 0
    characterTopLevelNode.physicsBody?.restitution = 1 //bounceness of the object
    characterTopLevelNode.physicsBody?.angularDamping = 1 // rotationess
    characterTopLevelNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    node.addChildNode(characterTopLevelNode)

 }
}

在此处输入图像描述

在此处输入图片描述

推荐答案

检查并确保您的 characterTopLevelNode 实际上是您认为的节点(名称不匹配很常见).在这种情况下,这通常是问题所在.您似乎正在将 characterTopLevelNode 添加为 node 的子级,但从未添加 node 作为正在显示的场景的子级.

Check to make sure that your characterTopLevelNode is actually the node you think it is (name mismatches are pretty common). That's often the problem in these kinds of situations. You seem to be adding the characterTopLevelNode as a child of node but never adding node as a child of the scene being displayed.

另一件事,在创建物理实体之前,请勿在物理实体上设置选项.

One other thing, don't set the options on the physics body before you create the physics body.

例如:sphere1.physicsBody = SCNPhysicsBody(type:.Dynamic,...sphere1.physicsBody.affectedByGravity = truesphere1.physicsBody.friction = 0sphere1.physicsBody.restitution = 1sphere1.physicsBody.angularDamping = 1

这篇关于将简单的物理应用于SceneKit XCODE SWIFT中的.scn对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆