开发者

Box2D and GameCenter multiplayer

开发者 https://www.devze.com 2023-04-09 04:00 出处:网络
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 pos

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 dat开发者_开发知识库aWithBytes:&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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号