Set permanent SKSpritenode image orientation - ios

Hi I have a bunch of round SKSpriteNodes with a circle physical body. Now when these balls roll down a path I want some of these SKSpritenodes image to stay upright even when rolling. So think of an arrow pointing upwards. When the ball starts rolling the arrow spins in circles. But for some balls Id like the arrow to remain pointing up even when the ball rolls. Whats the best way of doing this?
Edit
So an answer was given but from testing it turns out it is not the correct one. Not allowing the ball to rotate affects the way it rolls down the path. So I guess what I want is rotation to be on but the image to always appear to the user like its not rotating. Thanks.

This looks like a job for SupermSKConstraint. Constraints are evaluated and applied after the physics simulation runs on each frame, so you can use them for tasks like making a node point a certain direction regardless of what physics does to it. For this, you'd want a zRotation constraint.
But there's a bit more to it than that. If you set a zero-rotation constraint on the ball:
// Swift
let constraint = SKConstraint.zRotation(SKRange(constantValue: 0))
ball.constraints = [constraint]
You'll find that SpriteKit resets the physics body's transform every frame due to the constraint, so it only sort-of behaves like it's rolling. Probably not what you want. (To get a better idea what's going on here, try adding a zero-rotation constraint to a rectangular physics body in a world without gravity, applying an angular impulse to it, and watching it try to spin in a view with showsPhysics turned on. You'll see the sprite and its physics body get out of sync and shake a bit -- probably due to accumulated rounding errors as the physics engine and the constraint engine fight it out.)
Instead, you can do a bit of what's in 0x141E's answer, but use constraints to make it less code (and run more efficiently):
Give the ball node a circular physics body. (And possibly no texture, if the only art you want for the ball is a non-rotating sprite.)
Add the arrow node as a child of the ball node. (It doesn't need its own physics body.)
Put a zero-rotation constraint on the arrow.
Wait, that doesn't work -- I told the arrow to not rotate, but it's still spinning?! Remember that child nodes are positioned (and rotated and scaled) relative to their parent node. So the arrow isn't spinning relative to the ball, but the ball is spinning. Don't worry, you can still solve this with a constraint:
Tell the constraint to operate relative to the node containing the ball (probably the scene).
Now the constraint will keep the arrow in place while allowing the ball to rotate however the physics simulation wants it to.
Here's some test code to illustrate:
// Step 1: A rectangular spinner so we can see the rotation
// more easily than with a ball
let spinner = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 300, height: 20))
spinner.position.x = scene.frame.midX
spinner.position.y = scene.frame.midY
spinner.physicsBody = SKPhysicsBody(rectangleOfSize: spinner.size)
scene.addChild(spinner)
spinner.physicsBody?.applyAngularImpulse(0.1) // wheeeeee
// Step 2: Make the arrow a child of the spinner
let arrow = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width: 20, height: 50))
spinner.addChild(arrow)
// Step 3: Constrain the arrow's rotation...
let constraint = SKConstraint.zRotation(SKRange(constantValue: 0))
arrow.constraints = [constraint]
// Step 4: ...relative to the scene, instead of to its parent
constraint.referenceNode = scene

