reduce physics cpu usage

This commit is contained in:
Asraelite 2016-03-29 22:35:08 +01:00
parent 8a059b92d5
commit 8ea6c2d937
10 changed files with 2435 additions and 34 deletions

View file

@ -49,7 +49,7 @@ class Body {
}
applyDelta() {
this.world.applyDelta(this.packDelta());
this.world.applyDelta(this.packDelta(), this.pos);
}
applyForce(x, y, center) {

View file

@ -8,8 +8,10 @@ class Grapple extends Projectile {
super(world, pos);
this.r = pos.r;
this.x = pos.x * 32;
this.y = pos.y * 32;
this.x = pos.x;
this.y = pos.y;
this.xvel = pos.xvel;
this.yvel = pos.yvel;
this.xvel += Math.cos(this.r) * 0.25;
this.yvel += Math.sin(this.r) * 0.25;

View file

@ -6,8 +6,8 @@ class Missile extends Projectile {
constructor(world, pos, source) {
super(world);
this.x = pos.x * 32;
this.y = pos.y * 32;
this.x = pos.x;
this.y = pos.y;
this.xvel = pos.xvel;
this.yvel = pos.yvel;
this.r = pos.r;

View file

@ -15,6 +15,7 @@ class Ship extends Body {
// Body data.
this.x = pos.x || 0;
this.y = pos.y || 0;
this.r = player.team == 'b' ? Math.PI : 0;
this.type = 'ship';
this.class = build.ship;
@ -64,6 +65,7 @@ class Ship extends Body {
if (this.grapple) {
this.grapple.retract();
} else {
let s = this.world.scale;
this.launchGrapple(this.inputs.mx, this.inputs.my);
}
}

View file

@ -22,21 +22,23 @@ class World {
this.tpsCount = 0;
this.tpsStart = Date.now();
this.scale = 32;
this.tickCount = 0;
this.bounds = {
left: -5,
right: 50,
top: -15,
bottom: 15
left: 0,
right: 250,
top: 0,
bottom: 30
}
}
addPlayer(player) {
this.players.add(player);
let pos = {
x: player.team == 'b' ? 200 : 0,
y: 0
x: player.team == 'b' ? this.bounds.right - 5 : 5,
y: this.bounds.bottom / 2
};
let ship = new Ship(this, pos, player);
player.ship = ship;
@ -86,9 +88,14 @@ class World {
this.room.broadcast('effect', copula.packFull());
}
applyDelta(data) {
applyDelta(data, bodyPos) {
data = data.map(v => +(v.toFixed(3)));
this.players.forEach(player => player.applyDelta(data));
this.players.forEach(player => {
let dx = player.ship.pos.x - bodyPos.x;
let dy = player.ship.pos.y - bodyPos.y;
if (dx * dx + dy * dy < 900)
player.applyDelta(data)
});
}
explosion(pos, power) {
@ -110,12 +117,12 @@ class World {
}
populate() {
for (var i = 0; i < 5; i++) {
for (var i = 0; i < 50; i++) {
let pos = {
x: Math.random() * 2000 - 200,
y: Math.random() * 500 - 250
x: Math.random() * this.bounds.right,
y: Math.random() * this.bounds.bottom
};
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10);
this.spawner.spawnAsteroid(pos.x, pos.y, Math.random() * 50 + 10);
}
}
@ -171,8 +178,9 @@ class World {
this.tps = this.tpsCount / 5 | 0;
this.tpsCount = 0;
this.tpsStart = Date.now();
if(this.tps < 50)
if(this.tps < 50) {
wingbase.warning(`${this.room.name} TPS: ${this.tps}`);
}
}
this.tpsCount++;

View file

@ -44,7 +44,7 @@ class Physics {
let s = SCALE;
let bodyDef = new Box2D.b2BodyDef();
bodyDef.userData = body;
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0);
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
bodyDef.angle = body.r || 0;
bodyDef.fixedRotation = false;
bodyDef.active = true;
@ -55,7 +55,7 @@ class Physics {
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.06;
bodyDef.type = body.type == 'structure' ?
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
if (body.player || true) bodyDef.allowSleep = false;
if (body.player) bodyDef.allowSleep = false;
let b2body = this.world.CreateBody(bodyDef);
let fixtureDef = new Box2D.b2FixtureDef();