SCNNode static body with .dae causing issues - ios

I have built a landscape model in blender, exported to .dae and added to my xcode project.
I have loaded the scene then attached the child (landscape grid mesh) to my landscapeNode, this loads perfectly.
However when I attach a static physics body to the landscspeNode my heroNode seems to crash into a invisible wall when attempting to fly above the land.
The functionality I am looking for is the obvious collision with the land I have modelled so the heroNode cannot fly through the land and is forced to move around it.
Note: I did not do any converting of the y axis in blender nor xcode just simply rotated the node 90degrees about the -x axis.
Edit: code i've attempted to add physics shape
landscapeNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:[SCNPhysicsShape shapeWithNode: [landscapeScene.rootNode childNodeWithName:#"Grid" recursively:NO] options:#{SCNPhysicsShapeTypeKey:SCNPhysicsShapeTypeConcavePolyhedron}]];
landscapeNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:[SCNPhysicsShape shapeWithNode: landscapeNode options:#{SCNPhysicsShapeTypeKey:SCNPhysicsShapeTypeConcavePolyhedron}]];
landscapeNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:[SCNPhysicsShape shapeWithGeometry: landscapeNode.geometry options:#{SCNPhysicsShapeTypeKey:SCNPhysicsShapeTypeConcavePolyhedron}]];

If you don't specify additional options when creating a physics body for a node, what you get is a convex hull encompassing that node's geometry.
Because your landscape is a static body, you can make it use a concave shape that more closely approximates the geometry. (Note that this works only for static bodies.) To do this, you'll need to:
Use the bodyWithShape:type: method to create the body, not staticBody.
For that method you need to pass an SCNPhysicsShape. Create one with shapeWithGeometry:options: or shapeWithNode:options:.
For the options parameter, pass a dictionary containing the key SCNPhysicsShapeTypeKey and corresponding value SCNPhysicsShapeTypeConcavePolyhedron.

Related

SCNPhysicsShape(shapes:transforms:) creates MULTIPLE shapes?

My goal is to create a single physics body out of several of SceneKit's SCNBox geometries.
My understanding is that when I pass an SCNPhysicsShape(shapes:transforms:) to an SCNPhysicsBody(type:shape:), it should create a single physics body.
However, I end up with something that seems suspiciously like it's not a single physics body at all, but rather several separate physics bodies -- one for each shape I passed into SCNPhysicsShape(shapes:transforms:).
When I turn on scnView.debugOptions = .showPhysicsShapes, I can clearly see red lines defining the separate bodies in question. On its own, this isn't very convincing evidence (it's conceivable that those lines could be shown for whatever reason while still being a single physics body).
But there's another piece of data, here: The project in which I'm encountering this issue features a small ball that rolls around the scene -- and when that ball rolls over the red lines in question, the ball bounces up into the air a bit. So, it's quite obvious that, whatever is actually going on, there are edges where I would expect to see none.
This behavior is clearly visible in the following GIF. In it, each colored block is a separate SCNBox geometry with its own physics body. Each block has the exact same position.z. The ball bounces considerably as it crosses the point where one geometry meets another.
Here's some code illustrating the issue. parent is an SCNNode that holds the child nodes, and is the node to which I assign the physics body. Please assume that all properties are defined; I'm omitting things that aren't terribly relevant.
let childShape1 = SCNBox(width: 6, height: 2, length: 6, chamferRadius: 0.0)
//Other child shapes defined here...
//Set up the positional translation relative to the child node's parent:
let translateMatrixShape1 = SCNMatrix4MakeTranslation(childShape1.position.x, childShape1.position.y, childShape1.position.z)
//Other child translations defined here...
let parentShape = SCNPhysicsShape(shapes: [childShape1, childShape2, childShape3, childShape4], transforms: [translateMatrixShape1, translateMatrixShape2, translateMatrixShape3, translateMatrixShape4])
parent.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.static, shape: parentShape)
Now, parentShape is four rectangular boxes arranged around a central point, creating a sort of picture-frame-shaped object.
The ball is an SCNNode with an SCNSphere geometry and a dynamic physics body.
Question: Does anyone have any idea what might be going on, here? Have I somehow misunderstood how this whole thing works, or is this a limitation of SceneKit?
Usually you can create one single PhysicsBody from several geometry objects by making a flattenendClone from out of your (i.Ex.) parent node. This new node will have one single geometry then. For the PhysicsBody you can then use the geometry element of the new node. In Addition I recoommend to use a concavePolyhedron for the static body type. (I hope I understood you correctly)

How can I export a simple rigged model from Maya for use in Scenekit?

I am attempting to experiment with Apple’s Fox game SceneKit example (link below) by adding a model with a simple animation like the ‘panda.scn’ and ‘walk.scn’ assets.
I can create a static model with no joints or animation that works: e.g. In Maya (2017) I add a simple sphere, export selection to FBX_DAE (COLLADA) file, drag it into the project in XCode and convert it to a SCN file. I can then drag that model into the ‘level.scn’, position and scale it as I’d expect.
However as soon as I add any animation or joints to my model I lose the ability to position and scale the model in XCode.
In Maya I add two joints to my sphere select the sphere and joints and export as above. When I examine the model SCN in XCode (either in isolation or as a reference within another scene) I find that I cannot apply any translation or scaling. XCode lets me move the xyz locators in the GUI and update scale but the model does not change. I can see the mesh and joints in the outline view and I have tried moving the joints instead of the mesh, but they do nothing. I have even tried ignoring the GUI and positioning the model in code just as the panda character is set up, but applying positions or transforms to the node does nothing - it always appears at the origin and default scale.
Ignoring that for the moment, my understanding from looking at the ‘walk.scn’ file is that an animation is just an export of the joints with keyframes. I have tried to reproduce that by exporting only the joints to a separate DAE and importing it, then applying that in code as they do with the ‘walkAnimation’. This seems to do nothing as well.
I have experimented with various settings of the FBX_DAE export dialog including baking animations (By the way - what is the difference between baking an animation on export vs baking it in Maya before export? The former seems to do something and the other does nothing in my tests.)
I would dearly love a workflow for creating some simple character animations in Maya and getting them into SceneKit. Any help is greatly appreciated.
For reference:
Apple’s source: https://developer.apple.com/library/content/samplecode/Fox/Introduction/Intro.html
Maya scene for my trivial ball model with two joints:
http://pat.net/misc/ball1.mb
and the DAE output from Maya for that:
http://pat.net/misc/ball1.dae
UPDATE:
Mnuages's answer appears to be correct in that when I added a parent control over the geometry and joints SceneKit then allowed me to move them. But even after brushing up on my understanding of how these nodes and their transforms relate in Maya I do not feel that I have a real understanding of how SceneKit is interpreting them. (Would love to read some docs on that would illuminate this more if they exist).
UPDATE #2:
I was finally able to create an animation by doing the following: 1) Export either the full scene or just the joint with the animation being sure to select the "bake animation" option in the DAE export dialog or bake the entire animation using Key->Bake Animation. 2) It only works if I load the DAE file in scenekit instead of converting it to an SCN. Converting to SCN format seems to lose the animation.
what happens is that the node named joint1 is animated by the joint1-anim animation. So even if you move joint1 in the editor, what you see on screen is the result after the animation is evaluated.
If you create an intermediate node, say joint1-parent, and make joint1 as child node of joint1-parent, then you'll be able to translate and rotate joint1-parent freely and see the effects on joint1.
As for why moving pSphere does not change anything, it's the same idea. Just like the animation overrides the position, the skeleton will reposition the mesh.

