Scrolling combined with physics moves my sprite out of the screen - ios

I'm implementing a side scrolling game with SpriteKit:
As long as my sprite stays in the middle of the screen, I'm moving the sprite
When the sprite reaches the left or the right part of the screen, I'm moving the level instead of my sprite:
This works quite well, unless my sprite collides with another object. In that case the level (triggered by my code) and the sprite (triggered by the physics engine) are moved and the sprite moves outside the screen:
I tried to stop the impulse which is applied from the physics engine. This hasn't worked.
Any idea how to handle this?
Thanks
Stefan

I suppose by "moving the level" you mean moving the background view.
Why don't you just have an area in which your sprite moves that is the same size as your background? You can scroll to keep the sprite visible without disturbing the logic of physics and other manipulation of the movement.

Related

iOS - Physicsbody moving right through another

I am making an SpriteKit game involving sorting colored balls with a rotating funnel that rotates based off where you move your touch. Problem is if someone swipes across the screen really fast, balls will move right through the physicsBody on the funnel. I am changing the zRotation of the funnel with each call to touchesMoved(). Both the balls' physicsbody and the funnel's physicsbody has .usesPreciseCollisionDetection to true. .isDynamic is false for the funnel.
I figure when someone swipes across the screen, the animation is happening so fast that the framerate cannot keep up and as the funnel rotates, there is such a large gap in its rotation that the ball ends up on the other side of the physicsbody in a single frame.
Is there any way to fixing this? I am new to SpriteKit, any help is appreciated. My code is here

Bouncing Ball SKPhysicsBody - Stop Bouncing Perpendicular Forever

So I'm making a brick break style game using "Ray Wenderlich's amazing tutorial" in Spritekit/Swift, just like the tutorial.
I successfully have a ball bouncing around the screen using a SKSpriteNode() and SKPhysicsBody() and I've been tweaking values for the impulses used to begin the ball bouncing around the screen forever.
However, I stumbled upon a problem which I hope to find a solution.
Sometimes, in my game, the Sprite will bounce around and become "Wall Locked" bouncing almost Perpendicular (or straight) between two walls, (give or take a few pixels)! So ends up Zig-Zagging across the whole screen between two said walls for ages.
What I want to do is, should this happen and the ball get "Wall Locked" to introduce a new impulse to get it moving again...
How can this be done? For example, if it has bounced between the top and bottom walls 10 times, then it's time to adjust it's angle/introduce a new impulse.
Could the last 10 positions be stored in an array, then test if their almost perpendicular somehow?
If you inspect the velocity property of your Sprite, you could detect when either of its values is close to zero and then make them something that is not so close to zero. This would stop it getting stuck moving just horizontally or vertically.

SpriteKit physics: How to make a player sprite follow the (sloped) ground

I am creating a tilemap platform game in SpriteKit. I assigned collision paths to the physics body of all ground tiles and made them non-dynamic. To the player I assigned two collision polygons: a circle on the bottom and a rectangle on the top.
The player sprite has a fixed position on screen, while the level is scrolling from right to left. Now, as long as the player sprite is on flat ground, the collisions work perfectly and the player is walking on the ground. However, I also have some sloped terrain tiles that I want the player to follow (e.g. walking uphill). But when the player reaches a sloped tile, he simply bounces off of it, being unable to "climb" it.
Similarly, when I drop the player from above on a sloped tile, he slides down the "hill", instead of remaining in position.
I have both restitution and friction set to 0.
So how can I make the player sprite follow the ground regardless of its shape and how can I make the player stay on a sloped tile instead of sliding down?
I tried using SKConstraints with positionX set to a constant value. At first it seemed to work, but then the player got stuck in the middle of a sloped tile and eventually fell through it.
I also tried different shapes of collision polygons on the player (e.g. a rectangle instead of a circle at the bottom) but that changed nothing.
Any help is appreciated!
This has more to do with your game logic instead of your map properties. You need to have several "states" for your player. For example, if your player is idle you can set the CGVector to 0,0. This will stop the player from moving in any direction.
To give you some examples on movement. Let's say you want to make your object move right:
// move node's physics body to the right while leaving vertical movement as is
// 160 is just an example
myNode.physicsBody.velocity = CGVectorMake(160, self.physicsBody.velocity.dy);
// do not allow right movement to exceed 160
if(myNode.physicsBody.velocity.dx > 160)
myNode.physicsBody.velocity = CGVectorMake(160, self.physicsBody.velocity.dy);
To move left you inverse the dx value:
myNode.physicsBody.velocity = CGVectorMake(-160, self.physicsBody.velocity.dy);
if(myNode.physicsBody.velocity.dx < -160)
myNode.physicsBody.velocity = CGVectorMake(-160, self.physicsBody.velocity.dy);
Allow myNode to be affected by gravity and it should remain in contact with the ground as it moves down a slope.
It's a tilemap platform game...
Then gravity isn't important, put gravity to a very low value and then change all your impulses for the jumps and such in relation to the change in gravity...
OR,
Possibly, if the game isn't randomly generated you can set up a uibezierpath, and turn the path off if he stops moving up the hill.
But then if he stopped mid-hill or was starting from the top, he would still slide down...
And increasing friction (not setting it to 0) may help. Try friction at 1?

didBeginContact not called on fast moving sprite

I have a game where a user is dragging around my main sprite. The main sprite collides with other sprites just fine except when the user drags the main sprite very quickly. Sometimes when the main sprite is moving quickly, the physics bodies just pass right through each other and the two sprites suddenly overlap. I have a breakpoint set that logs the hit count at didBeginContact, and it is not hit.
Is there a limit to how fast a sprite can move and still be covered by didBeginContact? Am I allowing the user to move the sprite faster than the game cycle can handle the collisions?
Again, when the sprite is moving at slow speeds, the physics are working perfectly.
Remember, these things are all calculated frame by frame. You're probably moving the sprite so fast that its ending up on the other side of the screen in too few frames to count as a collision. If someone is spastically moving their finger around it might not catch it. You could put some kind of speed limit on the sprite or something.
try to set physic body with usesPreciseCollisionDetection = YES

Cocos2d. Sprite and progress bar tight connection

Start to learning Cocos2d and have a question. With moving of the sprite I need progress (health) bar keep moving with them (slightly above the sprite), with keeping it position relative to the sprite like if it was one sprite. Any ideas how to realise?
Thanks,
Alex.
add the health bar as a child to the sprite, at the required offset from the soldier's body. When you later move the soldier, the health bar will follow.
You can add the health bar sprite as a child of the other sprite:
[sprite addChild:healthBar];
You can position the health bar sprite as usual, but the final position will be relative to the parent's anchor point (which by default is located at the center of the sprite).

Resources