physicsBody contactDidBegin not being called? - ios

in my code I have physics bodies set up for ball and welding sprites.
here is the physicsBody setup for ball sprites:
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
ball.physicsBody.affectedByGravity = NO;
ball.physicsBody.velocity = velocity;
ball.physicsBody.restitution = 1;
ball.physicsBody.friction = 0;
ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.collisionBitMask = ballCategory | barrierCategory | weldCategory | weldPointCategory;
ball.physicsBody.contactTestBitMask = weldingCategory;
and this is the physicsBody setup for welding sprites:
weldRU = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(2, 2)];
weldRU.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:weldRU.size];
//weldRU.physicsBody.dynamic = NO;
weldRU.physicsBody.affectedByGravity = NO;
weldRU.physicsBody.categoryBitMask = weldingCategory;
weldRU.physicsBody.collisionBitMask = 0;
weldRU.physicsBody.contactTestBitMask = ballCategory;
weldRU.physicsBody.usesPreciseCollisionDetection = YES;
in the setup I give each other categoryBitMask's and contactTestBitMask's but the contact override method never gets called when these bodies come in contact. the welding sprites do have actions running at any possible contact time but the physicsBody is always present. So am I missing something that is causing them to not detect contact?

Related

Add sprite to other sprite position

I have two body
SKShapeNode *hero = [SKShapeNode shapeNodeWithCircleOfRadius:HERO_SIZE];
hero.lineWidth = 1;
hero.fillColor = [SKColor orangeColor];
hero.name = #"Hero";
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:HERO_SIZE];
hero.physicsBody.categoryBitMask = hero;
hero.physicsBody.collisionBitMask = friendlyCategory | enemyCategory;
hero.physicsBody.contactTestBitMask = friendlyCategory | enemyCategory;
hero.position = [self getStartPosition];
SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:BALL_SIZE];
circle.position = [self randomPossition];
circle.lineWidth = 2;
circle.strokeColor = [SKColor blueColor];
circle.fillColor = color;
circle.name = #"Circle";
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:BALL_SIZE];
circle.physicsBody.categoryBitMask = friendlyCategory;
circle.physicsBody.collisionBitMask = hero;
circle.physicsBody.contactTestBitMask = hero;
then,
- (void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (friendlyCategory | hero ))
{
SKShapeNode* node = (SKShapeNode*)contact.bodyA.node;
if ([node.name isEqualToString:#"Hero"])
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyB.node;
}
else
{
self.activeFriendlyBall = (SKShapeNode*)contact.bodyA.node;
}
self.hero.position = self.activeFriendlyBall.position;
}
}
Hero must be added above activeFriendlyBall, for him center position. But it added near him. I think it because physic body added. But I need to use physic body for other logic.
http://cl.ly/image/0g1S0d3h1h0w
must be like, how in top screen.
I find solution.
self.heroBall.physicsBody.velocity = CGVectorMake(0, 0);
[self.heroBall runAction: [SKAction moveTo:self.activeFriendlyBall.position duration:0.0]];
I believe it is because you have collision detection enabled for both, which means they can't be put on top of one another. If you are just trying to determine if one sprite comes into contact with another all you need is the contact detection. Removing either hero or friendlyball from the collision detection bit mask will allow them to be placed on top of each other/go through each other etc.
If you don't care about collisions at all for one of them (the sprite doesn't 'bounce' off anything) I'd recommend just setting collision detection to 0 for that sprite.
Best

SpriteKit didBeginContact function not called

I am trying to get collision working in my SpriteKit game using the didBeginContact function.
My problem is that the function is just not getting called at all when the ball bounces off of the bricks. This is how I have set them both up:
static const uint32_t blockCollisionCheck = 0x1 << 0;
static const uint32_t ballCollisionCheck = 0x1 << 1;
Ball:
SKShapeNode *ball = [[SKShapeNode alloc] init];
CGMutablePathRef drawPath = CGPathCreateMutable();
CGPathAddArc(drawPath, NULL, 0, 0, _ballRadius, 0, M_PI * 2, YES);
ball.path = drawPath;
CGPathRelease(drawPath);
ball.fillColor = [SKColor greenColor];
ball.position = CGPointMake(CGRectGetMidX(self.frame), 150);
ball.name = #"ball";
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_ballRadius];
ball.physicsBody.friction = 0.0;
ball.physicsBody.restitution = 1.0;
ball.physicsBody.linearDamping = 0.0f;
ball.physicsBody.allowsRotation = NO;
ball.physicsBody.dynamic = YES;
ball.physicsBody.categoryBitMask = ballCollisionCheck;
ball.physicsBody.contactTestBitMask = blockCollisionCheck;
[self addChild:ball];
Bricks:
SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(_blockWidth, _blockHeight)];
block.name = #"block";
block.position = CGPointMake(x, y);
block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:block.size];
block.physicsBody.allowsRotation = NO;
block.physicsBody.friction = 0.0;
block.physicsBody.dynamic = YES;
block.physicsBody.categoryBitMask = blockCollisionCheck;
block.physicsBody.contactTestBitMask = ballCollisionCheck;
[self addChild:block];
I can't for the life of me see what is wrong with this, as I have the category bit masks correct I think? Also both of the sprites are Dynamic, which is another problem I read it could have been.
It's not that the contents of my didBeginContact function is not working, it's just never getting there as evidenced from a lack of NSLog message and breakpoints not being reached.
Any help would be greatly appreciated.
Thank you.
If, as you say, the didBeginContact method is not being called at all, I suspect you did not add the self.physicsWorld.contactDelegate = self; into your GameScene init method.

