简体   繁体   中英

Box2D and GameCenter multiplayer

i have a hockey game with mallets running on iphone/ipad. The server controlls all the physics and send to the other device the server mallet position and the client sends to the server his mallet position.

This part is working properly, the problem is about the puck. Becuase is way too fast and the server is the onde whos sending the message. The problem is that the puck is a b2_dynamic body and the server sends to the client where puck is at. I have updated to send the position to the client every frame on gameLoop, but the problem is thats not flowing properly. The mallets movements are ok, but with the puck its wierd a bit, i think it has to due with the linear velocity of the puck.

What im doing is this:

[gamedelegate movePuckToX:messageData->x andY:messageData->y andAngle:0.0];


- (void) sendMovePuck {
MessageMovePuckTo message;
message.message.messageType = kMessageTypeMovePuck;

float_t pX = puck->GetPosition().x;
float_t pY = puck->GetPosition().y;

message.x = pX;
message.y = pY;

NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageMovePuckTo)];    
[self sendData:data];    
}


-(void )movePuckToX:(float_t)x andY:(float_t)y  andAngle:(float_t)ang
{
    lastPuckReceivedPosition = b2Vec2(x, [self getScreenSize].height/32 - y);
    puck->SetAwake(false);        
    puck->SetTransform(b2Vec2(x, [self getScreenSize].height/32 - y), 0);           
    puck->SetLinearVelocity(b2Vec2(0,0));
}

Whats the best of sending the puck position to the client? Any ideas or insights? I cant managed to solve this :(

You should try using dead reckoning and calculate it on each side. Use the host just to distribute last play.

In navigation, dead reckoning is the process of calculating one's current position by using a previously determined position, or fix, and advancing that position based upon known or estimated speeds over elapsed time, and course.

http://en.wikipedia.org/wiki/Dead_reckoning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM