Sprite Kit Overlap lag with xcode - ios

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;

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

How can i make a node collide with the sides of moving scene?

Hi i wish to know how can my nodes can collide with a moving scene. I have a main node that cans move along the x axis and cans collide with other nodes. I want to make the non main nodes collide with the sides of the moving scene. How can i make that ? ( the camera only follow the main node).
I have an obstacle class.
in my MLObstacle.m
-(id)init
{
if(self = [super init])
{
int aux = arc4random()%3;
switch (aux) {
case 0:
self = [MLObstaculo spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake((aux+1)*10,(aux+1)*13 )];
break;
case 1:
self = [MLObstaculo spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake((aux+1)*10,(aux+1)*15 )];
break;
case 2:
self = [MLObstaculo spriteNodeWithColor:[UIColor redColor] size:CGSizeMake((aux+1)*10,(aux+1)*14 )];
break;
}
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.categoryBitMask = obstacleCategory;
self.physicsBody.contactTestBitMask = visibleSidesCategory;
self.physicsBody.collisionBitMask = visibleSidesCategory;
[self setDx:0.0];
[self setDy:2.4];
}
return self;
}
So i add Obstacles to my WorldClass
In my WorlGen.m
-(void)GenerateWorld
{
SKSpriteNode *Ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width,100)];
Ground.position = CGPointMake(0, -self.scene.frame.size.height/2 + Ground.size.height/2);
Ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Ground.size];
Ground.physicsBody.dynamic = NO;
[self addChild:Ground];
for(int i = 0; i < 3; i++)
{
MLObstaculo * auxobs= [[MLObstaculo alloc]initObstaculo];
auxobs.position = CGPointMake((i+1)*50, Ground.position.y + Ground.size.height/2 +auxobs.size.height/2 );
[self.Obstaculos addObject:auxobs];
[self addChild:auxobs];
auxobs = NULL;
}
}
and then i add the world to my scene
Scene.m
-(id)initWithSize:(CGSize)size
{
if(self = [super initWithSize:size]
{
WorldGen * world = [[WorldGen alloc] init];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.contactDelegate = self;
self.physicsBody.categoryBitMask = VisibleSidesCategory
self.physicsBody.collisionBitMask = ObstacleCategory;
self.anchorPoint = CGPointMake(0.5,0.5);
[self addChild:world];
[world GenerateWorld];
}
}
Update...
-(void)didSimulatePhysics
{
[self centerOnNode:hero];
}
-(void)centerOnNode:(SKNode *)node
{
CGPoint pointinScene = [self convertPoint:node.position fromNode:node.parent];
world.position = CGPointMake(world.position.x -pointinScene.x , world.position.y);
}
I initalized all the category variables with this format ( static const uint32_t ObstacleCategory = 0x1 <<1; )
First edit by defining your collisionBitMask
-(id)init
{
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.categoryBitMask = ObstacleCategory;
self.physicsBody.contactTestBitMask = VisibleSidesCategory;
self.physicsBods.collisionBitMask = VisibleSidesCategory;
}
Second make this steps in Xcode -> File -> New -> File -> C and C++ -> Header File
Give a name of Physics.h and add those lines in it:
typedef enum : uint8_t {
ObstacleCategory = 1,
VisibleSidesCategory = 2,
} ColliderType;
and then import Physics.h into all your classes (You don't need to alloc or init for Physics.h, just use ObstacleCategory and VisibleSidesCategory)
From this and another question's problem, I have finally determined there is an error in the API. Specifically, a node's anchorPoint is considered (0,0) during initialization if using a WithRect: or FromRect: based initializer(but not a RectOfSize: or center: based one), so you get unexpected behavior.
Thus, even when the anchorPoint of the Scene and other objects are set to (0.5,0.5), a node places its lower left corner relative to the parent node's midpoint, rather than placing the child's midpoint relative to the parent node's midpoint.
Specifically, this can be solved in any one of these three ways:
Use an anchorPoint of (0,0).
Create your nodes without using Rect based initializers. You can use shapeNodeWithRectOfSize: instead of shapeNodeWithRect: and bodyWithEdgeLoopFromPath: instead of bodyWithEdgeLoopFromRect:
Set the Rect's initial position as lower and to the left, like so:
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(-node.size.width/2, -node.size.height/2, node.size.width, node.size.height)];

physicsBody contactDidBegin not being called?

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?

Resources