gpt4 book ai didi

ios - 从 .dae 加载 SCNScene 时的物理问题

转载 作者:行者123 更新时间:2023-11-28 18:33:03 27 4
gpt4 key购买 nike

我正在尝试加载目前只有 3 面墙、天花板和地板的场景。我正在加载我在 blender 中创建的场景并正常加载它。但是,具有 SCNBox 几何形状的 SCNNode 会直接掉落。该盒子附有一个动态物理体,我手动将 walls/floor 设置为静态节点。下面是我用来设置场景和添加框的代码,如果需要,我也可以发布我的 .dae。任何人对可能发生的事情有任何想法吗?

//Load the scene from file
SCNScene *scene = [SCNScene sceneNamed:@"mainScene.dar"];

//Get each node in the scene, and give it a static physics bodt
for (SCNNode *node in [[scene rootNode] childNodes]) {
SCNPhysicsBody *staticBody = [SCNPhysicsBody staticBody];
staticBody.restitution = 1.0;
node.presentationNode.physicsBody = staticBody;
NSLog(@"node.name %@",node.name);
}

//Create box
SCNNode *block = [SCNNode node];
block.position = SCNVector3Make(0, 0, 3);

//Set up the geometry
block.geometry = [SCNBox boxWithWidth:.8 height:.8 length:.8 chamferRadius:0.05];
block.geometry.firstMaterial.diffuse.mipFilter = SCNFilterModeLinear;
block.castsShadow = YES;


//Make it blue
for (SCNMaterial *mat in block.geometry.materials) {
mat.emission.contents = [UIColor blueColor];
}


//Add physics body
SCNPhysicsBody *body = [SCNPhysicsBody staticBody];
body.mass = 5;
body.restitution = .7;
body.friction = 0.5;
block.physicsBody = body;

//Add the node to the scene
[[scene rootNode] addChildNode:block];

为了回应 ricksters 的回答,我尝试为每个新节点创建自定义几何体,但我的盒子仍然掉落。这是我用于自定义几何的代码。这将替换原始代码中的 for-in。

//Get each node in the scene, and give it a static physics bodt
for (SCNNode *node in [[scene rootNode] childNodes]) {
SCNGeometry *geometry = [SCNBox boxWithWidth:node.scale.x height:node.scale.y length:node.scale.z chamferRadius:0.0];
SCNPhysicsShape *physicsShape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
SCNPhysicsBody *staticBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:physicsShape];
staticBody.restitution = 1.0;
node.physicsBody = staticBody;
}

最佳答案

所以我遇到了类似的问题。我发现问题源于文件是如何在 3d 建模软件中创建的。我正在使用 Blender 对此进行测试;我用盒子做了一个飞机,给盒子加了一个物理体,飞机和盒子掉了下来。我意识到这与规模有关。在 Blender 中,我应用了对象变换,通过按 CTRL A 并选择缩放选项将缩放重置为 1.0 1.0 1.0。所以最终似乎正在发生的是 SceneKit 使用基础几何体而忽略了几何体的变换。您在屏幕上看到的是应用了节点变换的基础几何体。在导出 Collada 文件之前将转换设置为标识,您应该已设置好。

关于ios - 从 .dae 加载 SCNScene 时的物理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com