I am trying to handle a player's movement, for a 3D Chat.
I have thought of something like this : http://img16.imageshack.us/img16/5431/schemat.png
Now the client sends his position every 1 second, with his timeElapsed value.
timeDelta is (currentTime - lastTime) / 1000.
The client calculates it's position locally like this :
Code: Select all
if (keyState & PacketClass::PK_FORWARD) {
pos += movedir * timeDelta * 50.f;
} else if (keyState & PacketClass::PK_BACK) {
pos -= movedir * timeDelta * 50.f;
}
if (keyState & PacketClass::PK_RIGHT) {
relativeRotation.Y += 25.f * timeDelta;
} else if (keyState & PacketClass::PK_LEFT) {
relativeRotation.Y -= 25.f * timeDelta;
}
if (timeToUpdate < Engine->getTime()) {
PacketClass velPck(PacketClass::PT_PLAYER_POSITION_UPDATE);
velPck.WriteFloat(Cynum->timeDelta);
velPck.WriteFloat(pos.X);
velPck.WriteFloat(pos.Y);
velPck.WriteFloat(pos.Z);
velPck.WriteFloat(relativeRotation.Y);
Client->sendPacket(&velPck);
timeToUpdate = Engine->getTime() + 1000;
}
timeStamp is the timeElapsed sent by the client, inside the newDelta var.
Code: Select all
f32 newDelta = TPacket->ReadFloat();
f32 pX = TPacket->ReadFloat();
f32 pY = TPacket->ReadFloat();
f32 pZ = TPacket->ReadFloat();
f32 rY = TPacket->ReadFloat();
timeStamp = newDelta;
//
// Server loop, calculating player's position, from the keys he is pressing down.
//
if (keyState & PacketClass::PK_FORWARD) {
vector3df rot = node->getRotation();
vector3df pos = node->getPosition();
pos += rot.rotationToDirection() * 50.f * timeStamp;
node->setPosition(pos);
} else if (keyState & PacketClass::PK_BACK) {
vector3df rot = node->getRotation();
vector3df pos = node->getPosition();
pos -= rot.rotationToDirection() * 50.f * timeStamp;
node->setPosition(pos);
}
if (keyState & PacketClass::PK_RIGHT) {
vector3df rot = node->getRotation();
rot.Y += 25.f * timeStamp;
node->setRotation(rot);
} else if (keyState & PacketClass::PK_LEFT) {
vector3df rot = node->getRotation();
rot.Y -= 25.f * timeStamp;
node->setRotation(rot);
}
if (positionUpdateTime < Server->getTime()) {
// Time to send the new player's position.
PacketClass upPck(PacketClass::PT_PLAYER_POSITION_UPDATE);
upPck.WriteInt(ID);
upPck.WriteFloat(timeStamp);
upPck.WriteFloat(node->getPosition().X);
upPck.WriteFloat(node->getPosition().Y);
upPck.WriteFloat(node->getPosition().Z);
upPck.WriteFloat(node->getRotation().Y);
Server->broadcastPacket(&upPck);
positionUpdateTime = Server->getTime() + 1000;
}
As a client, I press on the forward key for about... 100ms,
and the server tells that I am at X : 0, Y : 15, Z : 54874.4875221.............
But my camera was at X : 0, Y : 15, Z : 8.52000.......
Can anyone tell me what I am doing wrong please ?
Thanks.