IOS Sprite Kit 2 Objects collide and stay put - sprite-kit

I want objects of the same category to not bounce off or be moved when they collide with one another. I've tried playing around with collisionBitMask, contactTestBitMask, mass, density, etc. Does anyone know how to do this?
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.allowsRotation = NO;
self.physicsBody.categoryBitMask = CollisionTypeRock;
self.physicsBody.collisionBitMask = CollisionTypeRock;
self.physicsBody.contactTestBitMask = CollisionTypeRock;
Thanks.

Related

SpriteKit bodyWithTexture gap between sprites when colliding

I'm new to SpriteKit and I'm trying to create a game, where blocks would fall from top of the screen and land on the bottom of the screen or on top of another block. Here is the sample code from GameScene.m:
- (void)didMoveToView:(SKView *)view
{
self.size = CGSizeMake(view.frame.size.width, view.frame.size.height);
self.backgroundColor = [UIColor whiteColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
SKSpriteNode *redRect = [SKSpriteNode spriteNodeWithImageNamed:#"RedRect"];
redRect.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height);
redRect.physicsBody = [SKPhysicsBody bodyWithTexture:redRect.texture size:redRect.texture.size];
redRect.physicsBody.affectedByGravity = YES;
SKSpriteNode *blueRect = [SKSpriteNode spriteNodeWithImageNamed:#"BlueRect"];
blueRect.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height);
blueRect.physicsBody = [SKPhysicsBody bodyWithTexture:blueRect.texture size:blueRect.texture.size];
blueRect.physicsBody.affectedByGravity = YES;
[self addChild:redRect];
[self addChild:blueRect];
}
As you can see on the screenshot, there is a gap between the two blocks and between the red block and the ground. This only happens when rects collide with each other or with the ground. For instance, if I use the SKAction moveToY: which moves the block to the bottom of the screen, the gap disappears. How can I get rid of these gaps when the nodes collide?
I am totally sure that the problem is about your texture images, They are not precisely cropped or even created.
Spritekit engine never creates gaps between nodes.

How to create a pendulum with spritekit

I'm trying to create a pendalum in spritekit but i cannot figure out how to make the pendalum swing. So far the code is:
transparentpixel = [SKSpriteNode spriteNodeWithImageNamed:#"transparent_pixel"];
[transparentpixel setPosition:CGPointMake(150,150)];
transparentpixel.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, 1)];
transparentpixel.physicsBody.dynamic = YES;
[_bg addChild:transparentpixel];
pendalum = [SKSpriteNode spriteNodeWithImageNamed:#"pendalum_image"];
[pendalum setPosition:CGPointMake(150,120)];
pendalum.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pendalum.size];
pendalum.physicsBody.dynamic = YES;
[_bg addChild:pendalum];
CGPoint pendalumjointanchor = CGPointMake(150,150);
SKPhysicsJointPin *pendalumJointPin = [SKPhysicsJointPin jointWithBodyA:transparentpixel.physicsBody bodyB:pendalum.physicsBody anchor:pendalumjointanchor];
[self.physicsWorld addJoint:pendalumJointPin];
I can't figure out how to make the pendalum swing indefinetely.
You can create physics bodies on each side of the pendulum around where the pendulum would reach it's max height.
Set an event to trigger when the pendulum hits these physics bodies. In this event, Apply a downward force.
After playing with the physics bodies locations and the amount of force, you should be able to achieve something very similar to the effect you are after.

Apply Angular Impulse

Here is my code for the player and the ball that interact with each other. What I want to do is to apply force to the ball like if my player is shooting it. I want the ball to move away from my player with force. how can I apply Impulse or force to this. I have tried many times but I am a newbie with Sprite Kit.
- (void) Player {
_Player = [SKSpriteNode spriteNodeWithImageNamed:#"player1"];
_Player.xScale = 0.09;
_Player.yScale = 0.09;
_Player.position = CGPointMake(self.size.width/4, self.size.height/2);
_Player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_Player.size];
_Player.physicsBody.dynamic = NO;
[self addChild:_Player];
}
- (void) TheMethodForBall {
SKSpriteNode *sprites = [SKSpriteNode spriteNodeWithImageNamed:#"ball"];
sprites.xScale = 0.19;
sprites.yScale = 0.19;
sprites.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprites.size];
sprites.physicsBody.dynamic = YES;
sprites.physicsBody.affectedByGravity = NO;
sprites.physicsBody.allowsRotation = YES;
sprites.physicsBody.restitution = YES;
sprites.physicsBody.angularVelocity = 4;
sprites.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:sprites];
}
I think you want to apply an impulse like a kick ?
You need the following, maybe when you touch the screen / or a button
[_myBall.physicsBody applyImpulse:CGVectorMake(somePowerX, somePowerY)];
Here is another post that will help get you started
Also, this is a good tutorial for beginners.

