Detect When SKPhysicsBodies Overlap - swift

I have two SKSpriteNodes, one is a thin vertical rectangle dividing the screen and the other is a ball bouncing from side to side. When the ball touches the divider a contact is triggered as expected. However, while the ball is moving through the divider I would like to continue to trigger the contact function for each frame.
How can I detect when two SKPhysicsBody objects overlap? I had some success using the intersectsNode() function but that uses the sprite's image size, which doesn't always match the physics body outline so I'm looking for an alternative.
Thanks!

See the allContactedBodies method in the docs.

Related

PhysicsBody affected by change of anchorPoint

I have a situation where i need to use the resizeToHeight method (to zero), but i want to achieve a sliding door effect, where the resize happens from the bottom and not the center of the image (with an anchorPoint of (0.5,0.5), the resize pretty much happens in the middle.
Now, if i change the anchorPoint to (0,0), the resize occurs the way i want it, but the physicsbody of the object is not really affected by the change of the anchorPoint, thus messing with my collision detection (the invisible frame actually also collides and not the visible part of the image).
Based on what i could find online, it looks that maybe it's not the best idea to change the anchor point to CGPointZero. If that is the case, how can i handle this properly ? Or if CGPointZero is the way to go about it, how do i handle the physicsBody discrepancy ?
disconnect the sprite from the physics body, ie have a node that represents the body and another that represents the door image, that way you can move, scale them independently.
Best solution is probably to use a SKNode with physics and add the SKSpriteNode as child so you can offset it in relation to the physics body in any way you like, without having to constantly synchronize their position/rotation.

To follow a ball character like monkey kick off game

I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.

Creating a Body - Cocos2d/Box2d

I have a ball and another sprite. When the ball collides with the sprite it simulates falling.
My only problem is the other sprite is just on big image and the ball is on top of it, but there are spaces on the sprite and a lot of corners. I need to determine if the sprite has touch one of the corners. I know this is hard to understand.
So, my question is, is it possible to make a body without
b2PolygonShape blockShape;
and
blockShapeDef.shape = &blockShape;
OR
is there an alternative I can use? I cannot set the image as a box and it would take way to long to set edges because there are so many corners.
I have already set up the collision detection.
I really need help with this.
Thanks!
If you want it to react properly, you have to make a polygon using every single corner coordinate.
But don't be lazy about it. You can use SpriteHelper for creating *b2PolygonShape*s out of your sprites.
Or another alternative: VertexHelper

mouse joint is not working to restrict the ball in the half part of the screen

Hi guys I Am developing the application in cocoas2d using the box 2d frame work but unfortunately
i'm not able to restrict the gray ball in the half screen area of the image shown here
i want that ball not to go opposite part of the screen
I Have Used The b2Mousejoint For to move the ball around the screen
b2PrismaticJointDef restrict on any particular axis
But
i want to restrict on the particular rect area of the screen
You could create your custom distance joint which will restrict global axes of the ball. But it will be hard if you never write your own physics engine.
There are 2 easier ways to implement what you want.
Create 4 static "border" boxes around the area where the ball must stay. Then place the ball and the boxes into one collision group.
However, the response of the "border" boxes will not be instant. Therefore, the ball at high speed will sometimes "sink" into the boxes, then be popped out.
You can correct the position and reset the speed of the ball manually in code when it crosses the bounds of the desired area. But it may lead to the unstable physics simulation.

Problem regarding CCFollow and CCParallaxNode

I have a problem regarding CCFollow. I am using CCFollow along with CCParallaxNode. I have added layers to parallax node and then apply runaction: on parallax node.
[pn runaction:[CCFollow actionWithTarget:sprite worldBoundary:CGRectMake(0, 0, 5600, 320)]];
but using this the sprite always runs at middle of screen. Is there any way to keep it at left of screen and also layers follow this sprite?
Thanx.
If you want your player to remain on the left side of the screen, then lock the players X position to a specific region that makes sense. You should also be moving the background and other elements of your game, not your player (with exceptions, to the players position visually on the screen).