Spritekit - Moving Physics world issue - swift

I am building a tower stacking game. My structure is as follows:
I have a world node which has a physics body attached to it.
I add SKShapeNodes with physics bodies in it and they stack over one another.
When a certain height is reached, I move the worldNode down.
Now here is where the problem occurs. When I move the worldNode down, it disturbs the already placed physics bodies and it makes the tower fall quite number of times.
Coming from a Cocos2D background, I used to move the whole scene there and it did not create any problem there.
Help will be appreciated. Thank you.

Instead of moving the world node you should move the SKCameraNode into the opposite direction.

Related

Recreating physics seen in Snake vs Block

I am new to SpriteKit and am trying to learn by creating a game similar to the popular iOS game Snake vs Block
(game screenshot
).
I am not sure if I am setting up the game's physics the most efficiently.
I set the blocks to be affected by gravity, while the ball node is not.
In the touchesMoved method, I set the ball node's position to the touch's x position (y never changes).
Once a collision is detected, gravity is set to zero vector, the leading ball is removed and last ball put in its place.
Once the block is removed, I restore gravity.
However I am not sure how to keep several ball nodes connected to each other as in the game and make them follow the lead ball's position with lag.
Any advice on this?
You can connect them together as joints via SKPhysicsJointPin. You are basically making a rope / chain, and there are a ton of examples for Box2d ropes (SpriteKit physics is Box2d).
Just add the latest ball as a pin-joint to the bottom:
Here are some references for you in addition to my answer:
convert objC to swift (more or less):
https://objectivec2swift.com/#/home/converter/
(Github to project in link ): https://www.youtube.com/watch?v=7jWdcbmnmKQ
http://www.waveworks.de/howto-make-hanging-chains-sprite-kit-physics-joints/
https://developer.apple.com/documentation/spritekit/skphysicsjoint

CCParticleSystem moving particles with emitter rather than freely

I'm having a spot of trouble trying to get this to work
I basically have a CCParticleSystem (created in Sprite Builder) that I want to follow a sprite but I want the emitter node to leave a trail of sorts... kind of like an airplane leaves a trail of exhaust fumes behind it
As far as I can make out there are no setting in Sprite Builder that I can change to allow this so I went looking for a solution in code
I found this in the documentation:
self.myParticleNode.particlePositionType = CCParticleSystemPositionType.Free
Which declares that the particles will be positioned relative to the Physics World and not be affected by the position of the emitter, but it appears to not do a thing
Any help or suggestions on this would be greatly appreciated
I found that CCParticleSystems particle positioning doesn't actually function when the Emitter Node is scaled in any way... this has posed a problem for me because I wanted a zoomed out camera effect.
Never mind.
Looks like this just won't work for now

Dividing screen into two parts for iphone box2d game application

The concept is like i have 10 balls and one by one ball should come in between so i can do some action on it and remaining balls it should show at bottom. But I can not divide my screen into two parts to get the balls at bottom. I basically want to show all remaining balls at bottom where it should not collide or anything it will be just a display.
I have CCColorLayer extended class which I'm using to create game.
Can anyone please help me with it?
Thank you,
Ankita
That should be simple to do since Box2d won't touch any Cocos2d elements unless you manually create a connection between a Cocos2d element and Box2d element. Among the most common is to connect a CCSprite to a b2Body by updating the sprite's position to follow the body's position after Box2d finished simulating the world after each time step.
So for your case, you can just leave the balls at the bottom as sprites only, meaning don't assign a b2Body to it yet. Other b2Body will just ignore the bottom balls. Then when it's time for the ball to enter the game stage, assign a b2Body to it and connect the sprite and body together, and voila the balls will start having collisions!
That's all I can answer based on what you wrote in the question. If you need more details, perhaps you should ad a mock-up like HanClinto commented, or post up your codes here.

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?

Dragging a Sprite (Cocos2D) while Chipmunk is simulating

I have a simple project built with Cocos2D and Chipmunk. So far it's just a Ball (body, shape & sprite) bouncing on the Ground (a static line segment at the bottom of the screen).
I implemented the ccTouchesBegan/Moved/Ended methods to drag the ball around.
I've tried both:
cpBodySlew(ballBody, touchPoint, 1.0/60.0f);
and
ballBody->p = cgPointMake(touchPoint.x,touchPoint.y);
and while the Ball does follow my dragging, it's still being affected by gravity and it tries to go down (which causes velocity problems and others).
Does anyone know of the preferred way to Drag an active Body while the physics simulation is going on?
Do I need somehow to stop the simulation and turn it back on afterwards?
Thanks!
Temporarily remove the body from the space.
If you want the object to have inertia when you release it, that's a different story. The cleanest way is to attach a fairly stiff spring between the ball and a temporary sensor body that moves under the control of your finger. When you let go with your finger, the ball will retain whatever kinematics it had while you were dragging it. Be sure not to remove the ball from the space in this case.
You aren't updating the velocity of the object when you aren't using cpBodySlew(). That is why it falls straight down.
A better way to do it is to use a force clamped pivot joint like the official demos do to implement mouse control. http://code.google.com/p/chipmunk-physics/source/browse/trunk/Demo/ChipmunkDemo.c#296