Sprite Kit didBeginContact not called between body and edgeRect

I have a game where I'm flicking bodies off the screen. I want to detect when these bodies are coming into contact with the edges (still letting the bodies pass through to off the screen) of the screen and I've implemented everything but still it is not working..here's my code:
GameScene.h
typedef NS_OPTIONS(uint32_t, FAPhysicsCategory) {
FAWallCategory = 1 << 0,
FABottomWallCategory = 1 << 1,
FAAnimalCategory = 1 << 2,
};
GameScene : SKScene <SKPhsyicsContactDelegate>
GameScene.m
-(void)setupWorldPhysics
{
// Set world gravity
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);
float bottomOffset = 400.0;
CGRect newFrame = CGRectMake(0, -bottomOffset, self.frame.size.width, self.frame.size.height + bottomOffset);
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:newFrame];
self.physicsBody.categoryBitMask = FAWallCategory;
self.physicsBody.contactTestBitMask = FAAnimalCategory;
self.physicsBody.collisionBitMask = 0;
}
-(void)launchBody {
SKSpriteNode* animalSprite = [SKSpriteNode spriteNodeWithImageNamed:[animalImagesArray objectAtIndex:animalChoice]];
animalSprite.position = CGPointMake(self.size.width/2, -animalSprite.size.height);
animalSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
animalSprite.physicsBody.dynamic = YES;
animalSprite.physicsBody.allowsRotation = YES;
animalSprite.name = #"animalSprite";
animalSprite.physicsBody.categoryBitMask = FAAnimalCategory;
animalSprite.physicsBody.contactTestBitMask = FAWallCategory;
animalSprite.physicsBody.collisionBitMask = 0;
animalSprite.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:animalSprite];
}
Alright so it's late and I'm exhausted and I feel like an idiot..i was missing:
self.physicsWorld.contactDelegate = self;
tool

Sprite Kit Overlap lag with xcode

So I am trying to just get a feel of how Sprite Kit works, and one thing that I cannot figure out is why my demo lags when multiple versions of the same sprite over lap each other.
Basically what I do is I made a class that inherits SKSpriteNode, and on init I set the following properties
marioCategory = 1 << 1;
wallCategory = 1 << 2;
platformCategory = 1 << 3;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.density = 1.0;
self.physicsBody.angularDamping = 0.0;
self.physicsBody.linearDamping = 0.0;
self.physicsBody.friction = 0.0;
self.physicsBody.categoryBitMask = marioCategory;
self.physicsBody.collisionBitMask = platformCategory ;
self.physicsBody.contactTestBitMask = wallCategory ;
self.physicsBody.dynamic = YES;
self.physicsBody.allowsRotation = NO;
self.physicsBody.restitution = 0;
self.name = #"Mario";
Then I add all these "Mario"'s into an SKNode as its children, then I add this Node to the scene. (Adding the children directly to the scene does not add perfomance);
What I think it is doing is an extensive check to see if the overlapping marios are colliding or contacting, even though the contactTest bit mask says to only check for walls.
Any help to determine what step I am missing would really be appreciated, thanks.
This has been tested on an iPhone 5 btw.
Here are how I have about 40 platforms setup on screen, all are 16x16,even Mario
node.position = CGPointMake(x, y);
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size];
node.physicsBody.angularDamping = 0.0;
node.physicsBody.linearDamping = 0.0;
node.physicsBody.friction = 0.0;
node.physicsBody.affectedByGravity = NO;
node.physicsBody.categoryBitMask = platformCategory;
node.physicsBody.collisionBitMask = 0 ;
node.physicsBody.contactTestBitMask = 0 ;
node.physicsBody.dynamic = NO;

Spritekit Collision Detection not functioning properly

I am using SpriteKit to build a game and have run into some trouble with collision detection. I have 2 subclasses of SKSpriteNode, Player and Enemy. They should both detect collisions with each other. Here's how I am initializing the Player object's physicsBody:
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.categoryBitMask = playerCategory;
self.physicsBody.collisionBitMask = enemyCategory;
self.physicsBody.contactTestBitMask = enemyCategory;
And here's how I am initializing the Enemy objects's physicsBody:
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;
self.physicsBody.usesPreciseCollisionDetection = NO;
self.physicsBody.categoryBitMask = enemyCategory;
self.physicsBody.collisionBitMask = playerCategory;
self.physicsBody.contactTestBitMask = playerCategory;
I have my GameScene implementing the SKPhysicsContactDelegate protocol and I have this in its init:
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;
And yet, the didBeginContact method isn't being called as it should be. I tried initializing the physicsBodies after the objects are created in the scene and still nothing. What am I doing wrong?
UPDATE 1
Here's the code that makes the bitMasks, it's in a Common.h file that is imported in Prefix.pch file.
static const uint32_t enemyCategory = 0x1 <<0;
static const uint32_t playerCategory = 0x1 <<1;
You need to set physicsBody.dynamic to YES for your nodes.
From the SKPhysicsBody class reference:
dynamic
A Boolean value that indicates whether the physics body is
moved by the physics simulation.
The default value is YES. If the value is NO, then the physics body
ignores all forces and impulses applied to it.

Resources