fooled around with joints for hiding lag

This commit is contained in:
Logsol 2013-07-30 21:15:11 +02:00
parent f5eacb6335
commit 8f245f33d6
4 changed files with 68 additions and 12 deletions

View file

@ -1,4 +1,4 @@
define(function () {
define(function () {
function Key () {
this._active = false;

View file

@ -1,13 +1,15 @@
define([
"Lib/Vendor/Box2D",
"Game/Core/GameController",
"Game/Client/Physics/Engine",
"Game/Client/View/ViewController",
"Game/Client/Control/PlayerController",
"Game/Core/NotificationCenter",
"Lib/Utilities/RequestAnimFrame"
"Lib/Utilities/RequestAnimFrame",
"Game/Config/Settings"
],
function (Parent, PhysicsEngine, ViewController, PlayerController, NotificationCenter, requestAnimFrame) {
function (Box2D, Parent, PhysicsEngine, ViewController, PlayerController, NotificationCenter, requestAnimFrame, Settings) {
function GameController () {
this.viewController = new ViewController();
@ -23,6 +25,25 @@ function (Parent, PhysicsEngine, ViewController, PlayerController, NotificationC
GameController.prototype = Object.create(Parent.prototype);
GameController.prototype.makeMouseJoint = function(p) {
var ground = this.physicsEngine.getGround();
var body = this.me.getBody();
var def = new Box2D.Dynamics.Joints.b2MouseJointDef();
def.bodyA = ground;
def.bodyB = body;
def.target = p;
def.collideConnected = false;
def.maxForce = 100;
def.dampingRatio = 0.99;
this.mouse_joint = this.physicsEngine.world.CreateJoint(def);
body.SetAwake(true);
}
GameController.prototype.destruct = function() {
//destroy box2d world etc.
};
@ -51,11 +72,39 @@ function (Parent, PhysicsEngine, ViewController, PlayerController, NotificationC
if(bodyName && updateData[bodyName]) {
var update = updateData[bodyName];
body.SetAwake(true);
if(false && this.me && this.me.getBody() == body) {
var p = body.GetPosition();
var x = update.p.x - p.x;
var y = update.p.y - p.y;
var max = 0.5;
var factor = 0.2;
//if(x > max || x < -max || y > max || y < -max) {
if(!this.mouse_joint) this.makeMouseJoint(update.p);
else this.mouse_joint.SetTarget(update.p);
var self = this;
/*setTimeout(function() {
self.physicsEngine.world.DestroyJoint(self.mouse_joint);
self.mouse_joint = null;
}, Settings.WORLD_UPDATE_BROADCAST_INTERVAL / 2)*/
//} else {
//this.physicsEngine.world.DestroyJoint(this.mouse_joint);
//this.mouse_joint = null;
//}
// NEXT TIME, try to create a simple body, that gets position and velocities from server doll
// and connect the joint to that.
} else {
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
}
}
} while (body = body.GetNext());
}

View file

@ -22,7 +22,7 @@ define({
WALK_SPEED: 2.5,
RUN_SPEED: 4.0,
FLY_SPEED: 3.2,
JUMP_SPEED: 3.0,
JUMP_SPEED: 4.0,
JUMP_UPLIFT: 0.05,
// restitution: bouncyness, friction: rubbing, density: mass
@ -46,5 +46,5 @@ define({
DEBUG_MODE: true,
// NETWORKING
WORLD_UPDATE_BROADCAST_INTERVAL: 100
WORLD_UPDATE_BROADCAST_INTERVAL: 70
})

View file

@ -11,12 +11,17 @@ function (Settings, Box2D, CollisionDetector) {
new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY),
Settings.BOX2D_ALLOW_SLEEP
);
this.ground = null;
}
Engine.prototype.getWorld = function () {
return this.world;
}
Engine.prototype.getGround = function () {
return this.ground;
}
Engine.prototype.setCollisionDetector = function (player) {
var detector = new CollisionDetector(player);
@ -24,7 +29,9 @@ function (Settings, Box2D, CollisionDetector) {
}
Engine.prototype.createBody = function (bodyDef) {
return this.world.CreateBody(bodyDef);
var body = this.world.CreateBody(bodyDef);
if(!this.ground) this.ground = body;
return body;
}
Engine.prototype.update = function () {