some more hardcore network testing

This commit is contained in:
logsol 2012-06-17 00:42:28 +02:00
parent 5a3d3d0764
commit 26aa471b14
7 changed files with 123 additions and 31 deletions

View file

@ -18,21 +18,27 @@ 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());
if(isUpdateNeeded) {
sendToClients('world-update', update);
}
}
function sendToClients(message, data) {
var packet = {
@ -86,6 +92,7 @@ socket.on('connection', function(client) {
break;
}
}
updateClients();
});
client.on('disconnect', function(){

View file

@ -1,5 +1,15 @@
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;
}

View file

@ -1,5 +1,15 @@
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;
}

View file

@ -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);
}

View file

@ -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) {

View file

@ -2,7 +2,7 @@
<html>
<head>
<title>Box2dWeb Demo</title>
<script src="http://localhost:8003/socket.io/socket.io.js"></script>
<script src="http://fuuuuu.de:8003/socket.io/socket.io.js"></script>
<script src="box2d.js"></script>
<script src="common.js"></script>
<script src="client.js"></script>

View file

@ -13,26 +13,31 @@ function update() {
}
setInterval(update, 1000 / 60);
setInterval(updateClients, 500);
setInterval(updateClients, 100);
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()){
update[userData.bodyId] = {
p: body.GetPosition(),
a: body.GetAngle(),
v: body.GetLinearVelocity(),
};
isUpdateNeeded = true;
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
sendToClients('world-update', update);
}
}
function sendToClients(message, data) {
var packet = {
@ -45,7 +50,13 @@ function sendToClients(message, data) {
}
}
function pong(client, data) {
var packet = {
m: 'pong',
d: data
}
client.send(JSON.stringify(packet));
}
setupWorld();
//update();
@ -74,21 +85,49 @@ socket.on('connection', function(client) {
client.send(JSON.stringify({"startId" : clients.length}));
client.on('message', function(data){
data = JSON.parse(data);
client.on('message', function(packet){
packet = JSON.parse(packet);
if(data && data.m){
switch(data.m){
if(packet && packet.m){
switch(packet.m){
case 'jump':
jump();
break;
case 'ping':
pong(client, packet.d);
break;
default:
break;
}
}
updateClients();
});
client.on('disconnect', function(){
console.log("disconnect");
});
});
/******* collision ********/
function createCollisionDetector() {
var listener = new b2ContactListener();
listener.BeginContact = function(contact){
}
listener.PostSolve = function(contact, impulse){
}
listener.EndContact = function(contact){
}
return listener;
}