SpriteKit : enumerateBodiesAtPoint not showing correct bodies

I have a few non-completed-circles rotating constantly and a user going from circle to circle.
I removed all gravity, forces etc from my scene
Image A
Image B
Problem : I am trying to do a hit detection where I just check where the user is, and if there are SKNode's bodies at this point in the physics world of my scene. If it's a hit with the shape, the user can continue (image A), but fails if he is outside (image B)
Although the shapes are pretty complex, the scene.showPhysics seem to match my shapes precisely. (see image A and B)
let updatedOrigin = user.calculateAccumulatedFrame().origin
user.scene?.physicsWorld.enumerateBodiesAtPoint(updatedOrigin, usingBlock: { (body, stop) in
print("🍄 Shape contains \(body.node!.name)")
})
which prints
🍄 Shape contains Optional("User")
🍄 Shape contains Optional("circle")
🍄 Shape contains Optional("circle")
🍄 Shape contains Optional("circle")
🍄 Shape contains Optional("circle")
🍄 Shape contains Optional("Scene")
It prints the user and scene correctly, but also prints all the circle's shapes around, when there should only be one at this point, or none. The nodes are there, but the bodies physics should not hit.
Any ideas why it shows a hit for all those circles when it should only match 1 or none? Thanks!
Edit : additional info
- I had similar results when using user.physicsBody?.allContactedBodies()
- I am using a CGPath to create the PhysicsBody of my rotating node
I created a simple test project with a scene containing 3 arcs with physics bodies and 3 rectangle shape-nodes that identify the bounding box for each arc. I then added a touch handler that places a small circle at the tap point and a label that identifies the nodes returned by enumerateBodiesAtPoint with the touch location as the parameter. The figure below shows the results of tapping at various locations in the scene.
From the test results, it's not obvious how enumerateBodiesAtPoint determines if a physics body contains the specified point or not. It is clear, though, that the method is not consistent with its documentation. I suggest that you avoid using it in your app.
Alternatively, you can use SpriteKit's built-in contact detection:
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
}
func didBeginContact(contact: SKPhysicsContact) {
// Handle contacts between physics bodies here
}
}
You can also test if a point is within a CGPath using CGPathContainsPoint. Here, you will need to iterate over the paths you used to create the arc-shaped physics bodies. The below figure shows the result of my test project that uses CGPathContainsPoint instead of enumerateBodiesAtPoint. You may need to convert the test point, with convertPoint, to the appropriate coordinate space prior passing it to CGPathContainsPoint.