XCode Spritekit, collision with non-transparent sections of an png

I have the same problem like here: SKSpriteKit, detect non-transparency parts
My post is deleted from there. I don't know why, in other forums, THIS is a double post!!!
But ok, if you like that. Poor danny who still have not the answer!
Ok well, here comes the question again:
There are two png files with transparent areas. Like in the post of danny huang.
If they collide the "-(void)didBeginContact:(SKPhysicsContact *)contact" method fire.
But what we looking for is that they just collied with the non-transparent areas of the image.
Here is a small code snipped:
...
SKTexture *nyanTexture1 = [SKTexture textureWithImageNamed:#"test.png"];
nyanTexture1.filteringMode = SKTextureFilteringNearest;
nyan = [SKSpriteNode spriteNodeWithTexture:nyanTexture1];
nyan.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:nyan.size.height / 2];
nyan.physicsBody.dynamic = YES;
nyan.physicsBody.allowsRotation = NO;
...
nyan.physicsBody.categoryBitMask = nyanKategorie;
nyan.physicsBody.collisionBitMask = worldKategorie | tunnelKategorie;
nyan.physicsBody.contactTestBitMask = worldKategorie | tunnelKategorie;
...
WasserFallTexture1 = [SKTexture textureWithImageNamed:#"Wasserstrahl1.png"];
SKSpriteNode* tunnel1 = [SKSpriteNode spriteNodeWithTexture:WasserFallTexture1];
tunnel1.position = CGPointMake( 0, y );
tunnel1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:tunnel1.size];
tunnel1.physicsBody.dynamic = NO;
tunnel1.physicsBody.categoryBitMask = tunnelKategorie;
tunnel1.physicsBody.contactTestBitMask = nyanKategorie;
...
[tunnel addChild:tunnel1];
I can do that without the nice engine of Spritekit. I implement it as well, but i want to know a way to do that with the SpriteKit engine.
I know that i can change the "SKPhysicsBody" type and areas, but i'm looking for a soloution witch can catch a collision between two png files non-transparent areas.
Thanks,
Sam
Sounds like actually seeing the physics bodies in action is what you need. There is a property on SKView called showsPhysics. Set that to YES and you can see the physics bodies of your sprites in action.

Reanimating a bouncing ball in sprite-kit

Reanimating a bouncing ball in sprite-kit
I have a question about the best way to reanimate a bouncing ball. Using Ray Wenderlich's tutorial on building a break out game as a reference I have a number of circular shape nodes which continually bounce around a gravity-less environment. When the user touches one of them, it centers on the screen, stops moving, and transforms into something else. When the user touches it again, it is supposed to transform back into the circular shape node and then continue bouncing around the screen.
I have it all working as I like except for reanimating the ball. The manner in which I stop the animation is simply to set the physicsBody to nil. Once I want to reanimate, I call the method that sets up the physicsBody for the node and I apply an impulse to it. But frustratingly, the impulse does not have the effect I think it should. Clearly, my expectations are incorrect.
Here is the code in question:
- (void)applyPhysics
{
// Set physics properties
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.frame.size.width/2];
self.physicsBody.friction = 0.0f;
self.physicsBody.restitution = 1.0f;
self.physicsBody.linearDamping = 0.0f;
self.physicsBody.allowsRotation = NO;
self.physicsBody.collisionBitMask = wallCategory;
self.physicsBody.categoryBitMask = bubbleCategory;
}
- (void)morphPicker
{
BOOL check = NO;
if (check) NSLog(#"morphPicker called.");
[self runAction:[SKAction fadeAlphaTo:0.0 duration:.5]];
if (self.flipped) {
if (check) NSLog(#"The information side is showing.");
self.fillColor = bubbleColors[self.index % bubbleColors.count];
self.alpha = .9;
// Resume physics:
[self applyPhysics];
[self.physicsBody applyImpulse:CGVectorMake(10.0f, -10.0f)];
} else {
// stop physics:
self.physicsBody = nil;
if (check) NSLog(#"The statistic side is showing.");
self.fillColor = [SKColor blackColor];
self.alpha = .9;
[self transformToPickerNode];
[self removeAllChildren];
}
self.flipped = !self.flipped;
}
So my questions are these:
How should I be reanimating the ball if applying an impulse to it is not the correct method?
Is there a more elegant way to stop the animation after centering the node than setting the physicsBody to nil? Even to my novice instincts it feels like a brutish method.
After all my trials and searching for an answer to this I firmly believe that there is no elegant way to apply a new impulse to a sprite and expect it to move in the physics world. Thus my answer must be "it cannot be done at this point." However, my solution of removing all the the animated sprites from the scene and recreating them turned out to be the best solution for a completely different reason. The nature of my physics world caused my "floating bubbles" to eventually end up in the corners of the scene where they remained unmoving. Occasionally removing and recreating them had the effect of sending them happily bouncing around the scene again :)