Sprite kit physics in endless runner game? - cocoa-touch

How would I use sprite kit physics in my endless runner game?
An endless runner fakes motion by keeping the player stationary but moving the background and all the other objects by a set speed.
BUT, I want to simulate physics.
What if I let my player move with the physics engine, move the background by the displacement of the player from the original position, and then move the player back to it's original position?
Would this be smooth and look good? If so, then what methods of sprite kit do I use so no visual errors show to the user.
What's the proper solution?
Thank you.

The proper way is to center the scene on a node. The best way to learn how to do so is to go ahead and visit the docs here (go to section titled 'Centering Scene on a Node'), but if you encounter any problems with the implementation let us know!
In case you're wondering how it works, the background stays stationary (unless you want parallax scrolling), while the character moves. However, every frame the camera 'follows' a player, meaning wherever you moved your player with the physics, the screen will follow and keep the character at the center.
Edit
Here is the code I use in one of my games to center on the sprite (which is a plane controlled by buttons):
-(void)didSimulatePhysics {
... #Code here to simulate plane movement and such
SKNode *camera = [self childNodeWithName:#"//camera"];
SKNode *player = [self childNodeWithName:#"//sprite"];
if (player.position.y >= 0) camera.position = CGPointMake(player.position.x, player.position.y);
else camera.position = CGPointMake(player.position.x, 0);
[self centerOnNode:camera];
if (velocity>1){
self.directionMeter.zRotation = -M_PI/2 + mdirectionOfTravel;
}
}
-(void)centerOnNode:(SKNode *)node {
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
Basically, when the player is above a certain position, I move the camera up with the player. When the player moves anywhere on the x-axis, the camera always moves with them. I have contact detection to find where the player hits the ground (and thus loses), and the background color (the sky) changes according to the altitude of the plane (the higher the plane, the darker the blue).

Related

Sprite Kit Physics Simulation - bodies never come to rest

With Sprite Kit physics I have a simple setup with a ball bouncing inside a rectangular edge shape.
It all works fine, however the ball never comes to rest. The resting property is always NO and I can see that the ball animation keeps making a little bit of movement when it should be resting.
Sprite Kit is based on Box2D and here there is a doSleep option on the physics world, but I cannot find something similar with Sprite Kit.
What am I missing?
You should use restitution. A property for setting the bounciness of the physics body in sprite kit. Actually, it is a property describing how much energy a body retains when it bounces off of another body. Dot operator can be used with the ball's sprite name to access the restitution property.

How to clip part of a sprite based on its position?

I'm designing a game in Cocos2d, and at one point I have coins shooting out on a platform from a zelda-ish perspective. I'd like to display the coin's shadow sprite (a different sprite from the coin) on the platform, but mask or clip the shadow sprite on the edge of the platform. The coin can continue off the edge of the platform, but the shadow should stop at the edge. The platform also moves, so I need the shadow sprite to track with the platform's movement.
I thought it could work to use a CCClippingNode for this, but I can't add it as a child of anything in a spriteBatchNode which is how I'm making my platform. Without having the shadow as a child of the platform, I'll mess up z-order and the shadow movement won't track correctly. I also checked out Ray Wenderlich's tutorial on masking a sprite but I don't think that'll work since it looks like it masks an individual sprite texture and not an area of the view where the sprite shouldn't be displayed. Any ideas on how to solve this?

Is there a way to play (rewind/fwd) a paused movie with left right gesture in objective c

I have a video clip that will work like a intro video when application starts. Video contains many objects that comes from all directions and incorporates with each other.
I have two ways to do this:
I start video in paused mode. Video goes forward or backward with finger left-right movement, exactly similar to the play strip of a MPMoviePlayerController. (video will remain paused and will not play)
I make every object separate and inside a UIScrollView I move them as per the video describes with the help of - (void)scrollViewDidScroll:(UIScrollView *)scrollView method.
2nd way is too complicated because the video is too lengthy and has near 45 objects. So how to go for 1st option?
You should export your movie as a serie of image. Then use [UIImageView animationImages:listOfImages] and bind a UIGestureRecognizer on your imageView and display an image instead of another depending on the direction of the slide.

Cocos2d: Emitted particles do not pan correctly with screen

I have setup a particle emitter to show a glowing orb which looks great (added by the code below). The only issue is that when I pan around the level the particles that have already been created pan around too rather than staying local to the emitter location; the emitter itself pans around correctly and emits new particles from the correct location
CCParticleSystem *orb = [CCParticleSystemQuad particleWithFile:#"orb.plist"];
orb.position = ccp((screenSize.width / 2),screenSize.height);
[self addChild: orb];
What do I have to do to ensure that emitted particles also pan around with the screen?
There are three possible behaviors for particles positioning (positionType property of particle system). As stated in cocos2d sources:
kCCPositionTypeFree - Living particles are attached to the world and are unaffected by emitter repositioning.
kCCPositionTypeRelative - Living particles are attached to the world but will follow the emitter repositioning. Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.
kCCPositionTypeGrouped - Living particles are attached to the emitter and are translated along with it.
I'm not properly understanding what is your expected behavior. Try all these modes at first.
Also, cocos2d has great demo which is distributed with sources. Check ParticleTest example.

How to recognize the touch of a non regular sprite image?

I have a sprite and if it is touched the touch should be recognized. I used the coordinates to do so. I took the coordinates (min x, min y, max x , max y)of the sprite image. But The sprite image is not a rectangular shape. So, even if I touch the coordinates outside the sprite and inside the rectangular bounds the sprite is recognized.
But for my application I need only the sprite to be recognized. So, I have to take only the coordinates of the sprite, but it is not regular shape. I am using CCSprite in my program.
So, what can I do to for only the sprite to be selected ? Which classes should use for this?
Thank You.
You could try one of the following...
Create a bounding box smaller than the absolute extents of the sprite image. Yes it will be smaller than the sprite. This will eliminate the dead space click detection of the sprite the trade off being parts of your sprite which look selectable won't be
Use a circular bounding area to detect if the user has clicked on your sprite. Again you will have the dead space problem in my first suggestion but the sphere may give you some better coverage area over the sprite giving you better results on touch detection
This is a standard problem in physics collision detection systems which often end up using circles or rectangles as their collision bodies. I would go with the either a circle or rectangle smaller than the size of your sprite as your bounding area. Going finer detail than that you could generate bounding area polygons. This would however introduce a whole bunch of new issues and concerns.
I am building a Cocos2D game right now and what I am doing is first I step through my sprites and see which sprites the touch hit (they overlap in my app)
Then, for each sprite hit I use [sprite convertTouchToNodeSpace] to get an X,Y co-ordinate inside the sprite, which I can use (although the Y axis is flipped) to reference the CGImage I created the sprite with.
If the pixel at the touch point is 'clear' ie alpha 0, then the sprite was not really touched, and I check the next sprite in the z-order to see if it has color where it was touched.
Sometimes I think I should be using a two color mask image to go along with each sprite, not the sprite image. But, I am mr. make it work, then make it fast.
I realise this is not super efficient, but I do not have very many sprites and I do this only for touches.