Physics issue when loading SCNScene from .dae

I am trying to load a scene that is currently only 3 walls, ceiling and floor. I load the scene created in blender and load it ok. However, a SCNNode

with geometry a SCNBox

just falls straight. The box has a dynamic physics element attached to it and I manually set walls/floor

as static nodes. Below is the code I am using to set up the scene and add the drawer. I can also post mine .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];

      

In response to the riksters answer, I tried to create custom geometry for each new node, but my window still fails. Here is the code I'm using for custom geometry. This replaces the input in the source 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;
}

      

0


source to share


2 answers


So I had a similar problem. I found that the problem was with the way the file was created in the 3D modeling software. I used Blender to test this; I made an airplane with a box, added a physical body to the box, and the airplane and the box failed. I realized it had something to do with the scale. In Blender, I applied an object transform that reset the scale to 1.0 1.0 1.0 by pressing CTRL A and selecting the scale option. So ultimately it looks like SceneKit is using the underlying geometry, ignoring the geometry transformation. What you see on screen is the underlying geometry with the node transform applied. Before exporting the Collada file, set the mapping to id.



+4


source


Depending on how the geometry for these nodes is built into the DAE, SceneKit may not be able to automatically create a physical shape that does what you want.



Use instead bodyWithType:shape:

to create exactly the conflict shape you want, separate from the visible geometry of the node. It is best to create physical shapes from parametric geometry (e.g. SCNBox

, SCNSphere

).

0


source







All Articles