How to draw SKNode PhysicsBody for debugging? - ios

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

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.

Rotate nodes in SpriteKit before the scene displays

I'm creating a game where there is a split screen view for 2 players.
The game is for an iPad and the the split screen will work in such way that each player will see the nodes right-side-up.
The way that the app works is that the upper half of the game will have all nodes rotated upside-down.
The problem I have is that when the scene is being loaded, none of the actions are being executed before the scene is fully in view.
For an instance, I'm using the following command to rotate one of the labels:
[self.player1Score runAction:[SKAction rotateByAngle:UP_SIDE_DOWN duration:0]];
The problem is that the rotation won't take place until the scene animation is done - which will make it look very strange as the nodes will rotate immediately once the scene is fully in view (I could animate them, but I prefer they load up already rotated properly).
I tried running this from both initWithSize and didMoveToView but the results were the same.
My only option at the moment is for the Textures to be duplicated and rotated using Photoshop - but I'd rather to have the app "lighter" and not have unnecessary graphics if I can help it.
Any suggestions?
Thanks in advance!
Why don't u set SKNode's zRotation property to set the rotation as a the node is created, instead of rotating them using SKAction.
SKNode *node = [SKNode node];
node.zRotation = -M_PI/2.0;

Gravity with UIview

I drew a circle in UIView with a help of UIBezierPath. Then I added gravity behavior to that view using UIGravityBehavior and collision behavior using UICollisionBehavior. When the circle collides with other objects this view bumps as square, but I want to work with this view in collisions as with a circle. How can I do it?
You can’t use UIKit Dynamics to simulate a circle. It works with rectangular bodies only. You can add collision paths, but I think that is only for the reference frame. I recommend looking at SpriteKit’s SKPhysicsBody. Unfortunately, I haven’t used it, so I can’t provide a code sample.

Does SpriteKit apply physics if I modify the position and rotation of a SKSpriteNode directly without an SKAction?

For example if I want to do my own custom animation and move an SKSpriteNode every frame programmatically by x += 10, will Sprite Kit still apply physics correctly or must I always use SKAction?
Manually moving a node with a physics body is possible regardless of how or when you do it. But in any case it's not recommended since it can adversely affect the physics simulation. The node (view) could be out of sync with the position of the body for 1 frame, and you might move the body into a collision which the physics engine will forcibly resolve, causing jumps, jitter, exploding velocities or skipping collisions.
When you use physics, stick to moving the physics body through force, impulse or changing the body velocity directly.
No, you don't have to use SKAction to move SKSpriteNode. This approach works fine for me:
- (void)update:(CFTimeInterval)currentTime {
myNode.position = CGPointMake(myNode.position.x + 0.1,myNode.position.y);
}
All physics work as expected

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