Here are two methods to create a ball with a physics body and an arrow:
Add an arrow as a child of a ball
Add both the ball and the arrow directly to the scene
Here's what will happen when you add the above to the SpriteKit simulation:
The arrow will rotate when the ball rotates
Both the arrow and the ball will move/rotate independently
If you want the arrow to rotate with the ball, choose Option 1. If you want the arrow to remain fixed, choose Option 2. If you choose Option 2, you will need to adjust the rotation of the arrow to ensure that it points upward. Here's an example of how to do that.
-(void)didMoveToView:(SKView *)view {
self.scaleMode = SKSceneScaleModeResizeFill;
/* Create an edge around the scene */
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:view.frame];
// Show outline of all physics bodies
self.view.showsPhysics = YES;
CGFloat radius = 16;
SKNode *balls = [SKNode node];
balls.name = #"balls";
[self addChild:balls];
// Create 5 balls with stationary arrows
for (int i = 0;i<5;i++) {
// Create a shape node with a circular physics body. If you are targeting iOS 8,
// you have other options to create circular node. You can also create an SKSpriteNode
// with a texture
SKShapeNode *ball = [SKShapeNode node];
// Create a CGPath that is centered
ball.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-radius,-radius,radius*2,radius*2)].CGPath;
ball.fillColor = [SKColor whiteColor];
ball.position = CGPointMake(100, 100+i*radius*2);
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
[balls addChild:ball];
// Create an arrow node
CGSize size = CGSizeMake(2, radius*2);
SKSpriteNode *arrow = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:size];
arrow.name = #"arrow";
arrow.position = CGPointZero;
[ball addChild:arrow];
// Apply angular impulse to the ball so it spins when it hits the floor
[ball.physicsBody applyAngularImpulse:-1];
}
}
- (void) didSimulatePhysics
{
SKNode *balls = [self childNodeWithName:#"balls"];
for (SKNode *ball in balls.children) {
SKNode *arrow = [ball childNodeWithName:#"arrow"];
arrow.zRotation = -ball.zRotation;
}
}

sprite.physicsBody.allowsRotation = NO;
The allowRotation property should control exactly what you are asking.

Related

SpriteNode from class not rotating correctly in Scene

I have a Class with a SpriteNode that rotates very wide, when rotated within the main Scene(as if the anchor point is in the middle of the screen and the Sprite is rotating around it). I want it to rotate around the anchor point of itself in the main Scene(anchor point on the Sprite).
So in the Class i have something like the following
- (void)createChainWithPos:(CGPoint)pos {
SKTexture *myTex...
SKTexture *myTex2...
SKSpriteNode *chainFront = [SKSpriteNode spriteWithTexture:myTex];
chainFront.position = pos;
chainFront.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:mytex.size];
[self addChild:chainFront];
[_chainParts addObject:chainFront];
SKSpriteNode *chainSide = [SKSpriteNode spriteWithTexture:myTex2];
chainSide.position = CGPointMake(chainFront.position.x, chainFront.position.y - chainSide.size.height + 6);
chainSide.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:myTex2.size;
[self addChild:chainSide];
[_chainParts addObject:chainSide];
}
I have an loop creating the chain parts in the main file but couldn't get it rotate so stripped it down in an new project. There is actually 4 chain parts but i only did two. The other two are just mirrors of the ones above with their positions mirroring the chainSide.(to position them in a chain like fashion)
and in the Scene
self.chain1 = [chain node];
[self.chain1 createChainWithPos:CGPointMake(self.size.width/2, self.size.height/2);
self.chain1.zRotation = 3.14/4;
[self addChild:self.chain1];
I have a NSMutableArray in the chain class header that i use to hold the chains.
the physics joints
for (int i = 1; i < self.chain1.chainParts.count; i++ {
SKSpriteNode *nodeA = [[self.chain1 chainParts]objectAtindex:i-1];
SKSpriteNode *nodeB = [[self.chain1 chainParts]objectAtindex:i];
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:nodeA.physicsBody
bodyB:nodeB.physicsBody
anchor:CGPointMake(CGRectGetMidX(nodeA.frame), CGRectGetMinY(nodeA.Frame))];
}
I found that if i set the position of the chain in the middle in the Class, it rotates correctly in the Scene. However, the physics joints just start moving randomly across the screen and isn't correct to the anchor points set. (the physics joints are set in the Scene)
I don't know if i have to convert the coordinates or play with random anchor point positions , but if someone could shed some light if would be greatly appreciated.
You do not want to use the nodes frame, you want to use the nodes size, the frame mid is the screen coordinate + the fitted node size / 2, you want the middle of your sprite only, also for pi / 4 use M_PI_4

SKEmitterNode targetNode path following is inverted

I created an SKEmitterNoder (standard Fire.sks template) & added it to an SKSpriteNode called ball. The thing is, I followed a tutorial of iOS Games by Tutorials & the way they taught me to create Tiled maps, is essentially flip them upon init so that (0, 0) would be in bottom left corner of the screen & NOT top left. I think this is affecting my SKEmitterNode targetNode property because as the ball is moving in the scene, the fire emitter node goes in the exact opposite direction. Could someone tell me what to do to change this? I can't change the world parameters, so I need something that would alter the SKEmitterNode trajectory so that it actually follows the ball node properly. Here's my current code:
NPCBall *ball = [[NPCBall alloc] initWithBallType:BallTypeRed andName:#"Ball Red"];
ball.position = CGPointMake(x + w/2, y + h/2);
ball.zPosition = -104.0;
[_entityNode addChild:ball];
//Create a random Vector.dx & dy for the NPC. This will apply a random impulse to kick start NPC non-stop movement.
[ball.physicsBody applyImpulse:CGVectorMake([self randomVectorGeneration].dx, [self randomVectorGeneration].dy)];
//Create a particle for the ball (fire).
SKEmitterNode *emitFire = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"Particle_Fire" ofType:#"sks"]];
emitFire.position = CGPointMake(ball.position.x, ball.position.y);
emitFire.targetNode = ball;
[_entityNode addChild:emitFire];
P.S. _entityNode, to which both the ball & SKEmitterNode fire are added, is an SKNode. Which in turn is a child of an SKNode called _worldNode. _worldNode is a child of self (which is an SKScene). This _worldNode is the one flipped to have (0, 0) coordinates begin at the bottom left.
A buddy of my'n has found a solution to this. I'm posting it here in case someone also has an inverted Tiled map where the (0, 0) coordinates are in the bottom left corner of the scene.
//Create a ball sprite.
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
ball.position = CGPointMake(_worldNode.frame.size.width/2, _worldNode.frame.size.height/2);
[_entityNode addChild:ball]; //<--_entityNode is also an SKNode. It's a child of _worldNode.
ball.zPosition = -104.0;
//Create a random Vector.dx & dy for the NPC. This will apply a random impulse to kick start NPC non-stop movement.
[ball.physicsBody applyImpulse:CGVectorMake([self randomVectorGeneration].dx, [self randomVectorGeneration].dy)];
//Create a particle effect for the ball.
SKEmitterNode *emitFire = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"Particle_Fire" ofType:#"sks"]];
emitFire.name = #"Fire Emitter";
emitFire.targetNode = _worldNode; //<-- This is the demon. Make the emitter follow this SKNode since it moves when the ball moves. Everything in my game is a child of _worldNode.
[ball addChild:emitFire]; //Add the emitter to the ball.

Sprite Kit - Changing SKShapeNode's anchor point doesn't change SKPhysicalBody position

I changed the anchor point of my SKSpriteNode, but it seems that the SKPhysicalBody is not correct.
Without setting the anchor point of my sprite, it would appear out of the screen by a little, using anchor point fixed this problem.
But after setting the anchor point, the physical body is still at the previous location (before changing the anchor point)
func addGround() {
let gSize = CGSizeMake(self.size.width/4*3, 120);
let ground = SKSpriteNode(color: SKColor.brownColor(), size: gSize);
ground.name = gName;
ground.anchorPoint = CGPointZero
ground.position = CGPointMake(0, 0);
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size);
ground.physicsBody.restitution = 0.0;
ground.physicsBody.friction = 0.0;
ground.physicsBody.angularDamping = 0.0;
ground.physicsBody.linearDamping = 0.0;
ground.physicsBody.allowsRotation = false;
ground.physicsBody.usesPreciseCollisionDetection = true; //accurate collision
ground.physicsBody.affectedByGravity = false;
ground.physicsBody.dynamic = false;
ground.physicsBody.categoryBitMask = gBitmask;
ground.physicsBody.collisionBitMask = pBitmask;
}
The box is drawn correctly, but enabling skView.showPhysics showed me that the physical body is still not correct.
The left side is the physics body for the box seen in the middle of the screen....
The physics body is diagonally down towards the left side.
The anchorPoint is the position of the texture relative to the node's position. In other words, changing the anchorPoint changes where the texture is drawn relative to the sprite's position.
As such, the physics body shape is unaffected by changes to the anchorPoint since the node's position remains unaffected.
Note that on 4" iPhones your ground body's size would be 426x120 extending from the bottom left corner of the screen (assuming the scene's position is 0,0).
Also note that there is a small threshold around collision shapes which prevent them from lining up exactly, there may be a small gap if you use the exact same size as the images. Therefore you may need to make the shapes a little bit smaller.

SKPhysicBody In the Wrong position

I am creating a simple Sprite Kit game however when i am adding the PhysicsBody to one of my sprites it seems to be going in the wrong position. i know that it is in the wrong position as i have have set
skView.showsPhysics = YES;
and it is showing up in the wrong position.
The Square in the bottom corner is the physics body for the first semicircle. I am using a square at the moment just for testing purposes.
My app includes view following and follows my main sprite when it moves. I implemented this by following apples documentation and creating a 'myworld' node and creating all other nodes from that node.
myWorld = [SKNode node];
[self addChild:myWorld];
semicircle = [SKSpriteNode spriteNodeWithImageNamed:#"SEMICRICLE.png"];
semicircle.size = CGSizeMake(semicircle.frame.size.width/10, semicircle.frame.size.height/10);
semicircle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:semicircle.frame.size];
semicircle.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
semicircle.physicsBody.dynamic = YES;
semicircle.physicsBody.collisionBitMask = 0;
semicircle.name = #"semicircle";
[myWorld addChild:semicircle];
To centre on the node I call these methods
- (void)didSimulatePhysics
{
[self centerOnNode: [self childNodeWithName: #"//mainball"]];
}
- (void) centerOnNode: (SKNode *) node
{
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
I don't know if the my world thing makes any difference to the SkPhysics body...
SKPhysicsBody starts at coordinates 0,0 which is at the bottom left hand corner. If you make the area smaller, as you did by width/10 and height/10, you decrease the size but from the bottom left.
I think what you are looking for is bodyWithRectangleOfSize:center: which allows you to manually set the center from which you base your physics body area on.
Update:
Based on what I understand, your smallest semi circle pic size is the same as the screen size. I would suggest you modify the image size to something like the example I have. You can then set the sprite's position as required and set the physics body to the half of the image containing your semi circle.
Your centerOnNode call should be put in the didEvaluateActions function instead of the didSimulatePhysics function. This is because you need to move the world before the physics are drawn so that they stay in sync. Similar question found here: https://stackoverflow.com/a/24804793/5062806

SpriteKit Physics: Ignore when on different z plane

With SpriteKit, is it possible to have two nodes that collide with one another, but then when another node is introduced, have how the physics works change?
For example, consider the following 3 nodes:
- Object
- Object_Hole
- Player
So, a scene containing a Player (orange circle) and an Object (blue rect):
In the above scene, the Player (circle) would be affected by gravity and collide with the Object (square). I know how this works by using the physics body, category and collision bitmasks.
Now, introduce the Object_Hole (green square):
In the above scene, the Object_Hole (green square) overlays the Object (blue rect) - this can be on either the same z plane or a higher plane.
Is it possible to make the physicsBody on the Player (circle) not collide with the Object in the area that the Object_Hole is?
If there's a better way to achieve this than adding an overlaying Node, please let me know. Otherwise, is it possible?
Thanks!
You could look into using categoryBitMask and collisionBitMask:
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/Reference/Reference.html#//apple_ref/occ/instp/SKPhysicsBody/collisionBitMask
If you want your ball to collide with the object, but not some hole in the object, you will probably want to break your object into 2 pieces with an actual hole in the middle. Set the hole sprite's categoryBitMask and collisionBitMask to be completely different from those of the object and ball.
// sprite1 and sprite2 will never collide.
SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(20.0f, 10.0f)];
sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite1.physicsBody.collisionBitMask = 0xffff0000;
sprite1.physicsBody.categoryBitMask = 0xffff0000;
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(20.0f, 10.0f)];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite2.physicsBody.collisionBitMask = 0x0000ffff;
sprite2.physicsBody.categoryBitMask = 0x0000ffff;
Create the blue bar as 2 separate objects, with a gap between them of 0 points (or even overlapping slightly in case the point where they touch causes odd physics reactions) when the 'hole' isn't around, but with a 'hole-sized' gap between them when the hole is preset.

Resources