SpriteKit categoryBitMask issue - ios

I'm new to Spritekit, and I'm having some trouble with something simple.
I'm making a pinball game. In order to detect collisions and award points with the ball, the bumpers have the following set:
categoryBitMask
collisionBitMask
contactTestBitMask
However, once I set categoryBitMask, the object no longer acts the same way. The ball passes through the bumper.
I've tried to set the properties in code to mirror what is set in the SKS file:
physicsBody?.isDynamic
physicsBody?.affectedByGravity
physicsBody?.allowsRotation
physicsBody?.pinned
physicsBody?.mass
But this doesn't make any difference.
How can I make a SKSpriteNode maintain it's physical properties after setting the categoryBitMask?

I found the issue. I was setting the collision on one object but not the other
ball.collisionBitMask = ...contains bumper
ball.contactTestBitMask = ... contains bumper
but not the bumper:
bumper.collisionBitMask = ...didn't contain ball
bumper.contactTestBitMask = ...didn't contain ball

Related

SKReferenceNode's SKPhysicsBody Off Center and Invisible

I am working on a game, and I am using an alpha mask to create an SKPhysicsBody for the basket + catcher. The SKView has showPhysics set to true.
I can't figure out what in the world the disc is hitting. There is no physics field there. When I recreate the reference node with SKSpriteNodes this works flawlessly. I would like to use a reference node though because it would make it easier to make lots of these quickly. Here is where I set up the reference node:
Here is where I create the scene itself:
I have zero idea at this point what is happening. Clearly there is an error with showing the physics as well, because there is no blue outline there.

What's the relationship between physicsbody.dynamic and physicsbody.categorybitmask

Recently I have found that some sprite nodes can't test the collision with others. After some days trying and googling finally I made them works as usual. I have realized that there seems to be some relations between the dynamic and category bit mask. My guess is that:
player.dynamic = false
enemy.dynamic = true
player.category = player
enemy.collision = player
In the conditions above, enemy can't test the collision with player, but I wanna to know more details, any help will be appreciated.
update:
In my game, I want some of the sprites fixed in the scene, whose dynamic should be set to false, but can be collided by the players or enemies. For example the ground, a tree, or some buildings. What should I do to deal with these properties correctly ?
Like
tree.dynamic = false
enemy.collision = tree //can't works
tree.collision = enemy //should I do this? Is there another way to do this?
Non-dynamic bodies don't collide. From Apple's docs:
The dynamic property controls whether a volume-based body is affected
by gravity, friction, collisions with other objects, and forces or
impulses you directly apply to the object.

Make a certain sprite ignore SKPhysicsBody

I am having an issue making a sprite ignore the effects of a SKPhysicsBody placed as a ground platform in an iOS project. I have only one sprite that I would like to "follow the rules" of the SKPhysicsBody and for all other sprites to "pass through". I have searched for a way to do this, but to no avail. Any suggestions?
for any physics body that you want to pass through set its collisionBitMask to 0 that means that it won't collide with anything, make sure you don't set the collisionBitMask of the ground to the type of sprite you want them to pass through,

How to make a specific object defy gravity? (SpriteKit / Objective-C)

I'm working on a piece of code, and I want there to be gravity in the view but I want specific projectiles to defy gravity and fly across the screen. I know to get rid of the gravity on the whole view its just:
self.physicsWorld.gravity = CGVectorMake(0, 0);
But as stated I want gravity on the scene.
So I'm wondering if there is a way to take the gravity off one specific item? (i.e. the SKSpriteNode _debris item in my case)
By setting the physicsBody to not be affected by gravity.
E.g.
myNoGravityObject.physicsBody.affectedByGravity = NO;
See the documentation of SKPhysicsBody.
If you don't want a node to interact with physics calculations at all, but still want it to have a physicsBody (i.e. to make it begin to interact with things later on, or to check for collisions with other nodes), you can set
node.physicsBody.dynamic = NO;
This will cause the node to ignore gravity, as well as collisions, impulses, and the like. If you are setting up the contact delegate, note that at least one node in any given contact must be dynamic for the contact delegate to be notified of the event.

How to draw SKNode PhysicsBody for debugging?

I create my SKPhysicsBody with rectangle from size, I set it to size of the node, but the collision detection does not work on good portion of the node.
How do I draw the red frame around physics body?
I tried using precise collision detection, but it doesn't help.
I have checked some debug library but it draws only direct descendants of the scene, and my nodes are few levels deep. Author apparently doesn't know what recursion is.
How do I draw red frame around physics body if my body is rectangle?
This works as of iOS 7.1 and in all OSX versions supporting SpriteKit.
In this example I'm using the variable mySKView to hold the SKView of the game.
Set the "showPhysics" property of your SKView to true/YES. This is usually done by adding the following line after the line "mySKView.showsFPS = true". Usually in the viewcontroller owning the SKView.
Obj-C:
mySKView.showsPhysics = YES;
Swift
mySKView.showsPhysics = true

Resources