diff --git a/networking/app.js~ b/networking/app.js~ index 0bc5121..1e036a5 100644 --- a/networking/app.js~ +++ b/networking/app.js~ @@ -18,20 +18,26 @@ setInterval(updateClients, 500); function updateClients() { var body = world.GetBodyList(); var update = {}; + var isUpdateNeeded = false; do { var userData = body.GetUserData(); - - if(userData && userData.bodyId){ + + if(userData && userData.bodyId && body.IsAwake()){ + console.log(body.IsAwake()); update[userData.bodyId] = { p: body.GetPosition(), a: body.GetAngle(), v: body.GetLinearVelocity(), }; + isUpdateNeeded = true; } } while (body = body.GetNext()); - sendToClients('world-update', update); + + if(isUpdateNeeded) { + sendToClients('world-update', update); + } } function sendToClients(message, data) { @@ -41,7 +47,7 @@ function sendToClients(message, data) { } //console.log(JSON.stringify(packet)); for (var i = 0; i < clients.length; i++) { - clients[i].send(JSON.stringify(packet)); + clients[i].send( JSON.stringify(packet)); } } @@ -86,6 +92,7 @@ socket.on('connection', function(client) { break; } } + updateClients(); }); client.on('disconnect', function(){ diff --git a/networking/client.js b/networking/client.js index 61c5997..d5e6f87 100644 --- a/networking/client.js +++ b/networking/client.js @@ -1,4 +1,14 @@ -var id = null; +var id = null; + +//window.setInterval(ping, 1000); + +function ping(){ + var packet = { + m: 'ping', + d: new Date().getTime() + } + socket.send(JSON.stringify(packet)); +} function setupCanvas() { var debugDraw = new b2DebugDraw(); @@ -13,12 +23,13 @@ function setupCanvas() { } function _jump() { + console.log('---jump---'); jump(); - var data = { + var packet = { m: 'jump' }; - socket.send(JSON.stringify(data)); + socket.send(JSON.stringify(packet)); } function init() { @@ -63,11 +74,18 @@ function updateWorld(data) { var body = world.GetBodyList(); do { var userData = body.GetUserData(); - if(userData && userData.bodyId){ + if(userData && userData.bodyId && data[userData.bodyId]){ var update = data[userData.bodyId]; + + console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y); + + /*body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); body.SetLinearVelocity(update.v); + */ + + } } while (body = body.GetNext()); } @@ -91,6 +109,9 @@ socket.on('message', function(packet) { case 'world-update': updateWorld(packet.d); break; + case 'pong': + console.log('pong', new Date().getTime() - packet.d); + break; default: break; } diff --git a/networking/client.js~ b/networking/client.js~ index efbf1f1..c273cfb 100644 --- a/networking/client.js~ +++ b/networking/client.js~ @@ -1,4 +1,14 @@ -var id = null; +var id = null; + +//window.setInterval(ping, 1000); + +function ping(){ + var packet = { + m: 'ping', + d: new Date().getTime() + } + socket.send(JSON.stringify(packet)); +} function setupCanvas() { var debugDraw = new b2DebugDraw(); @@ -13,12 +23,13 @@ function setupCanvas() { } function _jump() { + console.log('---jump---'); + jump(); - var data = { + var packet = { m: 'jump' }; - - socket.send(JSON.stringify(data)); + socket.send(JSON.stringify(packet)); } function init() { @@ -63,11 +74,18 @@ function updateWorld(data) { var body = world.GetBodyList(); do { var userData = body.GetUserData(); - if(userData && userData.bodyId){ + if(userData && userData.bodyId && data[userData.bodyId]){ var update = data[userData.bodyId]; + + console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y); + + body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); body.SetLinearVelocity(update.v); + + + } } while (body = body.GetNext()); } @@ -91,6 +109,9 @@ socket.on('message', function(packet) { case 'world-update': updateWorld(packet.d); break; + case 'pong': + console.log('pong', new Date().getTime() - packet.d); + break; default: break; } diff --git a/networking/common.js b/networking/common.js index 1f6c28c..8f73a25 100644 --- a/networking/common.js +++ b/networking/common.js @@ -2,7 +2,7 @@ var bodiesNum = 3; var world; var xport = 8003; -var xhost = '127.0.0.1'; +var xhost = 'fuuuuu.de'; var b2Vec2 = Box2D.Common.Math.b2Vec2, b2AABB = Box2D.Collision.b2AABB, @@ -22,8 +22,8 @@ function setupWorld() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; - fixDef.friction = 0.5; - fixDef.restitution = 0.2; + fixDef.friction = 0.99; + fixDef.restitution = 0.01; var bodyDef = new b2BodyDef; @@ -56,7 +56,9 @@ function setupWorld() { bodyDef.position.x = (i + 1) * 4; bodyDef.position.y = 3; - bodyDef.userData = {"bodyId" : parseInt(i) }; + bodyDef.userData = { + 'bodyId': i + '' + }; world.CreateBody(bodyDef).CreateFixture(fixDef); } diff --git a/networking/common.js~ b/networking/common.js~ index 9793e9c..7a28b49 100644 --- a/networking/common.js~ +++ b/networking/common.js~ @@ -2,7 +2,7 @@ var bodiesNum = 3; var world; var xport = 8003; -var xhost = '127.0.0.1'; +var xhost = 'fuuuuu.de'; var b2Vec2 = Box2D.Common.Math.b2Vec2, b2AABB = Box2D.Collision.b2AABB, @@ -23,7 +23,7 @@ function setupWorld() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; fixDef.friction = 0.5; - fixDef.restitution = 0.2; + fixDef.restitution = 0.01; var bodyDef = new b2BodyDef; @@ -56,7 +56,9 @@ function setupWorld() { bodyDef.position.x = (i + 1) * 4; bodyDef.position.y = 3; - bodyDef.userData = {"bodyId" : parseInt(i) }; + bodyDef.userData = { + 'bodyId': i + '' + }; world.CreateBody(bodyDef).CreateFixture(fixDef); } @@ -65,7 +67,7 @@ function setupWorld() { function jump() { var body = findBody(1); body.SetAwake(true); - body.ApplyImpulse(new b2Vec2(0, -5), body.GetPosition()); + body.ApplyImpulse(new b2Vec2(0, -9), body.GetPosition()); } function findBody(index) { diff --git a/networking/index.html b/networking/index.html index 635dffd..670bec7 100644 --- a/networking/index.html +++ b/networking/index.html @@ -2,7 +2,7 @@