从 .dae 加载 SCNScene 时的物理问题 [英] Physics Issue When Loading SCNScene from .dae

查看:59
本文介绍了从 .dae 加载 SCNScene 时的物理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to load a scene that is currently just 3 walls, a ceiling and a floor. I am loading the scene that I created in blender and load it in fine. However, a SCNNode with a geometry of a SCNBox just falls right through. The box has a dynamic physics body attached to it and the I am manually setting the walls/floor to be static nodes. Below is the code I am using to set up the scene and add the box, I can also post my .dae if needed. Anyone have any ideas as to what might be going on?

//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.

In response to ricksters answer I tried to create custom geometry for each new node, but my box still falls through. Here is the code that I am using for the custom geometry. This replaces the for-in in the original code.

//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 文件之前将转换设置为身份,您应该已设置.

So I had a similar issue. I found that the issue stemmed from how the file was created in the 3d modeling software. I was using Blender to test this; I made a plane with a box, added a physics body to the box and the plane and the box fell through. I realized this had to do with the scale. In Blender, I applied the object transformation which reset the scale to 1.0 1.0 1.0 by pressing CTRL A and selecting the scale option. So ultimately what seems to be happening is that SceneKit uses the base geometry ignoring the transform of the geometry. What you are seeing on the screen is the base geometry with the node's transform applied. Set the transform to identity before exporting the Collada file and you should be set.

这篇关于从 .dae 加载 SCNScene 时的物理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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