Detecting touch inside bounding box (alpha mask) of Sprite using SpriteKit

I am starting to learn Swift2 to develop on iOS using SpriteKit and I cannot seem to detect if I touched a visible part of my Sprite.
I can find which node was touched like this:
let touchedNode = self.nodeAtPoint(location)
But this detect the touch on the sprite. I would like to detect the touch on the "not transparent" part of the sprite only.
I tried creating a PhysicsBody with an alpha mask bounding box and testing if the bounding box of the node I selected contains the location of the touch like this:
sprite.physicsBody = SKPhysicsBody.init(texture: sprite.texture!, size: sprite.size)
sprite.physicsBody?.affectedByGravity = false
if (touchedNode.containsPoint(location)){
But it didn't help. If I click in a transparent part of my sprite, the event still triggers.
The documentation says "A new physics body is created that includes all of the texels in the texture that have a nonzero alpha value.", so shouldn't it work?
Thanks for your time.
PS: I also tried to be more generous on my alpha threshold, but this also did not work (in case my transparency wasn't perfect).
UPDATE:
To add a little more details, I am building a level editor. This means that I will create many nodes, depending on what the user chooses, and I need to be able to select/move/rotate/etc those nodes. I am actually using SKSpriteNodes as my PNG pictures are automatically added in xcassette that way. I decided to use the PhysicsBody's Alpha Mask bounding box, as this value is shown in the Scene Editor (Xcode) when you select a node, and when selected, the Alpha Mask highlights exactly the part of my Sprite that I want to be able to detect a touch inside.
If I am using the wrong ideas/techniques, please tell me. This is possible as I am only starting to use Swift and SpriteKit.
UPDATE 2
I queried the physicsWord (as recommended) to get the right physicsBody and got the name of the attached node like this:
let body = self.physicsWorld.bodyAtPoint(location)
print(body?.node?.name)
But this is still printing the name of the node even if I touch outside the bounding box, which makes no sense to me.
Thank you anyway for your help.
The SKPhysicsBody doesn't define the bounds of the SKNode. You can do the hit check by querying the SKPhysicsWorld of your scene by calling bodyAtPoint on it. That will return the SKPhysicsBody you are interested in.
I was having the same problem. You actually can't perform the touch on the alpha mask of a Sprite Node and it says it right in the Apple documentation. You have to use a Composite Box. I used a shape Node and changed the "line width" to 0 so essentially the composite box is invisible. Make sure that the Z axis is higher for the composite box and then run the touch event through the composite box shape node instead of the original node.

Best way to add a SKPhysicsJoint to an object

So I have a marionette I am making in Sprite Kit, and placing him in a scene. I have created a Marionette class, which is a subclass of SKNode, and when I create an instance of this class, I add a number of SKSpriteNodes to each other. This should give you an idea of how it works (I've snipped out the unnecessary lines):
[self addChild:self.head];
[self.head addChild:self.chest];
[self.chest addChild:self.leftLeg];
[self.chest addChild:self.rightLeg];
[self.leftLeg addChild:self.leftFoot];
[self.rightLeg addChild:self.rightFoot];
So, after doing this, I have my marionette showing up. I want to add a SKPhysicsJointPin to each of these connections. Code looks like this:
self.chestPin = [SKPhysicsJointPin jointWithBodyA:self.marionette.head.physicsBody bodyB:self.marionette.chest.physicsBody anchor:self.marionette.chest.anchorPoint];
At first I thought I could just set up all the pins inside the Marionette class, but that doesn't work (I get EXC_BAD_ACCESS if I recall). It turns out that I have to add the Marionette to the overall SKScene before I add the joints.
This make sense I guess, but I can't help thinking there should be some way I could just set everything up in the Marionette class, and then stick 2 lines of code in the scene to create a new Marionette, then addChild him to the Scene.
As it is, my SKScene subclass has a bunch of code now to add all the pins, and it doesn't feel like it fits in the Scene. Thoughts?
You don't want to use anchorPoint as anchor. The anchorPoint property is a factor which ranges from 0 to 1 and affects how the texture is offset from the sprite's position, ie the default 0.5/0.5 centers the sprite's texture on the sprite's position. In other words anchorPoint is not a position, and using that will anchor the body & head joint to the lower left corner of the screen.
Instead try using either chest or head's position as anchor, or the point in between the two.

Resources