Network Game - Interpolation/extrapolation with SpriteKit - ios

I'm currently facing a - I think - very common networking problem. But I can't find an answer with SpriteKit.
Let's take a simulation with a ball that bounce again wall. The objective is to synch the ball position on two devices via bluetooth or Wifi.
It uses SpriteKit physics to simulate ball movement and collisions
It uses MultipeerConnectivity for networking on bluetooth or Wifi.
The Device A - master - simulates everything and tells ball position and vector to Device B at synchronisation points. The Device B simulates the scene between synch points.
Currently, the ball in Device B is always ~50ms later than device A. To prevent this, someone told me about "interpolation" : Device B receive the DeviceA's ball position and knows the info is 50ms old. So Device B should be able to predict the "real" position 50ms after the received one.
But I did not find a way to do this with SpriteKit. I don't want to manually develop what's SpriteKit physics automatically made for me!
I'm just searching for a way to simulate a 50ms jump in the future!
Any idea?

You can experiment with speed property of your SKScene. It can set above 1.0.

Related

How to spawn power ups and detect when they are picked up without enemies being to interact with them

I am currently making a game with sprite and the spritekit library, and I am trying to add power ups. I have tried to use physics bodies to detect when the player touches the power up, however that has not worked for me as the enemies are also able to interact with it. This is a problem, as I only want the player to be able to pick up the power up and have enemies walk straight over the power ups.(The game is top down) I was wondering how I could achieve this. Thank you.
There are a lot of different ways to achieve this.
You could add in some code to the power-up sprite that checks that the colliding sprite is the player.
You could also add in some code to the enemy sprite so that when it collides with the power-up sprite nothing is done.
I'd recommend looking up some tutorials on game development with Swift.
Good luck!

iOS game center game is out of sync

I've been building a game center game for iOS and it works great so far. I finally started testing the game and theres (obviously) some latency, which is causing the game to be out of sync.
Basically I have 2 players, each controlling a game character. The game character can shoot fireballs and iceballs etc. These attacks do damage, and they have effects. For example. the ice ball effect will freeze the opponent for 3 seconds if it makes contact. the fireball will do extended fire damage for 3 seconds.
so the problem is, when I was playing against my brother, because of the latency, my game said I had 40 health left and he was down to 0, while on his device, the game said I had 0 and he had 20.
This means that attacks were registering/colliding on one device (based on the positions of the characters/fireballs) and not on the other. and vice versa.
I'm currently using the default peer-to-peer game-center architecture. Would using a client-server architecture (one person becomes the server) solve this out-of-sync problem?
if not, what other options do I have with the game kit API?
I've found a solution to this problem.
Note: this will be a relatively long answer.
One thing that I did implement was the use of udp for some of my data transfers that are not as critical as others. For example, since I'm sending movement data about 10 times a second, I figured its ok if 1 or 2 of the 10 get lost once in a while.
Now to the actual problem:
So what was happening is the following- since I'm using a p2p architecture, both clients have a delay when they see the game 'world'. This means that on my device, I see the enemy player at a position that is delayed 100-200 milli seconds. (so he was actually there 100-200 ms in the past).
The problem with this, is when I shoot a projectile at the enemy, and I see a collision, if the collision was right on the edge of his sprite (his feet or head), in his screen, he was already past that position - PLUS - my fireball appears 100ms delayed on his screen. This means on his device, he was able to dodge my attack. This can happen a random number of times, but I'd guess its probably below 30% of the time. 70%, both devices see the collision.
The solution
What I came up with is to send a message to the other player when either device sees a collision. And remember, each device has no idea if the other saw a collision or not. So I have no way of knowing whether both devices saw a collision, or only 1, and the other saw that the attack was dodged.
This means I have to send a message to the other player every time theres a collision. Now, because of the way I've architected my game, when a collision is sent, I'm automatically applying the collision event (meaning, damage dealt, projectile effect that took place - like ice bolt freezing the player it collided with) This is problematic, because what if both devices saw the collision. That means both devices are sending each other a message of collision, and applying collisions again.
To get around this, I've added a "spell number" to each spell/attack, and when a collision happens, I save this number with the player that the collision happened with as the "last collision spell number". So if a collision takes place with that spell, the player knows he/she already collided with that specific object, so collision logic doesn't run twice.

managing sprite kit multiplayer world node

I've been working on a 'side scroller' sprite kit game (not really side scroller, you can move in any direction) and the apple docs/stackoverflow answers say to make a world node as the child of the scene, and move the world node instead of the character sprite.
does this suggestion change for a multiplayer game where there are multiple character sprites moving inside the same world node? Because in this case, one sprite moving changes the world for everybody.
The suggestion still applies in multiplayer. Normally you have only one local player, so the "camera" will still be following that particular player and none other.
In local multiplayer (2 players playing on same device) you would probably have the camera follow the mid point between the two players. But it's still the same principle.

How to add Bodies to a SKPhysicsWorld

I'm developing a running game for iOS devices and have run into a problem. I created a subclass for a game tile which is 4 screen lengths long. The game tile will have some nodes which are obstacles. I have a scene which creates the player, some background decorations, and particle systems. I need to add the obstacle nodes to the physics simulation in the scene so that I can detect when they contact. I'm not sure how to do that... Is there another solution that would be simpler? Thanks in advance for your help.

Transferring billiard game state from player to player with box2d

Im using Union server for my iOS (Starling) billiard game.
So far the connections work great.
My question is:
How would you handle the transfer of the ball positions from the opponent.
Lets say I make the break, and I want to copy that shot to the other player?
Do you think its a good idea to send a message over union every frame (x, y)?
Will this cause latency problems?
First about your question:
The game is installed on both devices, the rules are same. So on shot send the white balls force and all other properties you modify. On the receiving device add that force etc. and the ball will repeat the action done by the sending user.
Now the sad part:
I will be the first one to disappoint you - even if you solve your problem and send the message from player to player with out problems the result won't be pleasant: box2d calculations are optimised for performance, but the result will differ as it's calculated with approximate accuracy. As a result even on the same device the balls will end up in a different location on different runs. You won't notice it in one-two hits, but after playing for a minute you'll end up with deferent ball locations.
You can also try to send a message of every ball position in space after all balls stop moving and relocate the remote users ball positions. After that "correcting" message was received return the control to the player.
We had a similar game and I just wrote my own 2d engine. If you're working only with ball to ball and ball to rectangle collisions it's easy to write your own engine.

Resources