How to keep sync a coop iphone game in cocos2d (+box2d) with gamekit?
so i have made a box2d game with cocos2d. The user can move the player and in the world there are dynamic objects. Now i thought of implementing a coop mode.
I know there's gamekit.
My thoughts:
make one of the two devices (or more..) to the host.
if level loads send all positions from all objects (static+ dynamic + player positions) to the other clients (iphones/ipods/ipads) and now it should be sync
BUT
should i make a dict and then add the positions of all objects to it and then convert it to a 开发者_开发问答nsdata object then send it.
Is that enough?
I want to make this without having any internet connection so P2P.
The only possibility is Bluetooth i think or are there more?
Is that enough?
Would be great if someone could give me some advice and some code on how to do that.
Thanks for reading Have a nice day.
:)
I think the good idea would be making one of the host to be a server. Because, if you create connection each to each, then you'll have N*(N-1) connections total, and (N-1) connections for each host. If you use the server then you'll have only N-1 connection for the server and only 1 connection for each client. So you'll have to perform full simulation on each of the hosts and then synchronize that data with server. The simulation, synchronized at regular intervals, will tend to run identically on all computers. It means that only small and infrequent adjustments will be necessary to keep it the same for all users.
edit: you should also think about traffic minimization, that's why sending the NSDictionary would be too much. You can send little structure, like
typedef struct {
short obj_id;
float pos;
float vel;
float angle;
} obj_inf;
edit2:
obj_info obj1_inf;
//set the values here ...
NSData* packet = [NSData dataWithBytes: &obj1_inf length: sizeof(obj_info)];
//if you have an array of obj info (e.g. 20 objects):
obj_info* oinf_arr = calloc(20, sizeof(obj_info));
for(int i=0; i<20; ++i) [{
//fill each oinf_arr[i]
}
NSData* packet = [NSData dataWithBytes: oinf_arr length: 20*sizeof(obj_info)];
Probably you want to add some header to the packet and place there the number of objects etc.
Yes, Bluetooth is your only option under those circumstances.
Regarding setting up one device as host etc., Apple provide sample code that will get you started.
In my experience, Bluetooth is very unreliable when it comes to action games simply due to performance issues, especially when Wi-Fi is active, which is out of your control. If your game depends on the games being very closely synced in terms of timings, I would make every effort to reduce the amount of data going over Bluetooth. This means sending your entire game state over continually is probably not going to be good enough. You want to sync up your games, set up the world on each device, then send over just the player input.
精彩评论